一个简易的饭店管理系统——cpp课程设计
#include<iostream>
#include<string>
#include<utility>
#include<vector>
#include<fstream>
#include<sstream>
#include<cstring>
using namespace std;
#define STAR "*******************************************************************************************"
#define SPACE " "
#define HELLO "*********************************欢迎来到饭店管理系统**************************************"
#define TOPSPINE "********************************食材表***************************************************"
#define LOGIN "**************************************管理员登录*******************************************"
#define CHOOSE "请选择要进行的操作(按0退出)"
#define MaxAmount 500
#define SumTable 100
class goods {
protected:
int no;
string name;
double price;
public:
goods();
goods(int n_,string n,double a);
int getno();
string getname();
double getprice();
~goods();
};
class food_material:public goods {
protected:
double amount;
public:
food_material();
food_material(int n_,const string n,double a,double b);
~food_material();
double getamount();
void addamount(double a);
friend class fstream;
friend class manager;
static double sumamount;
};
class dishes:public goods {
protected:
public:
vector<pair<food_material *,double> > Ingredientsvec;
dishes();
dishes(int n_,const string n,double a,vector<pair<food_material *,double> > b);
~dishes();
friend class fstream;
friend class manager;
};
class user {
protected:
int no;
int username;
int psword;
public:
user();
user(int a,int b,int c);
int getno();
int getusername();
int getpsword();
};
class manager:public user {
protected:
vector<food_material> vecmaterial;
vector<dishes> vecdishes;
public:
manager();
manager(int a,int b,int c);
void showmaterial();
void showdishes();
void showtables();
void order_dishes();
void purchase_materials();
void count_materials();
void Calculating_turnover();
};
class table {
protected:
int no;
double pay;
public:
bool isused;
table();
table(int a);
int getno();
double getpay();
void addpay(double a);
void startuse();
void finishuse();
};
class order {
private:
static int ordercount;
int orderID;
int tableID;
int listcount;
double pay;
vector<pair<dishes,int> > ordervec;
public:
order() {}
order(int a);
void orderonedish(const dishes &a,int b);
void display();
void displaydishes();
void setpay(double a);
double getpay();
};
class RestuarantManage {
protected:
public:
vector<food_material *> fptr;
vector<dishes *> dptr;
vector<table *> tptr;
vector<order *> optr;
user * queryuser(int a,int b);
user * login();
void showmenu();
void menu();
bool check_inventory(int a,int b);
void use_inventory(int a,int b);
void init();
};
RestuarantManage rm;
goods::goods() {
name=" ";
price=0.;
}
goods::goods(int n_,const string n,double a) {
no=n_;
name=n;
price=a;
}
int goods::getno() {
return no;
}
string goods::getname() {
return name;
}
double goods::getprice() {
return price;
}
goods::~goods() {
}
food_material::food_material() {
amount=0.;
}
food_material::food_material(int n_,const string n,double a,double b) :goods(n_,n,a) {
amount=b;
sumamount+=b;
}
double food_material::getamount() {
return amount;
}
void food_material::addamount(double a) {
amount+=a;
}
food_material::~food_material() {
}
dishes::dishes() {
}
dishes::dishes(int n_,const string n,double a,vector<pair<food_material*,double> > b) :goods(n_,n,a) {
Ingredientsvec=b;
}
dishes::~dishes() {
}
user::user(int a,int b,int c) {
no=a;
username=b;
psword=c;
}
int user::getno() {
return no;
}
int user::getusername() {
return username;
}
int user::getpsword() {
return psword;
}
manager::manager(int a,int b,int c):user(a,b,c) {
vecmaterial.clear();
vecdishes.clear();
fstream ifs;
ifs.open("food_material.txt",ios::in);
if (!ifs.is_open()) {
cout<<"cannot open file \"food_material.txt\""<<endl;
return;
}
food_material fm;
while(ifs>>fm.no&&ifs>>fm.name&&ifs>>fm.price) {
vecmaterial.push_back(fm);
}
ifs.close();
ifs.open("dishes.txt",ios::in);
if (!ifs.is_open()) {
cout<<"cannot open file \"dishes.txt\""<<endl;
return;
}
string s;
int s1;
double s2;
while(getline(ifs,s)) {
istringstream ss(s);
dishes ds;
ss>>ds.no>>ds.name>>ds.price;
vecdishes.push_back(ds);
}
ifs.close();
}
void manager::showmaterial() {
int i=0;
for (vector<food_material>::iterator iter=vecmaterial.begin();iter!=vecmaterial.end();++iter,++i) {
cout<<iter->getno()<<"."<<iter->getname()<<" ¥"<<iter->getprice()<<"元/斤"<<"\t";
if((i+1)%3==0) {
cout<<endl;
}
}
cout<<endl;
}
void manager::showtables() {
for (int i=0;i<rm.tptr.size();++i) {
if(rm.tptr[i]->isused) {
cout<<rm.tptr[i]->getno()<<"("<<"x"<<")"<<"\t";
} else {
cout<<rm.tptr[i]->getno()<<"("<<"√"<<")"<<"\t";
}
if((i+1)%3==0) {
cout<<endl;
}
}
cout<<endl;
}
void manager::showdishes() {
int i=0;
for (vector<dishes>::iterator iter=vecdishes.begin();iter!=vecdishes.end();++iter,++i) {
cout<<iter->getno()<<"."<<iter->getname()<<" ¥"<<iter->getprice()<<"元"<<"\t";
if((i+1)%3==0) {
cout<<endl;
}
}
cout<<endl;
}
void manager::order_dishes() {
cout<<"**************欢迎使用点菜系统*************"<<endl;
cout<<"可供选择的桌号如下:"<<endl;
showtables();
cout<<"请输入桌号:(输入-1退出)"<<endl;
int tableno;
int i,flag=0;
hr: cin>>tableno;
if(tableno==-1) {
return ;
} else {
for(i=0;i<SumTable;i++)
if(rm.tptr[i]->getno()==tableno) {
if(rm.tptr[i]->isused) {
cout<<"此桌已被占用,请重新选择"<<endl;
goto hr;
} else {
cout<<"为您选中"<<tableno<<"号桌"<<endl;
flag=1;
break;
}
}
if(!flag) {
cout<<"桌号不存在"<<endl;
} else {
cout<<"共有"<<vecmaterial.size()<<"种菜品"<<endl;
cout<<" 菜品清单如下: "<<endl;
showdishes();
vector<pair<int,int> > v;
while(1) {
int no;
hr2: cout<<"请输入菜品编号(输入-1取消点餐退出系统,输入-2结束点餐并确认)"<<endl;
cin>>no;
if(no==-1) {
v.clear();
rm.tptr[i]->finishuse();
cout<<"正在取消之前点餐操作,退出点餐系统..."<<endl;
return ;
} else if(no>=0&&no<vecmaterial.size()) {
cout<<"购买?份(输入大于0的整数)"<<endl;
int count;
cin>>count;
if(!rm.check_inventory(no,count)) {
cout<<"食材不足,请重新操作"<<endl;
goto hr2;
} else {
v.push_back(make_pair(no,count));
}
} else if(no==-2) {
if(!v.empty()) {
rm.optr.push_back(new order(rm.tptr[i]->getno()));
rm.tptr[i]->startuse();
for(int j=0;j<v.size();++j) {
rm.use_inventory(v[j].first,v[j].second);
rm.tptr[i]->addpay(rm.dptr[v[j].first]->getprice()*v[j].second);
rm.optr[rm.optr.size()-1]->orderonedish(*rm.dptr[v[j].first],v[j].second);
}
cout<<"您的桌号为:"<<rm.tptr[i]->getno()<<endl;
cout<<"您的点餐清单为:";
double l=rm.tptr[i]->getpay();
int k=rm.optr.size()-1;
rm.optr[k]->displaydishes();
rm.optr[k]->setpay(l);
cout<<"需付费:"<<l<<"元"<<endl;
break;
}
} else {
cout<<"无此菜品,请重新操作" <<endl;
goto hr2;
}
}
}
}
}
void manager::purchase_materials() {
cout<<"**************欢迎使用进货系统*************"<<endl;
cout<<"共有"<<vecmaterial.size()<<"种食材可以购买"<<endl<<endl;
cout<<" 可供选择的食材如下: "<<endl;
showmaterial();
while(1) {
int no;
hr3: cout<<"请输入食材编号(输入-1结束进货)"<<endl;
cin>>no;
if (no==-1) {
cout<<"正在退出进货系统..."<<endl;
system("pause");
system("cls");
return ;
} else if(0<=no&&no<vecmaterial.size()){
cout<<"购买?斤"<<endl;
double count;
cin>>count;
if(food_material::sumamount+count<MaxAmount) {
rm.fptr[no]->addamount(count);
food_material::sumamount+=count;
} else {
cout<<"cannot purchase materials,because stock will be not enough"<<endl;
cout<<"back to the main menu..."<<endl;
system("pause");
system("cls");
return ;
}
} else {
cout<<"标号为"<<no<<"的食材不存在"<<endl;
goto hr3;
}
}
}
void manager::count_materials() {
cout<<"**************欢迎使用食材统计系统*************"<<endl;
cout<<" 当前贮存的食材如下: "<<endl;
for (int i=0;i<rm.fptr.size();++i) {
cout<<rm.fptr[i]->no<<"."<<rm.fptr[i]->name<<" "<<rm.fptr[i]->getamount()<<"斤"<<"\t";
if((i+1)%3==0) {
cout<<endl;
}
}
cout<<endl;
}
void manager::Calculating_turnover() {
double sum=0;
for(int i=0;i<rm.optr.size();++i) {
sum+=rm.optr[i]->getpay();
}
cout<<"本饭馆累计营业额为"<<sum<<"元"<<endl;
cout<<"查询订单?(输入0==no,1==yes)"<<endl;
int opt;
cin>>opt;
if(!opt) {
return ;
} else if(opt==1) {
cout<<"请输入订单号"<<endl;
int no;
cin>>no;
rm.optr[no]->display();
}
}
order::order(int a) {
ordercount++;
tableID=a;
orderID=ordercount;
}
void order::orderonedish(const dishes &a,int b) {
ordervec.push_back(make_pair(a,b));
listcount++;
}
void order::displaydishes() {
for(int i=0;i<listcount;i++)
cout<<ordervec[i].first.getname()<<"("<<ordervec[i].second<<"份)""\t";
cout<<endl;
}
void order::display() {
cout<<"订单信息"<<endl;
cout<<"订单号:"<<orderID<<",";
cout<<"桌号:"<<tableID<<",";
cout<<"所购菜品编号清单:";
for(int i=0;i<listcount;i++)
cout<<ordervec[i].first.getname()<<"("<<ordervec[i].second<<"份)""\t";
cout<<endl;
}
void order::setpay(double a) {
pay=a;
}
double order::getpay() {
return pay;
}
user * RestuarantManage::queryuser(int a,int b) {
user * uptr=NULL;
fstream ifs("user.txt",ios::in);
if(!ifs) {
cout<<"cannot open file \"user.txt\" "<<endl;
}
int no,username,psword;
while(ifs>>no&&ifs>>username&&ifs>>psword) {
if(username==a) {
if(psword==b) {
cout<<"管理员身份验证通过"<<endl;
uptr = new user(no,username,psword);
return uptr;
} else {
cout<<"用户名或密码错误"<<endl;
return NULL;
}
}
}
cout<<"管理员不存在"<<endl;
return NULL;
}
user * RestuarantManage::login() {
cout<<STAR<<endl;
cout<<SPACE<<endl;
cout<<HELLO<<endl;
cout<<SPACE<<endl;
cout<<LOGIN<<endl;
int username,psword;
cout<<"请输入账号:(6位纯数字)" <<endl;
cin>>username;
cout<<"请输入密码:(6位纯数字)" <<endl;
cin>>psword;
if(user * uptr=queryuser(username,psword)) {
return uptr;
} else {
cout<<"请重新输入"<<endl;
system("pause");
system("cls");
login();
}
}
void RestuarantManage::showmenu() {
cout<<STAR<<endl;
cout<<SPACE<<endl;
cout<<HELLO<<endl;
cout<<" 0: 退出 "<<endl;
cout<<" 1: 点菜 "<<endl;
cout<<" 2: 进货 "<<endl;
cout<<" 3: 食材统计 "<<endl;
cout<<" 4: 统计流水 "<<endl;
cout<<" 5: 注销 "<<endl;
cout<<STAR<<endl;
}
void RestuarantManage::init() {
fstream ifs;
ifs.open("food_material.txt",ios::in);
if (!ifs.is_open()) {
cout<<"cannot open file \"food_material.txt\""<<endl;
return;
}
int no;
string name;
double price;
while(ifs>>no&&ifs>>name&&ifs>>price) {
food_material* x=new food_material(no,name,price,0.0);
fptr.push_back(x);
}
ifs.close();
ifs.open("dishes.txt",ios::in);
if (!ifs.is_open()) {
cout<<"cannot open file \"dishes.txt\""<<endl;
return;
}
string s;
int s1;
double s2;
while(getline(ifs,s)) {
istringstream ss(s);
vector<pair<food_material *,double> >v;
ss>>no>>name>>price;
while(ss>>s1>>s2) {
v.push_back(make_pair(fptr[s1],s2));
}
dishes* y=new dishes(no,name,price,v);
dptr.push_back(y);
}
ifs.close();
for(int i=0;i<SumTable;++i) {
table* z=new table(i);
tptr.push_back(z);
}
}
void RestuarantManage::menu() {
user *uptr=login();
init();
manager * mptr=new manager(uptr->getno(),uptr->getusername(),uptr->getpsword());
int tmp;
system("pause");
system("cls");
while(1) {
showmenu();
cout<<CHOOSE<<endl;
cin>>tmp;
switch(tmp) {
case 0:
cout<<"正在退出系统..."<<endl;
system("pause");
exit(0);
case 1:
system("cls");
mptr->order_dishes();
system("pause");
system("cls");
break;
case 2:
system("cls");
mptr->purchase_materials();
break;
case 3:
system("cls");
mptr->count_materials();
system("pause");
system("cls");
break;
case 4:
system("cls");
mptr->Calculating_turnover();
system("pause");
system("cls");
break;
case 5:
delete mptr;
cout<<"用户已注销"<<endl;
mptr=new manager(login()->getno(),login()->getusername(),login()->getpsword());;
break;
default :
cout<<"无此选项,请重新选择" <<endl;
}
}
}
bool RestuarantManage::check_inventory(int a,int b) {
int i;
for(i=0;i<dptr.size();++i) {
if(dptr[i]->getno()==a) {
break;
}
}
int flag=1;
for(int j=0;j<dptr[i]->Ingredientsvec.size();++j) {
if(dptr[i]->Ingredientsvec[j].first->getamount()-dptr[i]->Ingredientsvec[j].second*b<0) {
flag=0;
break;
}
}
if(!flag) {
return false;
} else {
return true;
}
}
void RestuarantManage::use_inventory(int a,int b) {
int i;
if(check_inventory(a,b)) {
for(i=0;i<dptr.size();++i) {
if(dptr[i]->getno()==a) {
break;
}
}
for(int j=0;j<dptr[i]->Ingredientsvec.size();++j) {
dptr[i]->Ingredientsvec[j].first->addamount(-1.0*dptr[i]->Ingredientsvec[j].second*b);
}
} else {
cout<<"食材不足,无法制作"<<b<<"份编号为"<<a<<"的菜品"<<endl;
}
}
table::table(int a) {
no=a;
pay=0;
isused=false;
}
int table::getno() {
return no;
}
double table::getpay() {
if(isused==false) {
return 0;
} else {
return pay;
}
}
void table::addpay(double a) {
pay+=a;
}
void table::startuse() {
isused=true;
}
void table::finishuse() {
pay=0;
isused=false;
}
double food_material::sumamount=0;
int order::ordercount=0;
int main() {
rm.menu();
cout<<SPACE<<endl;
return 0;
}
//dishes.txt
0 Scrambled_Eggs_with_Tomato 13 7 0.5 6 0.5
1 Pork_with_pickled_cabbage 33 0 0.7 4 0.5
2 Stewed_potato_with_spareribs 55 4 0.6 8 0.4
3 Cola_Chicken_Wings 45 9 1.2
4 Braised_Prawns 50 5 1
5 Kung_Pao_Chicken 37 9 1.1
6 Boiled_meat 49 4 0.9
7 Eggplant_with_radish 18 1 0.8 2 0.8
8 Elbow 60 4 1.2
9 Quick_cucumber 13 1 0.9
//food_material.txt
0 cabbage 1
1 eggplant 3
2 radish 4
3 cucumber 2
4 pork 35
5 Prawns 28
6 Tomatoes 4
7 egg 5
8 potato 3
9 chicken 25
//user.txt
0 123456 123456
1 212421 325324
2 124214 124211
3 234325 324234
4 123214 436342
5 124325 533242
6 346433 242323
7 534534 533241
8 124214 756745
9 523543 623542