C++超市管理系统
C++超市管理系统
心得
写这个东西最重要环节不是上手去实现代码,而是再此之前的整体的系统分析,分析整个系统所需要定义的类,以及不同类实现的功能,以及类与类之间的消息的传递。最重要的还有模块之间的解耦,为后续的迭代,二次开发打好基础。
由于还有两天就开学了,所以本人直接就略过最重要的一步系统分析,直接开始敲了,边敲边考虑类的设计,这样导致的结果就是类所属功能的划分不够明确,部分代码冗余,以及为了实现老师作业中的几个继承组合的要求,导致整个系统有些地方并不是十分合理,本系统只是实现了最基本的功能模块,且进货功能和账单功能并未做出单独的类去进行统一管理,但思想都大差不差,想要在此基础之上进行改进,增加模块的小伙伴可以自行添加修改。
简介:环境 :codeblocks 20.03
我将会按照代码从上到下的顺序解析代码……
开始写了个getTime()方法,是卖货产生流水的获取本地时间的方法。一个员工类,继承出一个管理员,管理员可以创建新用户。商品基类,继承出食品类,饮品类,化妆品类,此处我认为着实牵强(老师要求),各自特有的实现方法我没有用,直接输出字符串了。紧接着就是管理商品的商品管理类(销售员),继承出商品管理类(管理员);销售员权限低,只能进行售货和查看,管理员除此之外还可以进行查看流水账单,进行商品补充进货,还可以创建新的账户。最后就是一个超市类,它持有一个员工类指针,还有一个商品管理类指针。大概一两天差不多OK!
注意
有些文件操作写在了超市类的析构函数中,若不是正常退出系统,会导致文件与内存数据之间存在差异(可以改但是我懒…),还有就是本系统涉及到三个txt文件,Users.txt(账户信息),Goods.txt(仓库商品),Bills.txt(流水账单),没有User.txt就登录不了了,自己建一个(同目录即可)每行中间三个空位\t,末尾位\n;

展示


