用c++生成一段餐厅点餐系统的代码能够实现基本点餐功能和餐厅管理功能
时间: 2024-03-10 16:48:05 浏览: 91
基于C++的点餐系统设计
好的,以下是一个简单的餐厅点餐系统的 C++ 代码,包含基本点餐功能和餐厅管理功能。注意,这只是一个示例代码,可以根据具体需求进行修改和完善。
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Dish {
public:
string name;
float price;
Dish(string name, float price) : name(name), price(price) {}
};
class Order {
public:
Dish dish;
int count;
Order(Dish dish, int count) : dish(dish), count(count) {}
};
class Table {
public:
int id;
vector<Order> orders;
Table(int id) : id(id) {}
};
class Restaurant {
public:
vector<Dish> dishes;
vector<Table> tables;
Restaurant() {}
void addDish(string name, float price) {
dishes.push_back(Dish(name, price));
}
void addTable(int id) {
tables.push_back(Table(id));
}
void printDishes() {
cout << "Dishes:\n";
for (Dish dish : dishes) {
cout << dish.name << " - $" << dish.price << endl;
}
}
void printTables() {
cout << "Tables:\n";
for (Table table : tables) {
cout << "Table " << table.id << endl;
if (table.orders.empty()) {
cout << "No orders\n";
} else {
float total = 0;
for (Order order : table.orders) {
cout << order.dish.name << " x " << order.count << " - $" << order.dish.price * order.count << endl;
total += order.dish.price * order.count;
}
cout << "Total: $" << total << endl;
}
}
}
void takeOrder(int tableId, string dishName, int count) {
Table* table = findTable(tableId);
if (table == nullptr) {
cout << "Table not found\n";
return;
}
Dish* dish = findDish(dishName);
if (dish == nullptr) {
cout << "Dish not found\n";
return;
}
table->orders.push_back(Order(*dish, count));
cout << "Order taken successfully\n";
}
void removeOrder(int tableId, string dishName, int count) {
Table* table = findTable(tableId);
if (table == nullptr) {
cout << "Table not found\n";
return;
}
Dish* dish = findDish(dishName);
if (dish == nullptr) {
cout << "Dish not found\n";
return;
}
for (int i = 0; i < table->orders.size(); i++) {
if (table->orders[i].dish.name == dishName) {
if (table->orders[i].count > count) {
table->orders[i].count -= count;
cout << "Order removed successfully\n";
return;
} else if (table->orders[i].count == count) {
table->orders.erase(table->orders.begin() + i);
cout << "Order removed successfully\n";
return;
} else {
cout << "Not enough orders to remove\n";
return;
}
}
}
cout << "Order not found\n";
}
private:
Table* findTable(int id) {
for (Table& table : tables) {
if (table.id == id) {
return &table;
}
}
return nullptr;
}
Dish* findDish(string name) {
for (Dish& dish : dishes) {
if (dish.name == name) {
return &dish;
}
}
return nullptr;
}
};
int main() {
Restaurant restaurant;
restaurant.addDish("Hamburger", 4.99);
restaurant.addDish("French fries", 1.99);
restaurant.addDish("Coke", 0.99);
restaurant.addTable(1);
restaurant.addTable(2);
restaurant.printDishes();
restaurant.takeOrder(1, "Hamburger", 2);
restaurant.takeOrder(1, "French fries", 1);
restaurant.takeOrder(2, "Coke", 3);
restaurant.printTables();
restaurant.removeOrder(1, "Hamburger", 1);
restaurant.printTables();
return 0;
}
```
以上代码实现了一个简单的餐厅点餐系统,包含了添加菜品和桌子、打印菜单和桌子信息、点餐和取消点餐等功能。可以根据需要进行修改和完善。
阅读全文