如何在基于Python基础概念开发的餐厅点餐系统中实现客户订单历史的存储与展示
Hey there! Let's work through this together—since you're just starting out with Python, we'll stick to super foundational concepts (no fancy libraries or complex stuff, promise!) to build your restaurant order system. Here's a step-by-step breakdown that's easy to follow:
基础实现方案:用列表+字典(适合纯新手)
First off, let's map out what we need to store:
- Each order needs a unique ID, customer name, list of items (with name, price, quantity), and total price.
- All orders will live in a list to track the full order history.
Here's a working code example with comments to explain each part:
# Initialize an empty list to hold all order history order_history = [] def create_order(customer_name, items): """Create a new order and add it to the history""" # Generate a unique order ID (uses the current number of orders + 1) order_id = len(order_history) + 1 # Calculate total price for the order total_price = sum(item["price"] * item["quantity"] for item in items) # Build the order as a dictionary new_order = { "order_id": order_id, "customer_name": customer_name, "items": items, "total_price": total_price } # Add the new order to our history list order_history.append(new_order) print(f"订单 #{order_id} 创建成功!") def show_order_history(): """Display all past orders in a readable format""" if not order_history: print("暂无订单历史!") return print("\n--- 订单历史 ---") for order in order_history: print(f"\n订单ID: {order['order_id']}") print(f"客户姓名: {order['customer_name']}") print("商品明细:") for item in order["items"]: print(f"- {item['name']} x{item['quantity']} | 单价: {item['price']}元") print(f"总价: {order['total_price']}元") # 测试系统 if __name__ == "__main__": # 示例订单1 items1 = [ {"name": "经典汉堡", "price": 30, "quantity": 2}, {"name": "薯条", "price": 12, "quantity": 1} ] create_order("张三", items1) # 示例订单2 items2 = [ {"name": "素食沙拉", "price": 25, "quantity": 1}, {"name": "冰可乐", "price": 8, "quantity": 2} ] create_order("李四", items2) # 展示所有订单 show_order_history()
这段代码的逻辑说明:
order_history相当于我们的"订单数据库",每创建一个新订单就把它加入这个列表。- 每个商品用字典存储细节,方便后续计算总价和展示信息。
create_order负责构建订单并存储,show_order_history遍历列表把所有订单清晰打印出来。
进阶实现:用类(如果已经了解基础面向对象)
当你熟悉列表和字典后,用类可以让代码更结构化。我们可以把相关的数据和功能打包在一起,后续加新功能(比如修改订单、加税费)会更方便:
class OrderItem: """代表订单里的单个商品""" def __init__(self, name, price, quantity): self.name = name self.price = price self.quantity = quantity def get_item_total(self): return self.price * self.quantity class Order: """代表一个完整的客户订单""" def __init__(self, order_id, customer_name, items): self.order_id = order_id self.customer_name = customer_name self.items = items # OrderItem对象的列表 self.total_price = sum(item.get_item_total() for item in items) def print_order(self): print(f"\n订单ID: {self.order_id}") print(f"客户姓名: {self.customer_name}") print("商品明细:") for item in self.items: print(f"- {item.name} x{item.quantity} | 单价: {item.price}元") print(f"总价: {self.total_price}元") class OrderHistory: """管理所有订单历史""" def __init__(self): self.orders = [] def add_order(self, customer_name, items): order_id = len(self.orders) + 1 # 把商品字典转换成OrderItem对象 order_items = [OrderItem(item["name"], item["price"], item["quantity"]) for item in items] new_order = Order(order_id, customer_name, order_items) self.orders.append(new_order) print(f"订单 #{order_id} 创建成功!") def show_all_orders(self): if not self.orders: print("暂无订单历史!") return print("\n--- 订单历史 ---") for order in self.orders: order.print_order() # 测试类版本的系统 if __name__ == "__main__": history = OrderHistory() items1 = [ {"name": "经典汉堡", "price": 30, "quantity": 2}, {"name": "薯条", "price": 12, "quantity": 1} ] history.add_order("张三", items1) items2 = [ {"name": "素食沙拉", "price": 25, "quantity": 1}, {"name": "冰可乐", "price": 8, "quantity": 2} ] history.add_order("李四", items2) history.show_all_orders()
小扩展建议(适合练习)
- 添加用户输入:用
input()让用户直接在控制台输入姓名和订单商品,代替硬编码的示例数据。 - 保存到文件:用Python内置的
json模块把订单历史写入.json文件,这样程序关闭后数据不会丢失。 - 订单搜索:加一个函数,通过客户姓名筛选出对应的所有订单。
慢慢来,如果列表和字典的版本更熟悉就先从这个开始。等你觉得顺手了,再尝试类的版本。要是哪个环节卡住了,随时问我,我会帮你拆解清楚!
内容的提问来源于stack exchange,提问作者Muskan Muskan
相关产品推荐
相关产品推荐

