torrero

导航

对“点餐系统”的阅读、分析与运行

该博文为软件工程综合实践专题的第一次作业,借用室友在面向对象程序设计的小学期大作业:点餐系统,并对其代码进行阅读、运行和分析。

一、阅读并分析代码

//代码运用到了文件的调用,将菜单txt显示出来。包含了三个类,两个空类型的函数和一个主函数。

//以下是我对代码的一些分析:

#include<iostream>

#include<fstream>

#include<cassert>

#include<string>

#include<time.h>

using namespace std;

int a;      

static int price=0;

static int flag=0;

class Order{                                                            //点餐类

public:

       int number;

       string name;

public:

       Order(){

              number=1;

              name=" ";

       }

       void print1(){

       cout<<"请输入菜品的编号:"<<endl;

       }

       int getnumber(){

       int number1;

       cin>>number1;                                            //用户输入菜品编号

       number=number1;

       switch(number){

              case 0:

                     {cout<<"结束点餐!"<<endl;}break;

              case 1:

                     {cout<<"德芙巧克力   RMB10"<<endl;

cout<<endl; price=price+10;}

break;

              case 2:

                     {cout<<"香草味八喜   RMB15"<<endl;

cout<<endl; price=price+15;}

break;

              case 3:

                     {cout<<"可可布朗尼   RMB35"<<endl;

cout<<endl; price=price+35;}

break;

              case 4:

                     {cout<<"榴莲菠萝蜜   RMB38"<<endl;

cout<<endl; price=price+38;}

break;

              case 5:

                     {cout<<"芝士玉米粒   RMB15"<<endl;

cout<<endl; price=price+15;}

break;

              case 6:

                     {cout<<"鸡汁土豆泥   RMB10"<<endl;

cout<<endl; price=price+10;}

break;

              case 7:

                     {cout<<"奥尔良烤翅   RMB12"<<endl;

cout<<endl; price=price+12;}

break;

              case 8:

                     {cout<<"黑椒牛里脊套餐    RMB45"<<endl;

cout<<endl; price=price+45;}

break;

              case 9:

                     {cout<<"黄焖辣子鸡套餐    RMB38"<<endl;

cout<<endl; price=price+38;}

break;

              case 10:

                     {cout<<"红烧排骨套餐       RMB40"<<endl;

cout<<endl; price=price+40;}

break;

              case 11:

                     {cout<<"酱醋鱼套餐         RMB58"<<endl;

cout<<endl; price=price+58;}

break;

              case 12:

                     {cout<<"雪碧     RMB5"<<endl;

cout<<endl; price=price+5;}

break;

              case 13:

                     {cout<<"可乐     RMB5"<<endl;

cout<<endl; price=price+5;}

break;

              case 14:

                     {cout<<"奶茶     RMB15"<<endl;

cout<<endl; price=price+15;}

break;

              case 15:

                     {cout<<"橙汁     RMB20"<<endl;

cout<<endl; price=price+20;}

break;

              case 16:

                     {cout<<"椰奶     RMB5"<<endl;

cout<<endl; price=price+5;}

break;

              case 17:

                     {cout<<"西瓜汁   RMB15"<<endl;

cout<<endl; price=price+15;}

break;

              default:

                  {cout<<"没有该编号对应的菜品!"<<endl;cout<<endl; }

break;

       }

       }

       };

Order d[17];

 

class Confirm{                                                                 //订单确认类

       public:

       void print2(){                           

              cout<<"确定点菜结束吗?(Yes/No)"<<endl;     //若想停止点菜则输入Yes

              string str;

              cin>>str;

              int s;

              if(str=="Yes") {

              flag=1;

              cout<<"请输入就餐人数:"<<endl;

              cin>>s;

              cout<<" "<<endl;

              cout<<"共需支付:"<<price<<endl;             //结算订单

              }

              else if(str=="No"){                                      //若想继续点菜则输入No,继续点菜

                  flag=2;

                     int getnumber2;

                     int k;

                     for(k=0; ;k++){

                     cout<<"------------"<<endl;

                     cout<<"请继续点菜!"<<endl;

                     cout<<"请输入菜品的编号:"<<endl;

                     cin>>getnumber2;

                     if(getnumber2==0) break;

               switch(getnumber2){

        case 1:{cout<<"德芙巧克力   RMB10"<<endl;

cout<<endl;

price=price+10;}

break;

              case 2:{cout<<"香草味八喜   RMB15"<<endl;

cout<<endl;

 price=price+15;}

break;

              case 3:{cout<<"可可布朗尼   RMB35"<<endl;

cout<<endl;

 price=price+35;}

break;

              case 4:{cout<<"榴莲菠萝蜜   RMB38"<<endl;

cout<<endl;

 price=price+38;}

break;

              case 5:{cout<<"芝士玉米粒   RMB15"<<endl;

cout<<endl;

price=price+15;}

break;

              case 6:{cout<<"鸡汁土豆泥   RMB10"<<endl;

cout<<endl;

price=price+10;}

break;

              case 7:{cout<<"奥尔良烤翅   RMB12"<<endl;

cout<<endl;

price=price+12;}

break;

              case 8:{cout<<"黑椒牛里脊套餐    RMB45"<<endl;

cout<<endl;

price=price+45;}

break;

              case 9:{cout<<"黄焖辣子鸡套餐    RMB38"<<endl;

cout<<endl;

price=price+38;}

break;

              case 10:{cout<<"红烧排骨套餐       RMB40"<<endl;

cout<<endl;

price=price+40;}break;

              case 11:{cout<<"酱醋鱼套餐         RMB58"<<endl;

cout<<endl;

price=price+58;}

break;

              case 12:{cout<<"雪碧     RMB5"<<endl;

cout<<endl;

price=price+5;}

break;

              case 13:{cout<<"可乐     RMB5"<<endl;

cout<<endl;

price=price+5;}

break;

              case 14:{cout<<"奶茶     RMB15"<<endl;

cout<<endl;

price=price+15;}

break;

              case 15:{cout<<"橙汁     RMB20"<<endl;

cout<<endl;

 price=price+20;}

break;

              case 16:{cout<<"椰奶     RMB5"<<endl;

cout<<endl;

 price=price+5;}

break;

              case 17:{cout<<"西瓜汁   RMB15"<<endl;

cout<<endl;

price=price+15;}

break;

              default:

                  {cout<<"没有该编号对应的菜品!"<<endl;

cout<<endl; }

break;

                }

}

}

}

};

