题目描述:
设计一个能够模拟餐厅点餐流程的程序,需要实现以下功能:
- 显示菜单:输出餐厅提供的所有菜品及其价格。
- 点餐:输入菜品名称和数量,将菜品添加到订单中。
- 修改订单:输入菜品名称和数量,修改订单中已有的菜品的数量。
- 删除订单:输入菜品名称,将菜品从订单中删除。
- 订单总价:计算订单中所有菜品的总价。
设计思路:
为了实现这个点餐程序,我们需要设计以下几个类:
- MenuItem类:用于储存菜品信息,包括菜品名称和价格等属性,以及一些方法,如获取与设置菜品信息。
- OrderItem类:用于储存订单项,包括菜品名称、数量和小计等属性,以及一些方法,如计算小计的方法。
- Order类:用于储存所有的订单项,包括添加订单项、修改订单项、删除订单项、计算订单总价等方法。
- Menu类:用于显示菜单,接收用户输入,并调用相应的Order类中的方法。
程序流程图:
start --> displayMenu --> getUserChoice --> performAction --> end
| |
v v
addOrder modifyOrder
| |
v v
deleteOrder calculateTotal
代码实现:
#include <bits/stdc++.h>标签:return,cout,int,MenuItem,周三,menuItem,打卡,quantity From: https://www.cnblogs.com/zeyangshuaige/p/17356912.html
using namespace std;
class MenuItem {
private:
string name;
int price;
public:
MenuItem(string n, int p) : name(n), price(p) {}
string getName() { return name; }
int getPrice() { return price; }
};
class OrderItem {
private:
MenuItem menuItem;
int quantity;
public:
OrderItem(MenuItem mi, int q) : menuItem(mi), quantity(q) {}
MenuItem getMenuItem() { return menuItem; }
int getQuantity() { return quantity; }
void setQuantity(int q) { quantity = q; }
int getSubtotal() { return menuItem.getPrice() * quantity; }
};
class Order {
private:
vector<OrderItem> orderItems;
public:
void addOrder(MenuItem menuItem, int quantity) {
OrderItem orderItem = OrderItem(menuItem, quantity);
orderItems.push_back(orderItem);
}
void modifyOrder(MenuItem menuItem, int quantity) {
for (int i = 0; i < orderItems.size(); i++) {
if (orderItems[i].getMenuItem().getName() == menuItem.getName()) {
orderItems[i].setQuantity(quantity);
break;
}
}
}
void deleteOrder(MenuItem menuItem) {
for (int i = 0; i < orderItems.size(); i++) {
if (orderItems[i].getMenuItem().getName() == menuItem.getName()) {
orderItems.erase(orderItems.begin() + i);
break;
}
}
}
int calculateTotal() {
int total = 0;
for (OrderItem orderItem : orderItems) {
total += orderItem.getSubtotal();
}
return total;
}
};
class Menu {
private:
Order order;
vector<MenuItem> menuItems = {
MenuItem("A", 10),
MenuItem("B", 15),
MenuItem("C", 20),
MenuItem("D", 25)
};
public:
void displayMenu() {
cout << "Menu:" << endl;
for (MenuItem menuItem : menuItems) {
cout << menuItem.getName() << ": $" << menuItem.getPrice() << endl;
}
cout << endl;
}
string getMenuItemName() {
string name;
cout << "Enter the name of the item: ";
getline(cin >> ws, name);
return name;
}
int getQuantity() {
int quantity;
cout << "Enter the quantity of the item: ";
cin >> quantity;
return quantity;
}
int getUserChoice() {
int choice;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
return choice;
}
void performAction(int choice) {
string menuItemName;
int quantity, total;
switch (choice) {
case 1:
displayMenu();
break;
case 2:
menuItemName = getMenuItemName();
quantity = getQuantity();
for (MenuItem menuItem : menuItems) {
if (menuItem.getName() == menuItemName) {
order.addOrder(menuItem, quantity);
cout << quantity << " " << menuItemName << "(s) have been added to the order." << endl << endl;
return;
}
}
cout << "Invalid item name. Please try again." << endl << endl;
break;
case 3:
menuItemName = getMenuItemName();
quantity = getQuantity();
for (MenuItem menuItem : menuItems) {
if (menuItem.getName() == menuItemName) {
order.modifyOrder(menuItem, quantity);
cout << menuItemName << "(s) quantity has been modified to " << quantity << "." << endl << endl;
return;
}
}
cout << "Invalid item name. Please try again." << endl << endl;
break;
case 4:
menuItemName = getMenuItemName();
for (MenuItem menuItem : menuItems) {
if (menuItem.getName() == menuItemName) {
order.deleteOrder(menuItem);
cout << menuItemName << "(s) have been removed from the order." << endl << endl;
return;
}
}
cout << "Invalid item name. Please try again." << endl << endl;
break;
case 5:
total = order.calculateTotal();
cout << "Total: $" << total << endl << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl << endl;
}
}
};
int main() {
Menu menu = Menu();
int choice;
while (true) {
cout << "1. Display menu" << endl;
cout << "2. Add item to order" << endl;
cout << "3. Modify item in order" << endl;
cout << "4. Delete item from order" << endl;
cout << "5. Calculate order total" << endl;
choice = menu.getUserChoice();
menu.performAction(choice);
}
return 0;
}