当前位置:   article > 正文

python餐厅点餐系统代码_python自助点餐程序代码

python自助点餐程序代码

以下是一个简单的Python餐厅点餐系统的示例代码。请注意,这只是一个基于命令行的简单实现,实际的餐厅点餐系统可能需要更多功能和复杂性

  1. class MenuItem:
  2. def __init__(self, name, price):
  3. self.name = name
  4. self.price = price
  5. class Order:
  6. def __init__(self):
  7. self.items = []
  8. def add_item(self, item):
  9. self.items.append(item)
  10. def remove_item(self, item):
  11. if item in self.items:
  12. self.items.remove(item)
  13. else:
  14. print("Item not found in the order.")
  15. def calculate_total(self):
  16. total = 0
  17. for item in self.items:
  18. total += item.price
  19. return total
  20. class Restaurant:
  21. def __init__(self):
  22. self.menu = {
  23. "burger": MenuItem("Burger", 5.99),
  24. "pizza": MenuItem("Pizza", 8.99),
  25. "salad": MenuItem("Salad", 4.99),
  26. # Add more items to the menu if needed
  27. }
  28. def display_menu(self):
  29. print("Menu:")
  30. for item_name, item in self.menu.items():
  31. print(f"{item_name}: ${item.price}")
  32. def place_order(self):
  33. order = Order()
  34. while True:
  35. self.display_menu()
  36. choice = input("Enter item to order (or 'done' to finish): ")
  37. if choice == 'done':
  38. break
  39. elif choice in self.menu:
  40. order.add_item(self.menu[choice])
  41. else:
  42. print("Invalid item. Please choose from the menu.")
  43. return order
  44. # 使用示例
  45. restaurant = Restaurant()
  46. new_order = restaurant.place_order()
  47. print("Your order:")
  48. for item in new_order.items:
  49. print(f"- {item.name}: ${item.price}")
  50. print(f"Total: ${new_order.calculate_total()}")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/80640
推荐阅读
相关标签
  

闽ICP备14008679号