#include
#include
#include
#include
//包含getch(); using namespace std; string getTime() {
time_t timep; time (&timep); char tmp[64]; strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) ); return tmp; } //普通员工类 class LogUser {
protected: string name; //员工姓名 string userId; //员工唯一ID(账号) string userPassword; //登录密码 string userLevel; //权限(管理员、售货员) public: LogUser(string n,string ui,string up,string ul); friend ostream& operator<<(ostream& out,LogUser& obj); friend istream& operator>>(istream& in,LogUser& obj); // void readFile(istream & in); string getName(); string getUserId(); string getUserPassword(); string getUserLevel(); }; LogUser::LogUser(string n,string ui,string up,string ul):name(n),userId(ui),userPassword(up),userLevel(ul) {
} ostream& operator<<(ostream& out,LogUser& obj) {
out<<obj.getName()<<obj.getUserId()<<obj.getUserPassword()<<obj.getUserLevel(); } istream& operator>>(istream& in,LogUser& obj) {
in>>obj.name>>obj.userId>>obj.userPassword>>obj.userLevel; } //void LogUser::readFile(istream & in) //{
// in>>namename>>userId>>userPassword; //} //void LogUser::writeFile(ostream & out) //{
// out<
name<<"\t"<
userId<<"\t"<
userPassword<<'\n';
//} string LogUser::getName() {
return name; } string LogUser::getUserId() {
return userId; } string LogUser::getUserPassword() {
return userPassword; } string LogUser::getUserLevel() {
return userLevel; } //管理员类 class LogAdministrator : public LogUser {
protected: public: LogAdministrator(string n,string ui,string up,string ul); void addUser(); //管理员特有的创建员工账号功能 }; LogAdministrator::LogAdministrator(string n,string ui,string up,string ul):LogUser( n, ui,up, ul) {
} void LogAdministrator::addUser() {
ofstream out("Users.txt",ios::app); if(!out) {
cout<<"Users.txt error!"<<endl; abort(); } else {
system("cls"); string name,userId,userPassword,userLevel; cout<<"输入员工信息:"<<endl; cout<<"姓名:"; cin>>name; cout<<"ID:"; cin>>userId; cout<<"密码:"; cin>>userPassword; cout<<"权限:"; cin>>userLevel; out<<name<<"\t"<<userId<<"\t"<<userPassword<<"\t"<<userLevel<<"\n"; out.close(); } } //商品基类 class Goods {
protected: int id; //商品唯一编号 string name; //商品名称 double purchasingPrice; //进货价格 double price; //售价 int repertory; //库存 string producer; //生厂商 string brand; //品牌 string type; //类型 public: Goods(int i,string n,double pP,double p,int r,string pr,string b,string t); virtual void introduce(); //产品说明 void show(); int getId(); string getName(); double getPurchasingPrice(); double getPrice(); int getRepertory(); string getProducer(); string getBrand(); string getType(); void setRepertory(int r); }; Goods::Goods(int i,string n,double pP,double p,int r,string pr,string b,string t):name(n),producer(pr),brand(b),type(t) {
id=i; purchasingPrice = pP; price = p; repertory = r; } void Goods ::introduce() {
cout<<"商品基类!"<<endl; } void Goods :: show() {
cout<<"商品名称:"<<name<<endl<<"进货价格:"<<purchasingPrice<<"\t\t"<<"售价:"<<price<<endl<<"库存:"<<"\t"<<repertory<<"生厂商:"<<endl<<producer<<"品牌:"<<"\t"<<brand<<"类型:"<<type<<endl; } int Goods :: getId() {
return id; } string Goods :: getName() {
return name; } double Goods :: getPurchasingPrice() {
return purchasingPrice; } double Goods :: getPrice() {
return price; } int Goods :: getRepertory() {
return repertory; } string Goods :: getProducer() {
return producer; } string Goods :: getBrand() {
return brand; } string Goods :: getType() {
return type; } void Goods :: setRepertory(int r) {
repertory = r; } //食品类 class Food: public Goods {
protected: public: Food(int i,string n,double pP,double p,int r,string pr,string b,string t); void introduce(); }; Food::Food(int i,string n,double pP,double p,int r,string pr,string b,string t):Goods(i,n,pP,p,r,pr,b,t) {
} void Food::introduce() {
cout<<"食品类!"<<endl; } //饮料类 class Drink: public Goods {
protected: public: Drink(int i,string n,double pP,double p,int r,string pr,string b,string t); void introduce(); }; Drink::Drink(int i,string n,double pP,double p,int r,string pr,string b,string t):Goods(i,n,pP,p,r,pr,b,t) {
} void Drink::introduce() {
cout<<"饮料类!"<<endl; } //化妆品类 class Cosmetics: public Goods {
protected: public: Cosmetics(int i,string n,double pP,double p,int r,string pr,string b,string t); void introduce(); }; Cosmetics::Cosmetics(int i,string n,double pP,double p,int r,string pr,string b,string t):Goods(i,n,pP,p,r,pr,b,t) {
} void Cosmetics::introduce() {
cout<<"化妆品类!"<<endl; } //商品管理类(售货员) class GoodsManage_Employee {
protected: vector<Goods*> goods_Vector; vector<Goods*> shopping_Cart; vector<int> shopping_Cart_Sums; public: GoodsManage_Employee(); ~GoodsManage_Employee(); void showAll(); //显示全部商品信息 void showByIndex(int index); //显示单个产品信息 void sell(LogUser* log); //出售 virtual int showMenu(LogUser* log); //售货员视角菜单 }; GoodsManage_Employee::~GoodsManage_Employee() {
ofstream out("Goods.txt"); if(!out) {
cout<<"Goods.txt error!"<<endl; abort(); } else {
int n = goods_Vector.size(); for(int i = 0; i<n; i++) {
out<<goods_Vector[i]->getId()<<"\t"<<goods_Vector[i]->getName()<<"\t"<<goods_Vector[i]->getPurchasingPrice()<<"\t"<<goods_Vector[i]->getPrice()<<"\t"<<goods_Vector[i]->getRepertory()<<"\t"<<goods_Vector[i]->getProducer()<<"\t"<<goods_Vector[i]->getBrand()<<"\t"<<goods_Vector[i]->getType()<<"\n"; } out.close(); } } GoodsManage_Employee::GoodsManage_Employee() {
ifstream in("Goods.txt"); if(!in) {
cout<<"Goods.txt error!"<<endl; abort(); } else {
int id,repertory; string name, producer,brand,type; double purchasingPrice,price; while(!in.eof()) {
Goods *temp; in>>id>>name>>purchasingPrice>>price>>repertory>>producer>>brand>>type; if(type=="食品类") {
temp= new Food(id,name,purchasingPrice,price,repertory,producer,brand,type); } else if(type=="饮料类") {
temp = new Drink(id,name,purchasingPrice,price,repertory,producer,brand,type); } else if(type=="化妆品类") {
temp = new Cosmetics(id,name,purchasingPrice,price,repertory,producer,brand,type); } goods_Vector.push_back(temp); } goods_Vector.pop_back(); in.close(); } } void GoodsManage_Employee :: showAll() {
int n = goods_Vector.size(); system("cls"); cout<<"ID\t"<<"商品名称\t"<<"进货价格\t"<<"售价\t"<<"库存\t"<<"生产商\t"<<"品牌\t"<<"类型"<<endl; for(int i = 0; i<n; i++) {
cout<<goods_Vector[i]->getId()<<"\t"<<goods_Vector[i]->getName()<<"\t\t"<<goods_Vector[i]->getPurchasingPrice()<<"\t\t"<<goods_Vector[i]->getPrice()<<"\t"<<goods_Vector[i]->getRepertory()<<"\t"<<goods_Vector[i]->getProducer()<<"\t"<<goods_Vector[i]->getBrand()<<"\t"<<goods_Vector[i]->getType()<<endl; } cout<<"选择ID查看商品详细信息!输入0退出!"<<endl; int _p; cin>>_p; while(_p!=0) {
showByIndex(_p); cin>>_p; } } void GoodsManage_Employee :: showByIndex(int index) {
int n = goods_Vector.size(),i; for(i = 0; i<n; i++) {
if(goods_Vector[i]->getId()==index) {
goods_Vector[i]->show(); cout<<endl; break; } } if(i==n) cout<<"未查询到对应商品信息!请输入正确的ID!"<<endl; } void GoodsManage_Employee :: sell(LogUser* log) {
while(1) {
system("cls"); int n = goods_Vector.size(); cout<<"ID\t"<<"商品名称\t"<<"进货价格\t"<<"售价\t"<<"库存\t"<<"生产商\t"<<"品牌\t"<<"类型"<<endl; for(int i = 0; i<n; i++) {
cout<<goods_Vector[i]->getId()<<"\t"<<goods_Vector[i]->getName()<<"\t\t"<<goods_Vector[i]->getPurchasingPrice()<<"\t\t"<<goods_Vector[i]->getPrice()<<"\t"<<goods_Vector[i]->getRepertory()<<"\t"<<goods_Vector[i]->getProducer()<<"\t"<<goods_Vector[i]->getBrand()<<"\t"<<goods_Vector[i]->getType()<<endl; } double sum=0; cout<<"购物车:\t\t\t\t\t总金额:"; if(shopping_Cart.size()==0) {
cout<<sum<<endl<<"啥也没有啊!赶紧购物吧!"<<endl; } else {
int m = shopping_Cart.size(); for(int i = 0; i<m; i++) {
sum += shopping_Cart[i]->getPrice()*shopping_Cart_Sums[i]; } cout<<sum<<endl; cout<<"ID\t"<<"商品名称\t"<<"售价\t"<<"购买数量\t"<<"总价\t"<<endl; for(int i = 0; i<m; i++) {
cout<<shopping_Cart[i]->getId()<<"\t"<<shopping_Cart[i]->getName()<<"\t\t"<<shopping_Cart[i]->getPrice()<<"\t"<<shopping_Cart_Sums[i]<<"\t\t"<<shopping_Cart_Sums[i]*shopping_Cart[i]->getPrice()<<endl; } cout<<endl; } int index,amount; cout<<"选择ID...(输入0退出)"<<endl; cin>>index; if(index==0) break; if(index<0||index>n) {
cout<<"ID错误!"<<endl<<"退出中"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } break; } cout<<endl<<"输入购买输入数量..."<<endl; cin>>amount; for(int i = 0; i<n; i++) {
if(goods_Vector[i]->getId()==index) {
if(amount>goods_Vector[i]->getRepertory()) {
cout<<"库存不足!请输入正确数量"; for(int j=0; j<6; j++) {
cout<<"."; Sleep(250); } break; } goods_Vector[i]->setRepertory(goods_Vector[i]->getRepertory() - amount); int m = shopping_Cart.size(),mm=0; for( mm = 0; mm<m; mm++) {
if(shopping_Cart[mm]->getId()==goods_Vector[i]->getId()) {
shopping_Cart_Sums[mm]+=amount; break; } } if(mm==m) {
shopping_Cart.push_back(goods_Vector[i]); shopping_Cart_Sums.push_back(amount); } for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } cout<<endl<<"购买成功!"; Sleep(1000); break; } } } ofstream out("Bills.txt",ios::app); if(!out) {
cout<<"Bills.txt error!"<<endl; abort(); } else {
int m = shopping_Cart.size(); for(int i = 0; i<m; i++) {
out<<getTime()<<"\t"<<log->getName()<<"\t"<<shopping_Cart[i]->getId()<<"\t"<<shopping_Cart_Sums[i]<<"\n"; } } } int GoodsManage_Employee :: showMenu(LogUser* log) {
for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"1.查看商品信息"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"2.购买商品"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"0.退出系统"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<"请输入执行操作的编号:"<<endl; int choice; cin>>choice; if(choice==1) {
showAll(); } else if(choice==2) {
sell(log); } else if(choice==0) {
} else {
cout<<"输入错误,请重新输入..."<<endl; } return choice; } //商品管理类(管理员) class GoodsManage_Administrator : public GoodsManage_Employee {
protected: public: GoodsManage_Administrator(); ~GoodsManage_Administrator(); void showBills(); //显示账单 void purchaseGoods(); //进货 void addUser(LogAdministrator* log); //增加账户 int showMenu(LogUser* log); }; GoodsManage_Administrator :: GoodsManage_Administrator() {
} GoodsManage_Administrator :: ~GoodsManage_Administrator() {
} void GoodsManage_Administrator :: showBills() {
ifstream in("Bills.txt"); if(!in) {
cout<<"Users.txt error!"<<endl; abort(); } else {
system("cls"); cout<<"日期\t\t\t"<<"售货员\t"<<"商品ID\t"<<"数量\t"<<endl; while(!in.eof()) {
string time1,time2,name; int type,sum; in >>time1>>time2>>name>>type>>sum; if(time1=="") break; cout<<time1<<" "<<time2<<"\t"<<name<<"\t"<<type<<"\t"<<sum<<endl; } in.close(); } cout<<"按任意键退出!"; getch(); } void GoodsManage_Administrator :: purchaseGoods() {
while(1) {
system("cls"); int n = goods_Vector.size(); cout<<"ID\t"<<"商品名称\t"<<"进货价格\t"<<"售价\t"<<"库存\t"<<"生产商\t"<<"品牌\t"<<"类型"<<endl; for(int i = 0; i<n; i++) {
cout<<goods_Vector[i]->getId()<<"\t"<<goods_Vector[i]->getName()<<"\t\t"<<goods_Vector[i]->getPurchasingPrice()<<"\t\t"<<goods_Vector[i]->getPrice()<<"\t"<<goods_Vector[i]->getRepertory()<<"\t"<<goods_Vector[i]->getProducer()<<"\t"<<goods_Vector[i]->getBrand()<<"\t"<<goods_Vector[i]->getType()<<endl; } cout<<"退出:0\t增加新产品:1\t\t增加已有产品数量:2\n"; int _P; cin>>_P; if(_P==0) {
cout<<"退出中"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } break; } else if(_P==1) {
int m = goods_Vector.size(); cout<<"1.食品类\t2.饮料类\t3.化妆品类"<<endl; int c; cin>>c; string name,producer,brand,type; double purchasingPrice,price; int repertory; if(c==1) {
cout<<"商品名称:"; cin>>name; cout<<"进货价格:"; cin>>purchasingPrice; cout<<"售价:"; cin>>price; cout<<"库存:"; cin>>repertory; cout<<"生厂商:"; cin>>producer; cout<<"品牌:"; cin>>brand; goods_Vector.push_back(new Food(m+1,name,purchasingPrice,price,repertory,producer,brand,"食品类")); } else if(c==2) {
cout<<"商品名称:"; cin>>name; cout<<"进货价格:"; cin>>purchasingPrice; cout<<"售价:"; cin>>price; cout<<"库存:"; cin>>repertory; cout<<"生厂商:"; cin>>producer; cout<<"品牌:"; cin>>brand; goods_Vector.push_back(new Drink(m+1,name,purchasingPrice,price,repertory,producer,brand,"饮料类")); } else if(c==3) {
cout<<"商品名称:"; cin>>name; cout<<"进货价格:"; cin>>purchasingPrice; cout<<"售价:"; cin>>price; cout<<"库存:"; cin>>repertory; cout<<"生厂商:"; cin>>producer; cout<<"品牌:"; cin>>brand; goods_Vector.push_back(new Cosmetics(m+1,name,purchasingPrice,price,repertory,producer,brand,"化妆品类")); } else {
cout<<"输入错误"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } } } else if(_P==2) {
int ID; cout<<"商品ID:"; cin>>ID; if(ID<=0||ID>goods_Vector.size()) {
cout<<"ID非法,退出中"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } } cout<<"输入进货数量:"; int sum; cin>>sum; goods_Vector[ID-1]->setRepertory(goods_Vector[ID-1]->getRepertory() + sum); cout<<"加入中"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } cout<<endl<<"加入成功"; Sleep(1000); } else {
cout<<"输入错误...\n退出中"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } } } } void GoodsManage_Administrator ::addUser(LogAdministrator* log) {
log->addUser(); } int GoodsManage_Administrator :: showMenu(LogUser* log) {
for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"1.查看商品信息"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"2.购买商品"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"3.账单"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"4.进货"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"5.创建账户"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"0.退出系统"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<"请输入执行操作的编号:"<<endl; int choice; cin>>choice; if(choice==1) {
showAll(); } else if(choice==2) {
sell(log); } else if(choice==3) {
showBills(); } else if(choice==4) {
purchaseGoods(); } else if(choice==5) {
addUser((LogAdministrator*)log); } else if(choice==0) {
} else {
cout<<"输入错误,请重新输入..."<<endl; } return choice; } //超市类 class Supermarket {
private: LogUser *user = nullptr; GoodsManage_Employee *goodsManage = nullptr; GoodsManage_Employee* getGoodsManage(); void login(); void Welcome(); public: Supermarket(); ~Supermarket(); void showMenu(); }; GoodsManage_Employee* Supermarket::getGoodsManage() {
if(user->getUserLevel()=="售货员") {
return new GoodsManage_Employee(); } else if(user->getUserLevel()=="管理员") {
return new GoodsManage_Administrator(); } } void Supermarket::login() {
ifstream in; string Log_Id,Log_Password; string name,userId,userPassword,userLevel; while(user==nullptr) {
in.open("Users.txt"); system("cls"); for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"欢迎来到超市管理系统"<<setw(12)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(16)<<left<<"●"<<setw(16)<<"请登录......"<<setw(16)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<"账号:"; cin>>Log_Id; cout<<"密码:"; cin>>Log_Password; if(!in) {
cout<<"Users.txt error!"<<endl; abort(); } else {
while(!in.eof()) {
in >>name>>userId>>userPassword>>userLevel; if(Log_Id==userId&&Log_Password==userPassword) {
if(userLevel=="管理员") {
user = new LogAdministrator(name,userId,userPassword,userLevel); } else {
user = new LogUser(name,userId,userPassword,userLevel); } break; } } in.close(); } if(user==nullptr) {
cout<<"账号或密码错误!重新登录"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } } } } void Supermarket :: Welcome() {
system("cls"); for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(10)<<left<<"●"<<setw(2)<<"欢迎"<<user->getName()<<"!"<<setw(27)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(10)<<left<<"●"<<setw(2)<<"正在进入超市管理系统"<<"(权限:"<<user->getUserLevel()<<")"<<setw(5)<<right<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; cout<<setw(46)<<left<<"●"<<"●"<<endl; for(int i=0; i<24; i++) cout<<"●"; puts(""); cout<<"加载数据中"; for(int i=0; i<6; i++) {
cout<<"."; Sleep(250); } } Supermarket::Supermarket() {
login(); goodsManage = getGoodsManage(); Welcome(); } Supermarket::~Supermarket() {
delete user; delete goodsManage; } void Supermarket :: showMenu() {
while(1) {
system("cls"); cout<<"用户:"<<user->getName()<<"\t\t\t "<<"权限:"<<user->getUserLevel()<<endl; if(goodsManage->showMenu(user)==0) break; } } int main() {
Supermarket s; s.showMenu(); cout<<getTime(); return 0; }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212739.html原文链接:https://javaforall.net
