好的,我会尽力在300行代码内编写一个简单的可视化点单系统,以下是代码实现。
```python
import tkinter as tk
from tkinter import ttk
class Menu:
def __init__(self, name, price, description):
self.name = name
self.price = price
self.description = description
class Order:
def __init__(self, menu, quantity):
self.menu = menu
self.quantity = quantity
class PointOfSale:
def __init__(self):
self.menu_items = [
Menu("Hamburger", 10.99, "Juicy beef patty with lettuce, tomato, and onion on a sesame seed bun."),
Menu("Cheeseburger", 11.99, "Juicy beef patty with cheddar cheese, lettuce, tomato, and onion on a sesame seed bun."),
Menu("Chicken Sandwich", 9.99, "Grilled chicken breast with lettuce, tomato, and mayo on a whole wheat bun."),
Menu("French Fries", 3.99, "Crispy golden fries with a sprinkle of salt."),
Menu("Onion Rings", 4.99, "Crispy fried onion rings with a touch of spice.")
]
self.orders = []
def add_to_order(self, menu_index, quantity):
menu = self.menu_items[menu_index]
for order in self.orders:
if order.menu.name == menu.name:
order.quantity += quantity
break
else:
self.orders.append(Order(menu, quantity))
def remove_from_order(self, order_index):
self.orders.pop(order_index)
def get_total(self):
total = 0
for order in self.orders:
total += order.menu.price * order.quantity
return total
class POSApp:
def __init__(self):
self.pos = PointOfSale()
self.root = tk.Tk()
self.root.title("Point of Sale")
self.root.geometry("600x400")
self.menu_label = ttk.Label(self.root, text="Menu")
self.menu_label.grid(row=0, column=0, padx=5, pady=5)
self.order_label = ttk.Label(self.root, text="Order")
self.order_label.grid(row=0, column=1, padx=5, pady=5)
self.menu_listbox = tk.Listbox(self.root, width=40)
for menu_item in self.pos.menu_items:
self.menu_listbox.insert(tk.END, f"{menu_item.name} (${menu_item.price}) - {menu_item.description}")
self.menu_listbox.grid(row=1, column=0, padx=5, pady=5)
self.quantity_entry = tk.Entry(self.root, width=5)
self.quantity_entry.grid(row=2, column=0, padx=5, pady=5)
self.add_button = ttk.Button(self.root, text="Add to Order", command=self.add_to_order)
self.add_button.grid(row=3, column=0, padx=5, pady=5)
self.order_listbox = tk.Listbox(self.root, width=40)
self.order_listbox.grid(row=1, column=1, padx=5, pady=5)
self.remove_button = ttk.Button(self.root, text="Remove from Order", command=self.remove_from_order)
self.remove_button.grid(row=2, column=1, padx=5, pady=5)
self.total_label = ttk.Label(self.root, text="Total: $0.00")
self.total_label.grid(row=3, column=1, padx=5, pady=5)
self.root.mainloop()
def add_to_order(self):
selection = self.menu_listbox.curselection()
if selection:
menu_index = selection[0]
quantity = int(self.quantity_entry.get())
self.pos.add_to_order(menu_index, quantity)
self.update_order_listbox()
self.update_total_label()
def remove_from_order(self):
selection = self.order_listbox.curselection()
if selection:
order_index = selection[0]
self.pos.remove_from_order(order_index)
self.update_order_listbox()
self.update_total_label()
def update_order_listbox(self):
self.order_listbox.delete(0, tk.END)
for order in self.pos.orders:
self.order_listbox.insert(tk.END, f"{order.menu.name} x {order.quantity}")
def update_total_label(self):
total = self.pos.get_total()
self.total_label.config(text=f"Total: ${total:.2f}")
if __name__ == "__main__":
app = POSApp()
```
上述代码使用了Tkinter库来实现图形界面,定义了三个类Menu、Order和PointOfSale来存储菜单信息、订单信息和点单系统信息。在PointOfSale类中实现了添加、删除、获取订单总价等功能。最后,定义了POSApp类来创建图形界面,包括菜单列表框、数量输入框、添加按钮、订单列表框、删除按钮和总价标签,并实现了相应的按钮点击事件和更新函数。
需要注意的是,本代码没有实现数据持久化存储和读取功能,如果需要可以使用文件读写或数据库等方法来实现。