要求
学校拟开发一套食堂菜品管理系统,以便对菜品和同学们的评价进行管理,其中包含如下信息:
商户:商户名称、柜面位置、电话.....
菜品:菜品编号、菜品名称、价格、所属商户......
学生:注册账号、昵称、电话......
食堂里的商户均不同名,一个商户可以提供多样菜品,食堂对所有商户的菜品进行编号,每样菜品由其唯一的商户提供;学生可对菜品进行评价,系统要存储学生对菜品的评分、评价内容和评价时间。
基本要求包括:
1、提供用户操作的菜单和界面,用户至少可分为学生和商户两种类型
2、设计相应的信息表,用于记录信息,如学生信息表、商户信息表、菜品信息表、评价信息表等,要求以文件的形式存储,格式可以自行设计。
3、商户可进行如下操作:
(1) 本商户信息的新增、查看、修改、删除等。
(2) 发布和更新菜品信息。
(3) 查询本商户菜品评价信息
4、学生可进行如下操作:
(4) 个人信息的新增、查看、修改、删除等。
(5) 对各菜品信息、评价信息、统计信息(评分高低排序)的浏览
(6)对各菜品的评分和评价。
5、系统退出时,更新相应文件中的信息;当下次运行程序时,从文件读取所有信息。
演示
主菜单
商户菜单
学生菜单
代码
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace std;
class Student {
public:
int studentId;
string nickname;
string phoneNumber;
string password;
Student(int id, string nick, string phone, string pass)
: studentId(id), nickname(nick), phoneNumber(phone), password(pass) {}
};
class Evaluation {
public:
double rating;
string content;
string timestamp;
int studentId;
Evaluation(double r, string c, string time, int sId)
: rating(r), content(c), timestamp(time), studentId(sId) {}
};
class Dish {
public:
int dishId;
string dishName;
double price;
int merchantId;
vector<Evaluation> evaluations;
Dish(int id, string name, double p, int mId)
: dishId(id), dishName(name), price(p), merchantId(mId) {}
void addEvaluation(double rating, string content, string timestamp, int studentId) {
Evaluation newEvaluation(rating, content, timestamp, studentId);
evaluations.push_back(newEvaluation);
}
};
class Merchant {
public:
int merchantId;
string merchantName;
string counterLocation;
string phoneNumber;
string password;
Merchant(int id, string name, string location, string phone, string pass)
: merchantId(id), merchantName(name), counterLocation(location), phoneNumber(phone), password(pass) {}
// 查找菜品
Dish* findDishById(int dishId, vector<Dish>& dishes) {
for (int i = 0; i < dishes.size(); ++i) {
if (dishes[i].dishId == dishId && dishes[i].merchantId == merchantId) {
return &dishes[i];
}
}
return nullptr; // 未找到菜品
}
// 显示菜品评价信息
void displayDishEvaluations( vector<Dish>& dishes) {
cout << "-------- 查询本商户菜品评价信息 --------" << endl;
// 遍历商户的菜品
for (int j = 0; j < dishes.size(); ++j) {
if (dishes[j].merchantId == merchantId && !dishes[j].evaluations.empty()) {
cout << "菜品编号: " << dishes[j].dishId << endl;
cout << "菜品名称: " << dishes[j].dishName << endl;
cout << setw(10) << left << "评分" << setw(20) << left << "评价内容" << setw(25) << left << "评价时间" << endl;
// 遍历菜品的评价信息
for (int k = 0; k < dishes[j].evaluations.size(); ++k) {
cout << setw(10) << left << dishes[j].evaluations[k].rating << setw(20)
<< left << dishes[j].evaluations[k].content << setw(25) << left << dishes[j].evaluations[k].timestamp << endl;
}
cout << "--------------------------" << endl;
}
}
}
// 显示指定菜品的评价信息
void displayDishEvaluation(int chosenDishId, vector<Dish>& dishes) {
// 查找菜品并显示评价
Dish* chosenDish = findDishById(chosenDishId, dishes);
if (chosenDish != nullptr) {
cout << "-------- 菜品评价信息 --------" << endl;
cout << "菜品编号: " << chosenDish->dishId << endl;
cout << "菜品名称: " << chosenDish->dishName << endl;
if (chosenDish->evaluations.size()==0){
cout<<"暂无评价信息"<<endl;
cout << "--------------------------" << endl;
return ;
}
cout << setw(10) << left << "评分" << setw(20) << left << "评价内容" << setw(25) << left << "评价时间" << endl;
for (int i=0; i< chosenDish->evaluations.size(); i++) {
cout << setw(10) << left << chosenDish->evaluations[i].rating <<
setw(20) << left << chosenDish->evaluations[i].content <<
setw(25) << left <<chosenDish->evaluations[i].timestamp << endl;
}
cout << "--------------------------" << endl;
} else {
cout << "未找到菜品编号为 " << chosenDishId << " 的菜品或无评价信息。" << endl;
}
}
// 显示菜品列表
void displayDishList( vector<Dish>& dishes) {
cout << "商户菜品列表:" << endl;
cout << setw(10) << left << "菜品编号" << setw(20) << left << "菜品名称" << setw(15) << left << "菜品价格" << endl;
for (int i = 0; i < dishes.size(); ++i) {
if (dishes[i].merchantId == merchantId) {
cout << setw(10) << left << dishes[i].dishId << setw(20) << left << dishes[i].dishName << setw(15) << left << fixed << setprecision(2) << dishes[i].price << endl;
}
}
cout << "--------------------------" << endl;
}
// 评价菜品
void evaluateDish(int chosenDishId,Student currentStudent, vector<Dish>& dishes) {
// 查找菜品并进行评价
Dish* chosenDish = findDishById(chosenDishId, dishes);
if (chosenDish != nullptr) {
double rating;
cout << "请输入评分 (0.0 到 5.0): ";
cin >> rating;
if (rating >= 0.0 && rating <= 5.0) {
string content;
cout << "请输入评价内容: ";
cin>>content;
// 获取当前时间作为评价时间
time_t now = time(0);
tm* localTime = localtime(&now);
char timestamp[80];
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d_%H:%M:%S", localTime);
// 添加评价
chosenDish->addEvaluation(rating, content, timestamp, currentStudent.studentId);
cout << "菜品评价成功!" << endl;
} else {
cout << "评分无效,请输入有效的评分范围 (0.0 到 5.0)。" << endl;
}
} else {
cout << "未找到菜品编号为 " << chosenDishId << " 的菜品。" << endl;
}
}
};
void displayMainMenu() {
cout << "-------- 欢迎使用食堂菜品管理系统 --------" << endl;
cout << "1. 新增商户" << endl;
cout << "2. 新增学生" << endl;
cout << "3. 商户菜单" << endl;
cout << "4. 学生菜单" << endl;
cout << "5. 退出系统并保存数据" << endl;
cout << "----------------------------------------" << endl;
}
// 用于发布/更新菜品信息
void updateDishInformation(Merchant& merchant, vector<Dish>& dishes) {
cout << "-------- 发布/更新菜品信息 --------" << endl;
cout << "1. 发布新菜品" << endl;
cout << "2. 更新已有菜品" << endl;
cout << "3. 返回上一级菜单" << endl;
cout << "----------------------------------" << endl;
int choice;
cout << "请选择操作: ";
cin >> choice;
switch (choice) {
case 1: {
int dishId;
string dishName;
double price;
cout << "请输入菜品编号: ";
cin >> dishId;
for (int j = 0; j < dishes.size(); ++j) {
if (dishes[j].dishId == dishId) {
cout << "菜品编号已存在,请重新输入。" << endl;
return;
}
}
cout << "请输入菜品名称: ";
cin>>dishName;
cout << "请输入菜品价格: ";
cin >> price;
Dish newDish(dishId, dishName, price, merchant.merchantId);
dishes.push_back(newDish);
cout << "菜品发布成功!" << endl;
}
break;
case 2: {
int dishId;
cout << "请输入要更新的菜品编号: ";
cin >> dishId;
for (int j = 0; j < dishes.size(); ++j) {
if (dishes[j].dishId == dishId && dishes[j].merchantId == merchant.merchantId) {
cout << "当前菜品信息:" << endl;
cout << "菜品编号: " << dishes[j].dishId << endl;
cout << "菜品名称: " << dishes[j].dishName << endl;
cout << "菜品价格: " << fixed << setprecision(2) << dishes[j].price << endl;
cout << "请输入新的菜品名称: ";
cin>>dishes[j].dishName;
cout << "请输入新的菜品价格: ";
double newPrice;
cin >> newPrice;
if (newPrice > 0) {
dishes[j].price = newPrice;
}
cout << "菜品信息更新成功!" << endl;
return;
}
}
cout << "未找到菜品编号为 " << dishId << " 的菜品或无权限更新。" << endl;
}
break;
case 3:
return;
default:
cout << "无效的选择,请重新输入。" << endl;
}
}
void displayMerchantDishes( vector<Merchant>& merchants, vector<Dish>& dishes) {
string merchantName;
cout << "请输入商户名称: ";
cin>>merchantName;
bool merchantFound = false;
// 遍历商户列表查找指定商户
for (int i = 0; i < merchants.size(); ++i) {
if (merchants[i].merchantName == merchantName) {
merchantFound = true;
cout << "-------- " << merchantName << " 的菜品信息 --------" << endl;
cout << setw(10) << left << "菜品编号" << setw(20) << left << "菜品名称" << setw(15) << left << "菜品价格" << endl;
// 使用普通的 for 循环
for (int j = 0; j < dishes.size(); ++j) {
if (dishes[j].merchantId == merchants[i].merchantId) {
cout << setw(10) << left << dishes[j].dishId << setw(20) << left << dishes[j].dishName << setw(15) << left << fixed << setprecision(2) << dishes[j].price << endl;
}
}
cout << "--------------------------" << endl;
break; // 结束循环,因为已找到指定商户
}
}
if (!merchantFound) {
cout << "未找到商户名称为 " << merchantName << " 的商户。" << endl;
}
}
void displayMerchantMenu(vector<Merchant>& merchants, vector<Dish>& dishes) {
int merchantId;
string password;
cout << "请输入商户编号: ";
cin >> merchantId;
cout << "请输入密码: ";
cin >> password;
// 在这里你需要遍历 merchants 来查找指定的商户
// 如果找到了商户,继续执行相应的操作
// 注意:在实际应用中,你可能需要设计一种更加高效的查找机制,比如使用索引等
// 这里为了简化示例,直接使用线性搜索
for (int i = 0; i < merchants.size(); ++i) {
if (merchants[i].merchantId == merchantId && merchants[i].password == password) {
while(1) {
cout << "-------- 商户菜单 --------" << endl;
cout << "1. 查看本商户信息" << endl;
cout << "2. 修改本商户信息" << endl;
cout << "3. 删除本商户信息" << endl;
cout << "4. 发布/更新菜品信息" << endl;
cout << "5. 查看本商户的所有菜品"<<endl;
cout << "6. 查询本商户菜品评价信息" << endl;
cout << "7. 返回上一级菜单" << endl;
cout << "-----------------------------" << endl;
int choice;
cout << "请选择商户操作: ";
cin >> choice;
// 根据用户选择执行相应的商户操作
// 可调用相应的商户类成员函数
switch (choice) {
case 1:
// 查看本商户信息
cout << "-------- 商户信息 --------" << endl;
cout << "商户编号: " << merchants[i].merchantId << endl;
cout << "商户名称: " << merchants[i].merchantName << endl;
cout << "柜面位置: " << merchants[i].counterLocation << endl;
cout << "联系电话: " << merchants[i].phoneNumber << endl;
cout << "--------------------------" << endl;
break;
case 2:
// 修改本商户信息
cout << "请输入新的商户名称: ";
cin>>merchants[i].merchantName;
for (int k = 0; k < merchants.size(); ++k) {
if (merchants[k].merchantName == merchants[i].merchantName) {
if (k!=i) {
cout << "商户名已存在,请重新输入。" << endl;
break;
}
}
}
cout << "请输入新的柜面位置: ";
cin>>merchants[i].counterLocation;
cout << "请输入新的联系电话: ";
cin>>merchants[i].phoneNumber;
cout << "商户信息修改成功!" << endl;
break;
case 3:
// 删除本商户信息
merchants.erase(merchants.begin() + i);
cout << "商户信息删除成功!" << endl;
return; // 退出函数,避免继续运行
case 4:
updateDishInformation(merchants[i], dishes);
break;
case 5:
merchants[i].displayDishList(dishes);
case 6:
merchants[i].displayDishEvaluations(dishes);
break;
case 7:
return ;
}
}
}
}
cout << "商户编号或密码错误,无法进入商户菜单。" << endl;
}
void displayStudentMenu(vector<Student>& students,vector<Merchant>&merchants, vector<Dish>& dishes) {
int studentId;
string password;
cout << "请输入学生编号: ";
cin >> studentId;
cout << "请输入密码: ";
cin >> password;
for (int i = 0; i < students.size(); ++i) {
if (students[i].studentId == studentId && students[i].password == password) {
while(1) {
cout << "-------- 学生菜单 --------" << endl;
cout << "1. 查看个人信息" << endl;
cout << "2. 修改个人信息" << endl;
cout << "3. 删除个人信息" << endl;
cout << "4. 浏览菜品信息" << endl;
cout << "5. 浏览评价信息" << endl;
cout << "6. 对菜品评分和评价" << endl;
cout << "7. 返回上一级菜单" << endl;
cout << "-----------------------------" << endl;
int choice;
cout << "请选择学生操作: ";
cin >> choice;
// 根据用户选择执行相应的学生操作
// 调用相应的学生类成员函数
switch (choice) {
case 1:
// 查看个人信息
cout << "-------- 学生个人信息 --------" << endl;
cout << "学生编号: " << students[i].studentId << endl;
cout << "昵称: " << students[i].nickname << endl;
cout << "联系电话: " << students[i].phoneNumber << endl;
cout << "-----------------------------" << endl;
break;
case 2:
cout << "请输入新的昵称: ";
cin>>students[i].nickname;
cout << "请输入新的联系电话: ";
cin>>students[i].phoneNumber;
cout << "请输入新的密码: ";
cin >> students[i].password;
cout << "学生信息修改成功!" << endl;
break;
case 3:
// 删除个人信息
students.erase(students.begin() + i);
cout << "学生信息删除成功!" << endl;
return; // 退出函数,避免继续运行
case 4:
displayMerchantDishes(merchants,dishes);
break;
case 5: {
// 查询本商户菜品评价信息
string merchantName;
cout << "请输入商户名称: ";
cin>>merchantName;
bool merchantFound = false;
// 遍历商户列表查找指定商户
for (int i = 0; i < merchants.size(); ++i) {
if (merchants[i].merchantName == merchantName) {
merchantFound = true;
// 选择要查看评价的菜品
int chosenDishId;
cout << "请输入要查看评价的菜品编号: ";
cin >> chosenDishId;
// 使用商户类的成员函数显示指定菜品的评价信息
merchants[i].displayDishEvaluation(chosenDishId, dishes);
break; // 结束循环,因为已找到指定商户
}
}
if (!merchantFound) {
cout << "未找到商户名称为 " << merchantName << " 的商户。" << endl;
}
break;
}
case 6: {
// 对菜品评分和评价
string merchantName;
cout << "请输入商户名称: ";
cin>>merchantName;
bool merchantFound = false;
// 遍历商户列表查找指定商户
for (int i = 0; i < merchants.size(); ++i) {
if (merchants[i].merchantName == merchantName) {
merchantFound = true;
cout << "-------- 对菜品评分和评价 --------" << endl;
// 使用商户类的成员函数显示菜品列表
merchants[i].displayDishList(dishes);
// 选择要评价的菜品
int chosenDishId;
cout << "请输入要评价的菜品编号: ";
cin >> chosenDishId;
// 使用商户类的成员函数进行评价
merchants[i].evaluateDish(chosenDishId,students[i],dishes);
break; // 结束循环,因为已找到指定商户
}
}
if (!merchantFound) {
cout << "未找到商户名称为 " << merchantName << " 的商户。" << endl;
}
break;
}
case 7:
return ;
}
}
}
}
cout << "学生编号或密码错误,无法进入学生菜单。" << endl;
}
void addMerchant(vector<Merchant>& merchants) {
int id;
string name, location, phone, password;
cout << "请输入商户编号: ";
cin >> id;
// 检查商户编号是否已存在
for (int i = 0; i < merchants.size(); ++i) {
if (merchants[i].merchantId == id) {
cout << "商户编号已存在,请重新输入。" << endl;
return;
}
}
cout << "请输入商户名称: ";
cin>>name;
for (int i = 0; i < merchants.size(); ++i) {
if (merchants[i].merchantName == name) {
cout << "商户名已存在,请重新输入。" << endl;
return;
}
}
cout << "请输入柜面位置: ";
cin>>location;
cout << "请输入联系电话: ";
cin>>phone;
cout << "请输入密码: ";
cin >> password;
Merchant newMerchant(id, name, location, phone, password);
merchants.push_back(newMerchant);
cout << "商户新增成功!" << endl;
}
void addStudent(vector<Student>& students) {
int id;
string account, nick, phone, password;
cout << "请输入学生编号: ";
cin >> id;
// 检查学生编号是否已存在
for (int i = 0; i < students.size(); ++i) {
if (students[i].studentId == id) {
cout << "学生编号已存在,请重新输入。" << endl;
return;
}
}
cout << "请输入昵称: ";
cin>>nick;
cout << "请输入联系电话: ";
cin>>phone;
cout << "请输入密码: ";
cin >> password;
Student newStudent(id, nick, phone, password);
students.push_back(newStudent);
cout << "学生新增成功!" << endl;
}
// 从文件加载数据到向量中
void loadData(vector<Merchant>& merchants, vector<Student>& students, vector<Dish>& dishes) {
ifstream merchantFile("merchants.txt");
ifstream studentFile("students.txt");
ifstream dishFile("dishes.txt");
// 检查文件是否打开成功
if (!merchantFile.is_open() || !studentFile.is_open() || !dishFile.is_open()) {
return;
}
// 从文件读取数据到向量中
for (int i = 0; !merchantFile.eof(); ++i) {
int id;
string name, location, phone, password;
merchantFile >> id >> name >> location >> phone >> password;
if (!merchantFile.eof()) {
Merchant newMerchant(id, name, location, phone, password);
merchants.push_back(newMerchant);
}
}
for (int i = 0; !studentFile.eof(); ++i) {
int id;
string account, nick, phone, password;
studentFile >> id >> nick >> phone >> password;
if (!studentFile.eof()) {
Student newStudent(id, nick, phone, password);
students.push_back(newStudent);
}
}
for (int i = 0; !dishFile.eof(); ++i) {
int id, merchantId;
string name;
double price;
int eCnt=0;
dishFile >> id >> name >> price >> merchantId >>eCnt;
if (!dishFile.eof()) {
Dish newDish(id, name, price, merchantId);
for (int k=0;k<eCnt;k++){
string content;
int studentId;
double rating;
string timeStamp;
dishFile>>studentId>>content>>rating>>timeStamp;
newDish.evaluations.push_back(Evaluation(rating,content,timeStamp,studentId));
}
dishes.push_back(newDish);
}
}
// 关闭文件
merchantFile.close();
studentFile.close();
dishFile.close();
}
// 将数据从向量保存到文件中
void saveData( vector<Merchant>& merchants, vector<Student>& students, vector<Dish>& dishes) {
ofstream merchantFile("merchants.txt");
ofstream studentFile("students.txt");
ofstream dishFile("dishes.txt");
// 检查文件是否打开成功
if (!merchantFile.is_open() || !studentFile.is_open() || !dishFile.is_open()) {
cout << "打开一个或多个数据文件以进行写入时发生错误。程序退出。" << endl;
exit(1);
}
// 将数据从向量写入文件中
for (int i = 0; i < merchants.size(); ++i) {
merchantFile << merchants[i].merchantId << " " << merchants[i].merchantName << " "
<< merchants[i].counterLocation << " " << merchants[i].phoneNumber << " "
<< merchants[i].password << endl;
}
for (int i = 0; i < students.size(); ++i) {
studentFile << students[i].studentId << " "
<< students[i].nickname << " " << students[i].phoneNumber << " "
<< students[i].password << endl;
}
for (int i = 0; i < dishes.size(); ++i) {
dishFile << dishes[i].dishId << " " << dishes[i].dishName << " " << dishes[i].price << " "
<< dishes[i].merchantId<<" ";
dishFile<<dishes[i].evaluations.size()<<" ";
for( int k=0;k<dishes[i].evaluations.size();k++){
dishFile<<dishes[i].evaluations[k].studentId<<" "
<<dishes[i].evaluations[k].content<<" "
<<dishes[i].evaluations[k].rating<<" "
<<dishes[i].evaluations[k].timestamp;
}
dishFile<<endl;
}
// 关闭文件
merchantFile.close();
studentFile.close();
dishFile.close();
}
int main() {
vector<Merchant> merchants;
vector<Student> students;
vector<Dish> dishes;
loadData(merchants, students, dishes);
cout<< "重要提示:所有输入不能包含空格,不然会导致程序数据异常"<<endl;
cout<< " 所有编号都是整数,不能输入数字外的其他字符"<<endl;
int choice;
while (true) {
displayMainMenu();
cout << "请选择操作: ";
cin >> choice;
switch (choice) {
case 1:
addMerchant(merchants);
break;
case 2:
addStudent(students);
break;
case 3:
// 商户菜单
displayMerchantMenu(merchants, dishes);
break;
case 4:
// 学生菜单
displayStudentMenu(students,merchants, dishes);
break;
case 5:
// 退出系统
saveData(merchants, students, dishes);
cout << "感谢使用食堂菜品管理系统,再见!" << endl;
return 0;
default:
cout << "无效的选择,请重新输入。" << endl;
}
}
}
说明
使用dev c++直接运行即可,添加一些商品评价等数据就会自动生成数据文件。
如需讲解或者注释,请私信我