面向对象程序课程设计(2014/2015学年第一学期第20周)指导教师:庄巧莉、杨东鹤班级:计算机科学与技术13(1)学号:姓名:面向对象程序课程设计目录一、题目二、需求分析三、系统结构图四、类的设计五、程序代码与说明六、运行结果与分析七、心得与体会一:题目自助点餐系统二:需求分析有一个小型餐厅,该餐厅提供即时就餐和外卖服务。
现在这个餐厅打算使用自助点餐系统,让顾客自己点餐,实现以下功能。
1、根据顾客的选择正确打出账单2、正确统计出每一天的及时就餐和外卖的销售情况3、实现对餐厅菜式和价格的有效管理4、考虑点餐的自动排序问题,使顾客可以实时查询到自己菜单处理情况三:系统结构图四:类的设计Client类:客户类,包含客户属性姓名,电话号码,费用等信息,登记客户,记录客户订单信息ClientManager类:客户管理类,用于管理客户,记录客户数量,存储客户菜单信息,查询客户订单信息,统计订单FeeManager类:用于记录就餐、外卖的销售费用,统计收入Food类:餐厅食物的类,包含食物名称和价格FoodManager类:管理菜单,用于添加食物,修改食物,删除食物,记录不同食物的信息,呈现菜单MenuView类:用于打印各种操作界面五:程序代码与说明Client.h#ifndef CLIENT#define CLIENT#include"FoodManager.h"#include<iostream>using namespace std;class Client{private:int number;//客户编号int type;//客户的类型,1表示就餐,2表示外卖double fee;//客户账单费用char *name;//客户名称char *phone;//客户手机号码public:Client(){}//构造函数char * getName();//返回客户姓名int num[100];//num[i]存储食物数量,i表示食物编号FoodManager client_fm; //管理客户所点的食物Client(char *n,char *p);//构造函数int getNumber();//返回客户编号void setNumber(int n);//设置客户编号int getType();//返回客户类型void setType(int n);//设置客户类型void addFee(double x);//添加费用double getFee();//返回费用void showClientMessage();//展示客户信息void changNum();//修改客户所点的食物的数量void deleFood();//删除客户所选择的食物};#endifClient.cpp#include"Client.h"#include<iomanip>Client::Client(char *s,char *p){name = s;phone = p;fee = 0;for(int i = 0 ; i < 100 ; i++)num[i] = 0 ;cout<<"客户注册成功"<<endl;}int Client::getNumber(){return number;}void Client::setNumber(int n){number = n;}int Client::getType(){return type;}void Client::setType(int n){type = n;}void Client::addFee(double x){fee += x;}double Client::getFee(){return fee;}void Client::showClientMessage(){char * ss;if(type == 1)ss = "就餐";else if(type == 2)ss = "外卖";cout<<"姓名: "<<name<<"\t\t"<<ss<<endl;cout<<"手机号码: "<<phone<<endl;cout<<"订单总价:"<<fee<<endl;cout<<"订单详情:"<<endl;cout<<setw(10)<<setiosflags(ios::left)<<"食物序列"<<setw(10)<<setiosflags(ios::left)<<"菜名"<<"\t数量\t价格"<<endl;for(int i = 0 ; i < client_fm.getTotal() ; i ++){cout<<setw(10)<<setiosflags(ios::left)<<setw(10)<<i+1<<setiosflags(ios::left)<<client_fm.f ood[i].getName()<<"\t"<<num[i]<<"\t"<<num[i]*client_fm.food[i].getPrice()<<endl;}cout<<"总价:\t\t"<<fee<<endl;}char * Client::getName(){return name;}void Client::changNum(){int n;int numss;cout<<"请输入食物序号:";cin>>n;cout<<"您选择了食物:"<<client_fm.getNameByList(n-1)<<endl;cout<<"请输入数量:";cin>>numss;fee += (numss - num[n-1] )*client_fm.getPriceByName(client_fm.getNameByList(n-1));num[n-1]=numss;}void Client::deleFood(){int n;cout<<"请输入食物序号:";cin>>n;cout<<"您选择了食物:"<<client_fm.getNameByList(n-1)<<endl;fee -= (num[n-1] )*client_fm.getPriceByName(client_fm.getNameByList(n-1));for(int i = n-1 ; i < client_fm.getTotal() ; i ++){client_fm.food[i]=client_fm.food[i+1];num[i]=num[i+1];}client_fm.setTotal(client_fm.getTotal()-1);}ClientManager.h#ifndef CLIENTMANAGER#define CLIENTMANAGER#include"Client.h"#include<iostream>using namespace std;#define MAX_CLIENT_NUM 60 //最大客户数量class ClientManager{public:ClientManager();//构造函数int clientNum ;//记录客户数量Client client[MAX_CLIENT_NUM];//记录存储客户void addClient(Client c);//添加新客户int getClientNum();//返回客户数量void showQuery(char * s);//查询客户void showAll();//显示所有客户};#endifClientManager.cpp#include"ClientManager.h"ClientManager::ClientManager(){clientNum = 0;}void ClientManager::addClient(Client c){client[clientNum++] = c;cout<<"客户注册成功"<<endl;}int ClientManager::getClientNum(){return clientNum;}void ClientManager::showQuery(char * s){int count = 0;int th;cout<<"----------------------------------"<<endl;for(int i = 0; i < clientNum ; i ++)if(client[i].getType()==1){cout<<"第"<<++count<<"单: "<<client[i].getName()<<"\t总价是:"<<client[i].getFee()<<"元"<<"\t就餐"<<endl<<endl;if(strcmp(client[i].getName(),s)==0)th =count;}for(int i = 0; i < clientNum ; i ++)if(client[i].getType()==2){cout<<"第"<<++count<<"单: "<<client[i].getName()<<"\t总价是:"<<client[i].getFee()<<"元"<<"\t外卖"<<endl<<endl;if(strcmp(client[i].getName(),s)==0)th =count;}cout<<"您当前排在第"<<th<<"单"<<endl;cout<<"----------------------------------"<<endl;}void ClientManager::showAll(){for(int i = 0 ; i < clientNum ; i ++ ){if(client[i].getType()==1){cout<<"订单号:"<<i+1<<endl;client[i].showClientMessage();cout<<endl;}}for(int i = 0 ; i < clientNum ; i ++ ){if(client[i].getType()==2){cout<<"订单号:"<<i+1<<endl;client[i].showClientMessage();cout<<endl;}}}Food.h#ifndef FOOD#define FOODclass Food{private:char *name;//食物名称double price;//食物价格public:Food(){};//构造函数Food(char *s,double p); //构造函数void setName(char *s); //更改食物名称char* getName();//返回食物名称double getPrice();//返回食物价格void setPrice(double p); //设置食物价格void showMessage();//显示食物的名称和价格};#endifFood.cpp#include"Food.h"#include<iostream>using namespace std;Food::Food(char *s,double p){name = s;price = p;}void Food::setName(char *s){name = s;}void Food::setPrice(double p){price = p;}void Food::showMessage(){cout<<name<<"\t"<<price<<"Ԫ"<<endl;}double Food::getPrice(){return price;}char* Food::getName(){return name;FoodManager.h#ifndef FOODMANAGER#define FOODMANAGER#include"Food.h"#define MAX_FOOD_NUM 99 //最多容纳的食物种类数class FoodManager{int total ;//食物种类数public:FoodManager();//构造函数int getTotal() ;//返回食物种类数Food food[MAX_FOOD_NUM];//记录食物void addFood(Food f);//添加食物void addFood(char *s,double p);//添加食物void deleteFood();//删除食物void changePrice();//修改食物价格void showFood();//显示食物信息char * getNameByList(int list);//通过食物编号返回食物名称double getPriceByName(char *s);//通过食物名称返回食物价格int getListByName(char *s);//通过食物名称返回食物编号void setTotal(int x);//修改食物种类数};#endifFoodManager.cpp#include"FoodManager.h"#include<iostream>using namespace std;#include<iomanip>FoodManager::FoodManager(){total = 0;}void FoodManager::addFood(Food f){food[total++]=f;cout<<"成功添加了食物,当前共有"<<total<<endl;}void FoodManager::showFood(){cout<<"食物中共有"<<total<<"种"<<endl;cout<<"食物编号"<<"名称\t\t"<<"单价"<<endl;for(int i = 0 ; i < total ; i ++ )cout<<setw(10)<<setiosflags(ios::left)<<i<<setw(10)<<setiosflags(ios::left)<<food[i].getNa me()<<"\t"<<food[i].getPrice()<<endl;}void FoodManager::changePrice(){char *s;int code;s = new char[20];double p;cout<<"请输入食物编号: ";cin>>code;s = food[code].getName();cout<<"您选择了食物:"<<s<<endl;cout<<"请输入价格: ";cin>>p;for(int i = 0; i < total ; i ++)if(strcmp(food[i].getName(),s)==0){food[i].setPrice(p);cout<<"修改成功"<<endl;return;}cout<<"没有这种食物,修改失败"<<endl;}void FoodManager::deleteFood(){char *s;s = new char[20];cout<<"请输入菜名: ";cin>>s;for(int i = 0 ; i < total ; i ++)if(strcmp(food[i].getName(),s)==0){cout<<"成功删除"<<s<<endl;total --;for(int t = i; t < total ; t ++)food[t]=food[t+1];return;}cout<<"没有这种食物"<<endl;}void FoodManager::addFood(char *s , double p){food[total++]=Food(s,p);}double FoodManager::getPriceByName(char *s){for(int i = 0 ; i < total ; i ++)if(strcmp(food[i].getName(),s)==0)return food[i].getPrice();return false;}int FoodManager::getListByName(char *s){for(int i = 0 ; i < total ; i ++)if(strcmp(food[i].getName(),s)==0)return i;return false;}int FoodManager::getTotal(){return total;}char * FoodManager::getNameByList(int list){return food[list].getName();}void FoodManager::setTotal(int x){total = x;}FeeManager.h#ifndef FEEMANAGER#define FEEMANAGERclass FeeManager{private:double jiuCanFee; //就餐总收入double waiMaiFee;//外卖总收入public:FeeManager();//构造函数double getWaiMaiFee();//返回外卖总收入double getJiuCanFee();//返回就餐总收入void addWaiMaiFee(double x);//增加外卖总收入void addJiuCanFee(double x);//添加就餐总收入};#endifFeeManager.cpp#include"FeeManager.h"FeeManager::FeeManager(){jiuCanFee = 0;waiMaiFee = 0;}double FeeManager::getWaiMaiFee(){return waiMaiFee;}double FeeManager::getJiuCanFee(){return jiuCanFee;}void FeeManager::addWaiMaiFee(double x){waiMaiFee += x;}void FeeManager::addJiuCanFee(double x){jiuCanFee += x;}MenuView.h#ifndef MENUVIEW#define MENUVIEWclass MenuView{private:char whiteSmile;//白色笑脸char blackSmile;//黑色笑脸char heart;//心形图形public:MenuView();//构造函数void showJiuCan();//显示就餐用户点菜界面void showWaiMai();//显示外卖用户点菜界面int showMain();//显示用户选择就餐还是外卖界面int showViewChoice();//显示进入本系统的功能选择界面int showRegister();//显示客户注册界面int showClientChoice();//显示客户点餐界面int showQuery();//显示查询客户订单界面int showMend();//显示修改食物界面int showMenuChoice();//显示选择菜单界面int showMendDingDan();//显示客户修改订单界面};#endifMenuView.cpp#include"MenuView.h"#include<iostream>using namespace std;MenuView::MenuView(){whiteSmile = 1;blackSmile = 2;heart = 3;}int MenuView::showMain(){for(int i = 1 ; i <= 80 ; i++ )cout<<heart;for(int i = 1;i<=80;i++){if(i==1||i==80)cout<<heart;else cout<<' ';}cout<<heart<<"\t\t\t\t欢迎光临本餐厅";for(int j=1;j<=33;j++)cout<<' ';cout<<heart;for(int i = 1;i<=80;i++){if(i==1||i==80)cout<<heart;else cout<<' ';}for(int i = 1 ; i <= 80 ; i++ )cout<<heart;//输出餐厅的外形cout<<endl<<endl;cout<<"\t我是服务员小坠"<<whiteSmile<<",竭诚为您服务哦"<<blackSmile<<endl<<endl<<endl;for(int k=1;k<=10;k++)cout<<' ';for(int i = 1 ; i <= 20 ; i++ )cout<<heart;cout<<endl;for(int k=1;k<=10;k++)cout<<' ';cout<<heart<<"请选择您需要的服务"<<heart<<endl;for(int k=1;k<=10;k++)cout<<' ';for(int i = 1 ; i <= 60 ; i++ )cout<<heart;cout<<endl;for(int k=1;k<=10;k++)cout<<' ';cout<<heart<<"1:就餐";for(int i = 1 ; i <= 52 ; i ++ )cout<<' ';cout<<heart<<endl;for(int k=1;k<=10;k++)cout<<' ';cout<<heart<<"2:外卖";for(int i = 1 ; i <= 52 ; i ++ )cout<<' ';cout<<heart<<endl;for(int k=1;k<=10;k++)cout<<' ';for(int i = 1 ; i <= 60 ; i++ )cout<<heart;cout<<endl<<endl;for(int i = 1;i<=7;i++)cout<<endl;cout<<"\t\t\t您的选择是:";int choice;cin>>choice;return choice;}void MenuView::showJiuCan(){cout<<"欢迎就餐,本餐厅有以下食品供您品尝"<<whiteSmile<<endl; }int MenuView::showViewChoice(){for(int i = 1 ; i <= 80 ; i++ )cout<<heart;for(int i = 1;i<=80;i++){if(i==1||i==80)cout<<heart;else cout<<' ';}cout<<heart<<"\t\t\t\t陈旺均特色餐厅";for(int j=1;j<=33;j++)cout<<' ';cout<<heart;for(int i = 1;i<=80;i++){if(i==1||i==80)cout<<heart;else cout<<' ';}for(int i = 1 ; i <= 80 ; i++ )cout<<heart;cout<<endl<<endl;cout<<"------------------------"<<endl;cout<<"| 1:顾客服务|"<<endl;cout<<"| 2:食物管理|"<<endl;cout<<"| 3:今日反馈|"<<endl;cout<<"| 4:退出系统|"<<endl;cout<<"------------------------"<<endl;cout<<"您的选择是: ";int next;cin>>next;cout<<endl;return next;}int MenuView::showRegister(){cout<<"------------------------"<<endl;cout<<"| 1:新顾客点餐|"<<endl;cout<<"| 2:查询订单|"<<endl;cout<<"------------------------"<<endl;int next;cin>>next;return next;}int MenuView::showClientChoice(){cout<<"------------------------"<<endl;cout<<"| 1:加菜|"<<endl;cout<<"| 3:提交订单|"<<endl;cout<<"------------------------"<<endl;int next;cin>>next;return next;}int MenuView::showQuery(){cout<<"------------------------"<<endl;cout<<"| 1:查询订单|"<<endl;cout<<"| 2:退出查询|"<<endl;cout<<"------------------------"<<endl;int next;cin>>next;return next;}int MenuView::showMend(){cout<<"------------------------"<<endl;cout<<"| 1:添加食物|"<<endl;cout<<"| 2:修改价格|"<<endl;cout<<"| 3:删除食物|"<<endl;cout<<"| 4:退出食物管理|"<<endl;cout<<"------------------------"<<endl;int next;cin>>next;return next;}int MenuView::showMenuChoice(){cout<<"添加菜单:"<<endl;cout<<"------------------------"<<endl;cout<<"| 1:原菜单|"<<endl;cout<<"| 2:更新后的菜单|"<<endl;cout<<"------------------------"<<endl;int next;cin>>next;return next;}int MenuView::showMendDingDan(){cout<<"------------------------"<<endl;cout<<"| 2:删除食物|"<<endl;cout<<"| 3:退出修改|"<<endl;cout<<"------------------------"<<endl;int next;cin>>next;return next;}Main.cpp#include<iostream>#include"Food.h"#include"MenuView.h"#include"FoodManager.h"#include"ClientManager.h"#include"FeeManager.h"#include<fstream>#include<iomanip>using namespace std;int main(){FeeManager feem;//管理就餐和外卖的费用MenuView mv; //管理界面ClientManager cm; //管理客户的类的对象FoodManager fm;//管理食物的类的对象fstream ioFile; //文件输入输出int clientChoice;// 1 服务顾客2 食物管理3 今日反馈4退出系统int clientService;//1新顾客点餐2 查询int mainChoice;// 1 就餐 2 外卖int curClient; //当前操作的顾客编号int queryChoice;//1 查询订单2 退出查询int foodChoice;//1 添加食物2 修改食物价格3删除某种食物4退出食物管理int viewChoice;//1 顾客服务2 食物管理3 今日反馈4 退出系统int menuChoice;//1 原菜单 2 更新后菜单int mendDingDanChoice;//1 修改数量2 删除食物3 退出修改io("C:\\Users\\Administrator\\Desktop\\food.txt",ios::in);while(!io()){char *s;double p;s=new char[20];ioFile>>s>>p;fm.addFood(s,p);}io();//以上是从文件读入食物的种类和价格信息while(true){viewChoice = mv.showViewChoice();if(viewChoice == 1) //顾客服务{clientService = mv.showRegister();if(clientService == 1)//用户注册{char *name,*phone;name = new char[30];phone = new char[20];cout<<"请完善客户资料"<<endl;cout<<"您的订单号是"<<cm.getClientNum()+1<<endl;curClient = cm.getClientNum();cout<<"请输入姓名: ";cin>>name;cout<<"请输入手机号码: ";cin>>phone;Client cc(name,phone);cc.setNumber(cm.getClientNum());cm.addClient(cc);mainChoice = mv.showMain();if(mainChoice == 1) //就餐{cm.client[curClient].setType(1);mv.showJiuCan();while(true){fm.showFood();int choices;choices = mv.showClientChoice();if(choices==1)//选择食物种类和数量,下单{char *n;int code;n = new char[20];int nums;double prices ;cout<<"请输入食物编号: ";cin>>code;n = fm.food[code].getName();cout<<"您选择了食物:"<<n<<endl;prices = fm.getPriceByName(n);cout<<"请输入数量: ";cin>>nums;cm.client[curClient].client_fm.addFood(Food(n,prices));cm.client[curClient].addFee(nums*prices);cm.client[curClient].num[cm.client[curClient].client_fm.getTotal()-1]=nums;cout<<"当前的总费用是"<<cm.client[curClient].getFee()<<endl<<endl;}else if(choices == 2)//修改订单{cout<<"您当前的订单是:"<<endl;while(true){cm.client[curClient].showClientMessage();mendDingDanChoice = mv.showMendDingDan();if(mendDingDanChoice==1){cm.client[curClient].changNum();}else if(mendDingDanChoice == 2){cm.client[curClient].deleFood();}else if(mendDingDanChoice == 3)break;}}else if(choices == 3)//提交订单break;}feem.addJiuCanFee(cm.client[curClient].getFee());//增加就餐总费用cout<<"您的订单是:"<<endl;cm.client[curClient].showClientMessage();//输出当前订单}else if(mainChoice == 2) //外卖{cm.client[curClient].setType(2);mv.showJiuCan();while(true){fm.showFood();int choices;choices = mv.showClientChoice();if(choices==1)//选择食物种类和数量,下单{char *n;int code;n = new char[20];int nums;double prices ;cout<<"请输入食物编号: ";cin>>code;n = fm.food[code].getName();cout<<"您选择了食物:"<<n<<endl;prices = fm.getPriceByName(n);cout<<"请输入数量: ";cin>>nums;cm.client[curClient].client_fm.addFood(Food(n,prices));cm.client[curClient].addFee(nums*prices);cm.client[curClient].num[cm.client[curClient].client_fm.getTotal()-1]=nums;cout<<"当前的总费用是"<<cm.client[curClient].getFee()<<endl<<endl;}else if(choices == 2)//修改订单{cout<<"您当前的订单是:"<<endl;while(true){cm.client[curClient].showClientMessage();mendDingDanChoice = mv.showMendDingDan();if(mendDingDanChoice==1){cm.client[curClient].changNum();}else if(mendDingDanChoice == 2){cm.client[curClient].deleFood();}else if(mendDingDanChoice == 3)break;}}else if(choices == 3)//提交订单break;}feem.addWaiMaiFee(cm.client[curClient].getFee());//增加就餐总费用cout<<"您的订单是:"<<endl;cm.client[curClient].showClientMessage();//输出当前订单}}//用户注册else if(clientService == 2){while(true){queryChoice = mv.showQuery();if(queryChoice == 1) //查询客户订单信息{cout<<"请输入您的姓名:"<<endl;char *s;s = new char[20];cin>>s;cm.showQuery(s);}else if(queryChoice == 2)break; //退出查询}}}else if(viewChoice == 2) //食物管理{while(true){fm.showFood();foodChoice = mv.showMend();if(foodChoice == 1) //添加食物{char *n;double p;n = new char[20];cout<<"请输入食物名称:";cin>>n;cout<<"请输入食物价格:";cin>>p;fm.addFood(Food(n,p));}else if(foodChoice == 2)//修改食物价格{fm.changePrice();}else if(foodChoice == 3)//删除食物{fm.deleteFood();}else if(foodChoice == 4)break; //退出食物管理}}else if(viewChoice == 3)//今日反馈{cout<<"今天共有"<<cm.getClientNum()<<"人消费,销售总额共"<<feem.getJiuCanFee()+feem.getWaiMaiFee()<<"元"<<endl;cout<<"其中就餐总额为"<<feem.getJiuCanFee()<<"元,";cout<<"外卖总额为"<<feem.getWaiMaiFee()<<"元"<<endl<<endl;cout<<"--------------------------------------"<<endl;cm.showAll();}else if(viewChoice == 4)//退出系统{//io("D:\\food.txt",ios::out);io("C:\\Users\\Administrator\\Desktop\\food.txt",ios::out);for(int i = 0 ; i < fm.getTotal() ; i ++){ioFile<<setw(10)<<setiosflags(ios::left)<<fm.food[i].getName()<<"\t"<<fm.food[i].getPrice( );if(i != (fm.getTotal()-1))ioFile<<endl;}io();cout<<"成功退出系统"<<endl<<endl<<endl;system("pause");return 0;}}}六:运行结果与分析从food.txt文件导入菜单,food.txt内容如下瓜丝儿10 山鸡丁儿12 拌海蜇19 龙须菜46炝冬笋84 玉兰片27 浇鸳鸯115 烧鱼头26烧槟子34 烧百合31 炸豆腐26 炸面筋4拔丝山药10 糖焖莲子18 酿山药88 山东煎饼3草泥马100 金华火腿99运行程序,进入主界面:选择:1 顾客服务再选择1新顾客点餐登记客户信息姓名:陈小坠电话接下来弹出客户服务选择界面进入服务选择界面,选择1就餐弹出菜单,有食物编号,名称,价格选择1加菜选择食物编号0 瓜丝儿,数量3同理,再添加食物金华火腿*1 山鸡丁儿*4 山东煎饼*5选择2,修改订单选择1,修改食物数量选择瓜丝儿,数量修改为1选择2 删除食物山东煎饼修改后的订单如上,选3退出修改,再选3提交订单,此时输出客户订单信息,并返回主界面,如下所示同理,继续添加客户如下李小龙就餐草泥马*1 炸豆腐*2陆小凤外卖山东煎饼*3 酿山药*4 玉兰片*3刘备外卖糖焖莲子*2猪八戒就餐龙须菜*3 浇鸳鸯*4 烧鱼头*5 拌海蜇*4现在点1,顾客服务2查询订单,查询猪八戒的订单订单排在第3,这里就餐和外卖进行了排序,就餐在外卖前面,就餐和外卖则按照先后顺利排列选择2:退出订单查询选择2,食物管理添加食物冰糖葫芦价格46,结果如下多出了第18道菜,冰糖葫芦选择2,修改食物价格,把浇鸳鸯的价格改为1先择3 删除食物草泥马改变后的菜单列表如下选择4,退出食物管理进入主菜单选择3 今日反馈显示今日的销售额,显示所有订单详情,按照就餐和外卖先后顺序排序选择4 退出系统七:心得与体会通过设计这个自主点餐系统,我对C++面向对面的思想和对于类的运用有了更深的理解。