class Order_number{                                                //随机生成订单号给用户

       public:

       int n;

       public:

       int printf(){

       srand((unsigned)time(NULL));

       n=5000+rand()%5000;

       cout<<"订单号为:"<<n<<endl;

       cout<<"完成订单!"<<endl;

       }

};

 

 

void showform(){                                                             //显示窗体

       cout<<"    Welcome to use the ordering system     "<<endl;

       cout<<"--------------------------------------------"<<endl;

       cout<<"           1.显示菜单                   "<<endl;

       cout<<"           2.点餐                       "<<endl;

       cout<<"           3.订单确认                   "<<endl;

       cout<<"           4.显示订单号                 "<<endl;

       cout<<"           5.完成订单                   "<<endl;

       cout<<"           6.退出                       "<<endl;

       cout<<"--------------------------------------------"<<endl;

       cout<<"Press the number you want to choose:        "<<endl;

}

void readtxt(string file){                                               //读取文件

       ifstream infile;

       infile.open(file.data());

       assert(infile.is_open());                                        //测试一个条件并可能使程序终止

       string s;

       while(getline(infile,s)){

              cout<<s<<endl;

       }

       infile.close();

}  

 

int main() {                                                             //主函数

       showform();

       int i;

       Order o;

       Order_number nu;

       Confirm c;

       while(a!=6){

              if(flag==1)

              {

                     a=4;

                     flag=0;

              }

              else if(flag==2)

              {

                     a=2;

                     flag=0;

              }

              else

                     cin>>a;

       switch(a){                                                            //用switch语句来实现目录的选择

              case 1:

                     {

                     ifstream fin("menu.txt",ios::in);             //显示菜品菜单

                     string file="menu.txt";

               readtxt(file);

                     int i;

                     for(i=0;i<17;i++){

                     fin>>d[i].number>>d[i].name;

                     }     

                     fin.close();

                     }

              break;

        case 2:

                     {while(o.number>=1){                        //进入点菜功能

                     o.print1();

                     o.getnumber();

                     }}

                     break;

              case 3:{                                                    //确认订单

                  c.print2();}

                     break;

              case 4:{                                                   //查看订单号

                     nu.printf();}

                     break;

           case 6:                                                      //退出系统

                     fflush(stdin);

                     break;

       }

       }

       return 0;

}

 

三、心得体会与问题分析

问题1:

该系统没有设置返回上一级的功能按钮,只能通过拉动来看。

解决方案:

在每一个目录选项的case中都加入返回目录的语句。

问题2:

在点餐时,只能选择餐品的编号,并不能选择餐品的数量,只能通过重复点单来叠加,十分不方便。

解决方案:

在点餐中加入一个变量来存储数量,由用户输入,在结算总价时调用这个数量。

问题3:

点餐时没有任何关于如何停止点餐的方法,全靠自己推测来发现输入0则结束点餐。

解决方案:输出“若想结束点餐请输入0”即可。

问题4:

确认订单时看到的显示的订单号是随机的,并没有任何意义。

解决方案:

可以引入用户登录功能,每个用户对应一个订单号,在确认订单时调用点餐结束时给的订单号。

 

心得体会:

第一次完整的看完别人写的程序,然后以审视的眼光来观察对方的不足,从用户的需求角度来思考该如何改进。也学习了对方所写的程序中的优点,为以后写类似的代码做准备。也能提示自己写类似代码时不要犯相同的失误。总之这是一次特别的经历!

 

posted on 2019-03-05 20:54  torrero  阅读(528)  评论(0编辑  收藏  举报