1.导言
通过设计一个小型的自助点餐系统,训练综合运用所学知识处理实际问题的能力,强化面向对象的程序设计理念,使自己的程序设计与调试水平有一个明显的提高:某餐厅需要设计一个自助点餐系统,方便顾客点餐的同时也方
便管理员对餐厅的菜单和订单进行管理同时并对当日的餐厅
销售情况进行统计。
2.内容简介
现针对题目要求,我们需要分两个方向进行程序设计,一是针
对顾客,在设计顾客程序的时候,我们首先要正确打印出题目
所要求的菜单,先询问顾客的消费方式,是在店消费还是点外
卖,然后询问顾客要点的菜品编号及数量,最后根据顾客的点
单情况,正确打印出订单并保存在 txt 文件里。
针对管理员,在进入管理员界面之前,用户需要输入正确的账
号和密码,而正确的账号和密码保存在 txt 文件中,正确进入
管理员系统界面后,可以选择对订单的管理,对菜单的管理和
对销售情况的统计。在对订单管理的界面里可以查找并对其进
行修改和删除某一天的订单,在菜单管理界面,可以对菜单进
行查询,修改,增加以及删除功能。
3.系统结构图
通过系统结构图我们可以很明确自己接下来的方向和类的设计
4.类的设计及UML图
5.代码内容及其注释
Control.h
#ifndef CONTROL_H
#define CONTROL_H
#include "InsiderOrder.h"
#include "TakeOutOrder.h"
#include "Date.h"
#include <iostream>
#include"Order.h"
#include"Menu.h"
#include <ctime> //时间头文件
#pragma warning( disable : 4996 )
using namespace std;
void Control()
{
int a = 0;
int b = 0;
Menu* m;
m = new Menu;
m->MENU();
int op;
time_t timer;
time(&timer);
tm* t_tm = localtime(&timer);
Date* d;
d = new Date(t_tm->tm_year + 1900, t_tm->tm_mon + 1, t_tm->tm_mday);
cout << "今天是" << t_tm->tm_year + 1900 << "年" << t_tm->tm_mon + 1 << "月" << t_tm->tm_mday << "日" << endl;
cout << "------------------------------------------------" << endl;
cout << "欢迎使用自助点餐系统!" << endl;
cout << "------------------------------------------------" << endl;
cout << "输入1:进入顾客系统" << endl;
cout << "输入2:进入管理员系统" << endl;
cout << "输入0:退出系统" << endl;
cout << "请输入:" << endl;
cin >> op;
while (op != 0)
{
while (!(op == 1 || op == 2))
{
cout << "您输入的指令不存在!请重新输入" << endl;
cout << "------------------------------------------------" << endl;
cout << "输入1:进入顾客系统" << endl;
cout << "输入2:进入管理员系统" << endl;
cout << "输入0:退出系统" << endl;
cout << "请输入:" << endl;
cin >> op;
}
if (op == 1)
{
cout << "欢迎进入自助点餐系统!" << endl << endl;
cout << "------------------------------------------------" << endl;
m->print();
cout << "------------------------------------------------" << endl << endl;
cout << "以上为全部菜单,在店消费请输入1,外卖服务请输入2,退出系统输入3" << endl;
int op_;
cin >> op_;
while (!(op_ == 1 || op_ == 2 || op_ == 3))
{
cout << "您输入的指令不存在!请重新输入" << endl;
cout << "------------------------------------------------" << endl;
cout << "在店消费请输入1,外卖服务请输入2,退出系统输入3" << endl;
cin >> op_;
}
while (op_ != 3)
{
if (op_ == 1)
{
a++;
InsiderOrder* in;
in = new InsiderOrder(d);
in->order();
}
else if (op_ == 2)
{
b++;
TakeOutOrder* out;
out = new TakeOutOrder(d);
out->order();
}
cout << "在店消费请输入1,外卖服务请输入2,退出系统输入3" << endl;
cin >> op_;
}
cout << "------------------------------------------------" << endl;
cout << "输入1:进入顾客系统" << endl;
cout << "输入2:进入管理员系统" << endl;
cout << "输入0:退出系统" << endl;
cout << "请输入:" << endl;
}
else if (op == 2)
{
string account;
bool power = 0;
cout << "请输入您的管理员账号:(退出请输入0)" << endl;
cin >> account;
while (account != "0")
{
string password;
cout << "请输入您的管理员密码:" << endl;
cin >> password;
ifstream admin;
admin.open("admin.txt");
{
string admin_account, admin_password;
admin >> admin_account >> admin_password;
if (admin_account == account && admin_password == password)
{
power = 1;
cout << "成功登入管理员系统!" << endl;
break;
}
else
{
cout << "您的管理员账号或者密码错误!请重新输入" << endl;
cout << "退出请输入0" << endl;
cout << "请输入您的管理员账号:" << endl;
}
}
admin.close();
cin >> account;
}
if (power)
{
bool flag = 1;
cout << "---------------------------------------------------------------------------" << endl;
cout << "您需要对订单进行操作请输入1,对餐厅菜品管理请输入2,统计某天销售记录请输入3" << endl;
cout << "退出请输入0" << endl;
int op;
cin >> op;
while (op != 0)
{
if (!(op == 1 || op == 2 || op == 3 || op == 0))
{
cout << "您输入的指令不存在!请重新输入" << endl;
cout << "---------------------------------------------------------------------------" << endl;
cout << "需要对订单进行操作请输入1,对餐厅菜品管理请输入2,统计某天销售记录请输入3" << endl;
cout << "退出请输入0" << endl;
}
else if (op == 1)
{
int year, month, day;
char c;
cout << "请输入你要查询的日期,输入格式为:2020/6/18" << endl;
cin >> year >> c >> month >> c >> day;
Date* date = new Date(year, month, day);
search(date);
}
else if (op == 2)
{
if (flag)
{
m->print();
flag = 0;
}
cout << "请输入指令:(添加菜品:1 修改菜品:2 删除菜品:3 查找菜品:4 修改折扣:5 退出:0)" << endl;
int op_;
cin >> op_;
while (op_ != 0)
{
if (op_ != 1 && op_ != 2 && op_ != 3 && op_ != 4&&op_!=5)
{
cout << "指令输入错误,请重新输入" << endl;
break;
}
if (op_ == 1)
m->add();
else if (op_ == 2)
m->modify();
else if (op_ == 3)
m->sub();
else if (op_ == 4)
m->search();
else if (op_ == 5)
{
double x;
cout << "请输入您要设置的折扣:(例如:输入95为95折)" << endl;
cin >> x;
m->setdiscount(x);
}
m->MENU();
m->print();
cout << "请输入指令:(添加菜品:1 修改菜品:2 删除菜品:3 查找菜品:4 修改折扣:5 退出:0)" << endl;
cin >> op_;
}
}
else if (op == 3)
{
int year, month, day;
char c;
cout << "请输入你要查询的日期,输入格式为:2020/6/18" << endl;
cin >> year >> c >> month >> c >> day;
Date* date = new Date(year, month, day);
statistics(a, b);
}
cout << "需要对订单进行操作请输入1,对餐厅菜品管理请输入2,统计某天销售记录请输入3" << endl;
cout << "退出请输入0" << endl;
cin >> op;
}
}
cout << "------------------------------------------------" << endl;
cout << "输入1:进入顾客系统" << endl;
cout << "输入2:进入管理员系统" << endl;
cout << "输入0:退出系统" << endl;
cout << "请输入:" << endl;
}
cin >> op;
}
}
#endif
Control.h主要负责系统界面的管理,包括顾客点餐和管理员的管理
Date.h
#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
Date(int Year, int Month, int Day);
int get_year() { return year; }
int get_month() { return month; }
int get_day() { return day; }
};
Date::Date(int Year, int Month, int Day)
{
year = Year;
month = Month;
day = Day;
}
#endif
Date.h主要负责返回系统时间
Order.h
#ifndef ORDER_H
#define ORDER_H
#include "Date.h"
#include"Control.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <ctime> //时间头文件
#pragma warning( disable : 4996 )
static double total = 0;
using namespace std;
class Order
{
private:
public:
virtual void order() = 0;
};
string itos(int i) // 将int 转换成string
{
stringstream s;
s << i;
return s.str();
}
void statistics(int a,int b)
{
int i = a;
int o = b;
time_t timer;
time(&timer);
tm* t_tm = localtime(&timer);
Date* d;
d = new Date(t_tm->tm_year + 1900, t_tm->tm_mon + 1, t_tm->tm_mday);
cout << "今天是" << t_tm->tm_year + 1900 << "年" << t_tm->tm_mon + 1 << "月" << t_tm->tm_mday << "日" << endl;
cout << "今日在店消费与外卖比例为:" <<i<<":"<<o<< endl;
cout << "今日的总销售额为:" << total<<"元"<<endl;
}
void search(Date* date)
{
int year, month, day;
year = date->get_year();
month = date->get_month();
day = date->get_day();
string str = itos(year) + '-' + itos(month) + '-' + itos(day) + ".txt";
ifstream s;
s.open(str);
string t[500];
int l = 0;
int h = 0;
string j[50];
int high[50] = { 0 };
while (getline(s, t[l]))
{
cout << t[l] << endl;
if (t[l].length() == 4)
{
high[h] = l;
j[h++] = t[l];
}
l++;
}
s.close();
cout << "请输入指令:(删除输入1,退出输入0)" << endl;
int op;
cin >> op;
while (op != 0)
{
while (op != 1)
{
cout << "输入错误,请重新输入" << endl;
cout << "请输入指令:(删除输入1,退出输入0)" << endl;
cin >> op;
}
if (op == 1)
{
cout << "请输入您想删除的订单编号" << endl;
string op_;
cin >> op_;
int flag = -1;
for (int i = 0; i < h; i++)
if (op_ == j[i])
{
flag = i;
break;
}
if (flag == -1)
{
cout << "您输入的订单号不存在" << endl;
cout << "请输入指令:(删除输入1,退出输入0)" << endl;
cin >> op;
continue;
}
else
{
if (high[flag + 1] != 0)
for (int k = high[flag]; k < high[flag + 1]; k++)
t[k] = "0";
else
for (int k = high[flag]; k < l; k++)
t[k] = "0";
ofstream s;
s.open(str);
for (int p = 0; p < l; p++)
{
if (t[p] != "0")
s << t[p] << endl;
}
s.close();
}
cout << "请输入指令:(删除输入1,退出输入0)" << endl;
cin >> op;
}
}
}
#endif
Order.h主要是对订单的管理以及其它功能的实现,比如将订单输出到txt文档里并以当前系统时间命名,因为获得的系统年月日是int类型,就需要将它转换成为string类型就用到了itos函数
TakeOutOrder.h
#ifndef TAKEOUTORDER_H
#define TAKEOUTORDER_H
#include "Menu.h"
#include "Order.h"
using namespace std;
int Inid;
static int outid = 2000;
class TakeOutOrder : public Order
{
private:
int id[MAXSIZE] = { 0 };
string name[MAXSIZE];
double price[MAXSIZE];
int count[MAXSIZE];
int foodid[MAXSIZE];
double totalprice = 0;
int cnt = 0;
string time;
string phonenumber;
string where;
int year;
int month;
int day;
public:
TakeOutOrder(Date* d);
void order();
void print();
};
TakeOutOrder::TakeOutOrder(Date* d)
{
outid++;
year = d->get_year();
month = d->get_month();
day = d->get_day();
ifstream outside;
outside.open("orderid.txt");
int x, y, z, n;
outside >> x >> y >> z >> Inid >> n;;
if (x == year && y == month && z == day)
outid = n + 1;
else
Inid = 1000;
outside.close();
}
void TakeOutOrder::order()
{
ofstream outside;
outside.open("orderid.txt");
outside << year << '\n' << month << '\n' << day << '\n' << Inid << '\n' << outid;
outside.close();
Menu* m;
m = new Menu;
m->MENU();
int Foodid;
cout << "请输入送餐时间:" << endl;
cin >> time;
cout << "请输入送餐地点:" << endl;
cin >> where;
cout << "请输入您的手机号:" << endl;
cin >> phonenumber;
cout << "请输入您想点的菜品编号,输入0退出" << endl;
cin >> Foodid;
while (Foodid != 0)
{
cout << "请输入您想点取的数量" << endl;
cin >> count[cnt];
id[cnt] = m->id[Foodid - 1];
name[cnt] = m->name[Foodid - 1];
if (discount <= 10)
price[cnt] = m->price[Foodid - 1] * discount / 10;
if (discount > 10)
price[cnt] = m->price[Foodid - 1] * discount / 100;
totalprice += price[cnt] * count[cnt];
foodid[cnt++] = Foodid;
cout << "请输入您想点的菜品编号,输入0退出" << endl;
cin >> Foodid;
}
print();
}
void TakeOutOrder::print()
{
cout << "------------------------------------------------" << endl;
cout << "您的订单号为:" << outid << endl;
cout << "您的消费方式为:外卖服务" << endl;
cout << "您的送餐时间为:" << time << endl;
cout << "您的送餐地点为:" << where << endl;
cout << "您的手机号码为:" << phonenumber << endl;
cout << "您的外卖费用为:" << "2元" << endl;
cout << "本店今日折扣为:" << discount << "折" << endl;
cout << "您的订单如下:" << endl;
cout << "Id\t" << "菜品名称\t" << "菜品价格\t" << "菜品份数\t" << "合计" << endl;
for (int i = 0; i < cnt; i++)
{
if (name[i].length() == 8)
cout << foodid[i] << '\t' << name[i] << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
else
cout << foodid[i] << '\t' << name[i] << '\t' << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
}
cout << "您的总消费为:" << totalprice + 2 << "元" << endl;
cout << "------------------------------------------------" << endl;
string str = itos(year) + '-' + itos(month) + '-' + itos(day) + ".txt";
ofstream out(str, ios::app);
out << outid << endl;
out << "id\t" << "菜品名称\t" << "菜品价格\t" << "菜品份数\t" << "合计" << endl;
for (int i = 0; i < cnt; i++)
{
if (name[i].length() == 8)
out << foodid[i] << '\t' << name[i] << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
else
out << foodid[i] << '\t' << name[i] << '\t' << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
}
out << "总消费为:" << totalprice + 2 << "元" << endl;
total += totalprice + 2;
out.close();
}
#endif
TakeOutOrder.h主要负责管理外卖订单的管理
InsiderOrder.h
#ifndef INSIDEORDER_H
#define INSIDEORDER_H
#include "Menu.h"
#include "Order.h"
using namespace std;
static int inid = 1000;
static int tablenumber = 100;
int Outid;
class InsiderOrder : public Order
{
private:
int id[MAXSIZE] = { 0 };
string name[MAXSIZE];
double price[MAXSIZE];
int count[MAXSIZE];
int foodid[MAXSIZE];
double totalprice = 0;
int cnt = 0;
int year;
int month;
int day;
public:
InsiderOrder(Date* d);
void order();
void print();
};
InsiderOrder::InsiderOrder(Date* d)
{
inid++;
year = d->get_year();
month = d->get_month();
day = d->get_day();
ifstream inside;
inside.open("orderid.txt");
int x, y, z, m;
inside >> x >> y >> z >> m >> Outid;
if (x == year && y == month && z == day)
inid = m + 1;
else
Outid = 2000;
inside.close();
}
void InsiderOrder::order()
{
ofstream inside;
inside.open("orderid.txt");
inside << year << '\n' << month << '\n' << day << '\n' << inid << '\n' << Outid;
inside.close();
Menu* m;
m = new Menu;
m->MENU();
int Foodid;
cout << "请输入您想点的菜品编号,输入0退出" << endl;
cin >> Foodid;
while (Foodid != 0)
{
cout << "请输入您想点取的数量" << endl;
cin >> count[cnt];
id[cnt] = m->id[Foodid - 1];
name[cnt] = m->name[Foodid - 1];
if(discount<=10)
price[cnt] = m->price[Foodid - 1] * discount / 10;
if (discount > 10)
price[cnt] = m->price[Foodid - 1] * discount / 100;
totalprice += price[cnt] * count[cnt];
foodid[cnt++] = Foodid;
cout << "请输入您想点的菜品编号,输入0退出" << endl;
cin >> Foodid;
}
print();
}
void InsiderOrder::print()
{
cout << "您的订单号为:" << inid << endl;
cout << "您的消费方式为:在店消费" << endl;
cout << "您的餐桌号为:" << ++tablenumber << endl;
cout << "您的餐桌费为:" << "2元" << endl;
cout << "本店今日折扣为:" << discount << "折" << endl;
cout << "您的订单如下:" << endl;
cout << "id\t" << "菜品名称\t" << "菜品价格\t" << "菜品份数\t" << "合计" << endl;
for (int i = 0; i < cnt; i++)
{
if (name[i].length() == 8)
cout << foodid[i] << '\t' << name[i] << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
else
cout << foodid[i] << '\t' << name[i] << '\t' << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
}
cout << "您的总消费为:" << totalprice + 2 << "元" << endl;
string str = itos(year) + '-' + itos(month) + '-' + itos(day) + ".txt";
ofstream out(str, ios::app);
out << inid << endl;
out << "id\t" << "菜品名称\t" << "菜品价格\t" << "菜品份数\t" << "合计" << endl;
for (int i = 0; i < cnt; i++)
{
if (name[i].length() == 8)
out << foodid[i] << '\t' << name[i] << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
else
out << foodid[i] << '\t' << name[i] << '\t' << '\t' << price[i] << '\t' << '\t' << count[i] << '\t' << '\t' << price[i] * count[i] << endl;
}
out << "总消费为:" << totalprice + 2 << "元" << endl;
total += totalprice + 2;
out.close();
}
#endif
InsiderOrder.h主要是负责管理店内消费
Menu.h
#ifndef MENU_H
#define MENU_H
#define MAXSIZE 50
#include <iostream>
#include <fstream>
#include <string>
using namespace std;//
double discount = 10;
class Menu
{
private:
public:
int count = 0;
int id[MAXSIZE] = { 0 };
string name[MAXSIZE];
double price[MAXSIZE];
void MENU();
void print();
void add();
void modify();
void sub();
void search();
void setdiscount(double b);
};
void Menu::MENU()
{
count = 0;
ifstream menu;
menu.open("menu.txt");
while (menu >> id[count] >> name[count] >> price[count])
count++;
menu.close();
}
void Menu::print()
{
cout << "菜号" << '\t' << "菜名" << '\t' << '\t' << "价格" << endl;
for (int i = 0; i < count; i++)
{
if (name[i].length() == 8)
cout << id[i] << '\t' << name[i] << '\t' << price[i] << endl;
else
cout << id[i] << '\t' << name[i] << '\t' << '\t' << price[i] << endl;
}
}
void Menu::add()
{
count++;
ofstream menu;
menu.open("menu.txt", ios::app);
cout << "请依次输入菜品的名字和价格:" << endl;
cin >> name[count] >> price[count];
menu << endl;
menu << count << '\t' << name[count] << '\t' << price[count];
menu.close();
}
void Menu::modify()
{
ofstream menu;
menu.open("menu.txt");
int num;
cout << "请输入想要修改的菜品的编号:(输入0退出)" << endl;
cin >> num;
while (num != 0)
{
if (num < 0 || num > count)
cout << "您所输入的菜品编号不存在" << endl;
else
{
cout << "请输入您想要修改的菜名:(如不修改菜名输入0)" << endl;
string str;
cin >> str;
if (str != "0")
{
name[num - 1] = "";
name[num - 1] = str;
}
cout << "请输入您想要修改的价格:(如不修改价格输入0)" << endl;
int p;
cin >> p;
if (p != 0)
price[num - 1] = p;
}
cout << "请输入想要修改的菜品的编号:(输入0退出)" << endl;
cin >> num;
}
for (int i = 0; i < count; i++)
{
if (name[i].length() == 8)
menu << id[i] << '\t' << name[i] << '\t' << price[i] << endl;
else
menu << id[i] << '\t' << name[i] << '\t' << '\t' << price[i] << endl;
}
menu.close();
}
void Menu::sub()
{
ofstream menu;
menu.open("menu.txt");
int num;
cout << "请输入想要删除的菜品的编号:(输入0退出)" << endl;
cin >> num;
while (num != 0)
{
if (num < 0 || num > count)
cout << "您所输入的菜品编号不存在" << endl;
else
{
count--;
for (int i = num - 1; i < count; i++)
{
id[i] = id[i + 1];
name[i] = name[i + 1];
price[i] = price[i + 1];
}
}
cout << "请输入想要删除的菜品的编号:(输入0退出)" << endl;
cin >> num;
}
for (int i = 0; i < count; i++)
{
if (name[i].length() == 8)
menu << id[i] << '\t' << name[i] << '\t' << price[i] << endl;
else
menu << id[i] << '\t' << name[i] << '\t' << '\t' << price[i] << endl;
}
menu.close();
}
void Menu::search()
{
ifstream menu;
menu.open("menu.txt");
int num;
cout << "请输入想要查找的菜品的编号:(输入0退出)" << endl;
cin >> num;
while (num != 0)
{
if (num < 0 || num > count)
cout << "您所输入的菜品编号不存在" << endl;
else
{
cout << "菜号" << '\t' << "菜名" << '\t' << '\t' << "价格" << endl;
if (name[num-1].length() == 8)
cout << id[num - 1] << '\t' << name[num - 1] << '\t' << price[num - 1] << endl;
else
cout << id[num - 1] << '\t' << name[num - 1] << '\t' << '\t' << price[num - 1] << endl;
}
cout << "请输入想要查找的菜品的编号:(输入0退出)" << endl;
cin >> num;
}
menu.close();
}
void Menu::setdiscount(double a)
{
discount = a;
}
#endif
Menu.h主要作用是读取txt文档里面的菜单并输出,同时对菜单进行查找修改删除等功能的实现
Main.cpp
#include "Menu.h"
#include "Control.h"
int main()
{
Control();
return 0;
}
5.运行结果及其分析
上图为进入顾客系统后的界面
上图为选择在店消费并点餐后的界面,可以看出很准确地打印出了订单
上面两张图为选择外卖的界面,正确打印出订单,内容包括送餐时间,送餐地点和手机号码以及外卖菜品内容
上图为点餐后的订单txt文档并以当前系统时间命名
上图为进入管理员界面,账户名密码默认设置为123、123,如果需要改动可以在下图文本文档进行修改
上图为进行菜品添加后的界面,同时下图为添加菜品后的txt文档
注:由于笔者的手残在麻辣香锅前面加了l,敬请见谅!
上图为修改菜品后的菜单
上图为删除其中一个菜品后的界面
上图为查找某一菜品的界面
对当天销售记录的统计
上面两图为删除订单后的界面以及相应txt文档的变化
特别鸣谢:以上内容是在我的好朋友FYQ的帮助下完成的,在此对他表示感谢!!!!!