使用c++开发一个简单的饭店订餐管理系统。要求实现以下功能: 【基本功能】 (1)菜单数据文件。菜单的信息包括:菜名,价格基本信息 (2)用户数据文件。每个用户的信息包括:用户名、手机号、账单、座位号等。 (3)能够创建用户信息、为预约用户安排座位、记录点菜情况、记录预约时间、记录消费情况等。 (4)能够按权限设置,实现基本的添加、删除、查找、修改、排序功能。 (5)能够将菜品信息、订餐信息、消费信息等存到磁盘、从文件导入等。 【扩展功能】 可以按照自己对饭店订餐管理系统的理解和解决问题的程度对系统进行扩展。比如:(1)支持新品批量导入(从文件中导入);(2)可以添加“用户订餐信息”的管理模块;(3)统计功能等。总之,可以根据自己的需求分析进行功能扩展
时间: 2023-08-14 21:02:48 浏览: 83
好的,这是一个非常全面的饭店订餐管理系统的需求。下面是一个使用C++开发的简单的饭店订餐管理系统的代码示例,包含基本功能的实现,但并不包含扩展功能的实现。
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// 菜品类
class Dish {
public:
Dish() {}
Dish(string name, double price) : name(name), price(price) {}
string getName() const { return name; }
double getPrice() const { return price; }
void setName(string name) { this->name = name; }
void setPrice(double price) { this->price = price; }
friend ostream& operator<<(ostream& os, const Dish& dish);
private:
string name;
double price;
};
ostream& operator<<(ostream& os, const Dish& dish) {
os << dish.name << " " << dish.price;
return os;
}
// 用户类
class User {
public:
User() {}
User(string name, string phone) : name(name), phone(phone) {}
string getName() const { return name; }
string getPhone() const { return phone; }
double getBill() const { return bill; }
int getSeat() const { return seat; }
void setName(string name) { this->name = name; }
void setPhone(string phone) { this->phone = phone; }
void setBill(double bill) { this->bill = bill; }
void setSeat(int seat) { this->seat = seat; }
void addDish(const Dish& dish) { dishes.push_back(dish); }
void clearDishes() { dishes.clear(); }
friend ostream& operator<<(ostream& os, const User& user);
private:
string name;
string phone;
double bill = 0;
int seat = -1;
vector<Dish> dishes;
};
ostream& operator<<(ostream& os, const User& user) {
os << user.name << " " << user.phone << " " << user.seat << " " << user.bill << endl;
for (const auto& dish : user.dishes) {
os << dish << endl;
}
return os;
}
// 订餐管理系统类
class Restaurant {
public:
Restaurant() {}
void initDishesFromFile(string filename) {
ifstream fin(filename);
if (!fin) {
cerr << "Error: cannot open file " << filename << endl;
return;
}
dishes.clear();
string name;
double price;
while (fin >> name >> price) {
dishes.emplace_back(name, price);
}
fin.close();
}
void initUsersFromFile(string filename) {
ifstream fin(filename);
if (!fin) {
cerr << "Error: cannot open file " << filename << endl;
return;
}
users.clear();
string name, phone;
double bill;
int seat;
while (fin >> name >> phone >> seat >> bill) {
User user(name, phone);
user.setSeat(seat);
user.setBill(bill);
string dishName;
while (fin >> dishName) {
if (dishName == "#") {
break;
}
auto it = find_if(dishes.begin(), dishes.end(), [dishName](const Dish& dish) { return dish.getName() == dishName; });
if (it != dishes.end()) {
user.addDish(*it);
} else {
cerr << "Error: dish " << dishName << " does not exist" << endl;
}
}
users.push_back(user);
}
fin.close();
}
void saveUsersToFile(string filename) {
ofstream fout(filename);
if (!fout) {
cerr << "Error: cannot open file " << filename << endl;
return;
}
for (const auto& user : users) {
fout << user;
fout << "#" << endl;
}
fout.close();
}
void createUser() {
string name, phone;
cout << "Please enter user name: ";
cin >> name;
cout << "Please enter user phone: ";
cin >> phone;
users.emplace_back(name, phone);
cout << "User " << name << " created" << endl;
}
void deleteUser() {
string name;
cout << "Please enter user name: ";
cin >> name;
auto it = find_if(users.begin(), users.end(), [name](const User& user) { return user.getName() == name; });
if (it != users.end()) {
users.erase(it);
cout << "User " << name << " deleted" << endl;
} else {
cerr << "Error: user " << name << " does not exist" << endl;
}
}
void searchUser() {
string name;
cout << "Please enter user name: ";
cin >> name;
auto it = find_if(users.begin(), users.end(), [name](const User& user) { return user.getName() == name; });
if (it != users.end()) {
cout << *it;
} else {
cerr << "Error: user " << name << " does not exist" << endl;
}
}
void modifyUser() {
string name, phone;
cout << "Please enter user name: ";
cin >> name;
auto it = find_if(users.begin(), users.end(), [name](const User& user) { return user.getName() == name; });
if (it != users.end()) {
cout << "Please enter new phone: ";
cin >> phone;
it->setPhone(phone);
cout << "User " << name << " modified" << endl;
} else {
cerr << "Error: user " << name << " does not exist" << endl;
}
}
void sortUsers() {
sort(users.begin(), users.end(), [](const User& a, const User& b) { return a.getBill() > b.getBill(); });
cout << "Users sorted by bill, descending order" << endl;
}
void listDishes() {
cout << "Dishes:" << endl;
for (const auto& dish : dishes) {
cout << dish << endl;
}
}
void createReservation() {
string name, phone;
int seat;
cout << "Please enter user name: ";
cin >> name;
cout << "Please enter user phone: ";
cin >> phone;
cout << "Please enter seat number: ";
cin >> seat;
auto it = find_if(users.begin(), users.end(), [name](const User& user) { return user.getName() == name; });
if (it != users.end()) {
cerr << "Error: user " << name << " already exists" << endl;
return;
}
users.emplace_back(name, phone);
auto userIt = users.end() - 1;
userIt->setSeat(seat);
cout << "User " << name << " reserved seat " << seat << endl;
}
void addDishToUser() {
string name, dishName;
cout << "Please enter user name: ";
cin >> name;
auto it = find_if(users.begin(), users.end(), [name](const User& user) { return user.getName() == name; });
if (it == users.end()) {
cerr << "Error: user " << name << " does not exist" << endl;
return;
}
cout << "Please enter dish name: ";
cin >> dishName;
auto dishIt = find_if(dishes.begin(), dishes.end(), [dishName](const Dish& dish) { return dish.getName() == dishName; });
if (dishIt == dishes.end()) {
cerr << "Error: dish " << dishName << " does not exist" << endl;
return;
}
it->addDish(*dishIt);
cout << "Dish " << dishName << " added to user " << name << endl;
}
void clearDishesOfUser() {
string name;
cout << "Please enter user name: ";
cin >> name;
auto it = find_if(users.begin(), users.end(), [name](const User& user) { return user.getName() == name; });
if (it == users.end()) {
cerr << "Error: user " << name << " does not exist" << endl;
return;
}
it->clearDishes();
cout << "Dishes of user " << name << " cleared" << endl;
}
void generateBillOfUser() {
string name;
double totalBill = 0;
cout << "Please enter user name: ";
cin >> name;
auto it = find_if(users.begin(), users.end(), [name](const User& user) { return user.getName() == name; });
if (it == users.end()) {
cerr << "Error: user " << name << " does not exist" << endl;
return;
}
for (const auto& dish : it->dishes) {
totalBill += dish.getPrice();
}
it->setBill(totalBill);
cout << "Bill of user " << name << " generated: " << totalBill << endl;
}
private:
vector<Dish> dishes;
vector<User> users;
};
int main() {
Restaurant restaurant;
restaurant.initDishesFromFile("dishes.txt");
restaurant.initUsersFromFile("users.txt");
while (true) {
cout << "Please select operation:" << endl;
cout << "1. Create user" << endl;
cout << "2. Delete user" << endl;
cout << "3. Search user" << endl;
cout << "4. Modify user" << endl;
cout << "5. Sort users" << endl;
cout << "6. List dishes" << endl;
cout << "7. Create reservation" << endl;
cout << "8. Add dish to user" << endl;
cout << "9. Clear dishes of user" << endl;
cout << "10. Generate bill of user" << endl;
cout << "11. Exit" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
restaurant.createUser();
break;
case 2:
restaurant.deleteUser();
break;
case 3:
restaurant.searchUser();
break;
case 4:
restaurant.modifyUser();
break;
case 5:
restaurant.sortUsers();
break;
case 6:
restaurant.listDishes();
break;
case 7:
restaurant.createReservation();
break;
case 8:
restaurant.addDishToUser();
break;
case 9:
restaurant.clearDishesOfUser();
break;
case 10:
restaurant.generateBillOfUser();
break;
case 11:
restaurant.saveUsersToFile("users.txt");
return 0;
default:
cerr << "Error: invalid choice" << endl;
break;
}
}
return 0;
}
```
这个示例代码实现了饭店订餐管理系统的基本功能,包括菜单数据文件、用户数据文件、创建用户、为用户预约座位、记录点菜情况、记录预约时间、记录消费情况、添加、删除、查找、修改、排序等功能。您可以自行扩展这个系统,添加新的功能。
阅读全文