赞
踩
以下是一个简单的Python餐厅点餐系统的示例代码。请注意,这只是一个基于命令行的简单实现,实际的餐厅点餐系统可能需要更多功能和复杂性
- class MenuItem:
- def __init__(self, name, price):
- self.name = name
- self.price = price
-
- class Order:
- def __init__(self):
- self.items = []
-
- def add_item(self, item):
- self.items.append(item)
-
- def remove_item(self, item):
- if item in self.items:
- self.items.remove(item)
- else:
- print("Item not found in the order.")
-
- def calculate_total(self):
- total = 0
- for item in self.items:
- total += item.price
- return total
-
- class Restaurant:
- def __init__(self):
- self.menu = {
- "burger": MenuItem("Burger", 5.99),
- "pizza": MenuItem("Pizza", 8.99),
- "salad": MenuItem("Salad", 4.99),
- # Add more items to the menu if needed
- }
-
- def display_menu(self):
- print("Menu:")
- for item_name, item in self.menu.items():
- print(f"{item_name}: ${item.price}")
-
- def place_order(self):
- order = Order()
- while True:
- self.display_menu()
- choice = input("Enter item to order (or 'done' to finish): ")
- if choice == 'done':
- break
- elif choice in self.menu:
- order.add_item(self.menu[choice])
- else:
- print("Invalid item. Please choose from the menu.")
- return order
-
- # 使用示例
- restaurant = Restaurant()
- new_order = restaurant.place_order()
-
- print("Your order:")
- for item in new_order.items:
- print(f"- {item.name}: ${item.price}")
- print(f"Total: ${new_order.calculate_total()}")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。