python食堂管理系统代码
时间: 2023-09-29 07:10:10 浏览: 93
由于食堂管理系统的具体功能和实现方式可能会有所不同,因此下面提供一个简单的 Python 食堂管理系统代码作为参考。
```python
import time
class Order:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
class Menu:
def __init__(self):
self.items = []
def add_item(self, name, price):
self.items.append((name, price))
def get_items(self):
return self.items
class Kitchen:
def __init__(self):
self.orders = []
def add_order(self, order):
self.orders.append(order)
def get_orders(self):
return self.orders
class Cashier:
def __init__(self, menu):
self.menu = menu
def take_order(self):
order_list = []
menu_items = self.menu.get_items()
print("Please choose from the following items:")
for i, item in enumerate(menu_items):
print("{0}. {1} (${2})".format(i+1, item[0], item[1]))
print("Enter 'done' to finish ordering.")
while True:
choice = input("Enter the item number or 'done': ")
if choice == 'done':
break
elif not choice.isdigit() or int(choice) <= 0 or int(choice) > len(menu_items):
print("Invalid input. Please try again.")
else:
item = menu_items[int(choice)-1]
quantity = int(input("Enter the quantity of {0}: ".format(item[0])))
order_list.append(Order(item[0], item[1], quantity))
return order_list
def calculate_total(self, order_list):
total = sum([order.price * order.quantity for order in order_list])
return total
def process_payment(self, total):
print("Your total is ${0:.2f}.".format(total))
while True:
payment = input("Enter the payment amount: $")
if not payment.replace('.','').isdigit() or float(payment) < total:
print("Invalid input. Please try again.")
else:
break
change = float(payment) - total
print("Thank you for your payment. Your change is ${0:.2f}.".format(change))
class Customer:
def __init__(self, name, cashier, kitchen):
self.name = name
self.cashier = cashier
self.kitchen = kitchen
self.order_list = []
def place_order(self):
self.order_list = self.cashier.take_order()
for order in self.order_list:
self.kitchen.add_order(order)
def check_status(self):
orders = self.kitchen.get_orders()
if len(orders) == 0:
print("Your order is being processed. Please wait.")
else:
print("Your order is ready. Enjoy your meal!")
total = self.cashier.calculate_total(self.order_list)
self.cashier.process_payment(total)
def main():
# Initialize the menu
menu = Menu()
menu.add_item("Hamburger", 6.99)
menu.add_item("Cheeseburger", 7.99)
menu.add_item("Fries", 2.99)
menu.add_item("Salad", 4.99)
# Initialize the cashier and kitchen
cashier = Cashier(menu)
kitchen = Kitchen()
# Initialize the customer
customer = Customer("John", cashier, kitchen)
# Place an order and check status
customer.place_order()
time.sleep(5) # Simulate the kitchen preparing the order
customer.check_status()
if __name__ == '__main__':
main()
```
该代码包含了 `Order`、`Menu`、`Kitchen`、`Cashier` 和 `Customer` 等类,并且通过这些类实现了食堂管理系统的基本功能,包括查看菜单、下订单、结账等等。具体实现过程可以参考代码注释。需要注意的是,该代码仅供参考,实际场景中可能需要根据具体需求进行修改或扩展。
阅读全文