# Python怎么制作一個簡易的點菜系統
## 一、項目概述
在餐飲行業中,一個高效的點菜系統能顯著提升運營效率。本文將使用Python開發一個基于控制臺的簡易點菜系統,包含以下核心功能:
1. 菜品管理(添加/刪除/修改)
2. 顧客點餐功能
3. 訂單計算與賬單生成
4. 簡單數據持久化存儲
## 二、技術選型
```python
# 主要技術棧
import json # 數據存儲
from tabulate import tabulate # 表格展示
import os # 文件操作
menu = {
"001": {"name": "魚香肉絲", "price": 38, "category": "熱菜"},
"002": {"name": "宮保雞丁", "price": 42, "category": "熱菜"}
}
orders = {
"table1": [
{"dish_id": "001", "quantity": 2},
{"dish_id": "002", "quantity": 1}
]
}
menu_manager.py
- 菜品管理order_system.py
- 點餐邏輯bill_generator.py
- 賬單生成main.py
- 主程序入口def load_menu():
"""從文件加載菜單數據"""
if os.path.exists("menu.json"):
with open("menu.json", "r") as f:
return json.load(f)
return {}
def save_menu(menu_data):
"""保存菜單到文件"""
with open("menu.json", "w") as f:
json.dump(menu_data, f, indent=4)
def add_dish(menu_data):
"""添加新菜品"""
dish_id = input("輸入菜品編號: ").strip()
if dish_id in menu_data:
print("該編號已存在!")
return
name = input("菜品名稱: ").strip()
price = float(input("價格: "))
category = input("分類: ").strip()
menu_data[dish_id] = {
"name": name,
"price": price,
"category": category
}
save_menu(menu_data)
print("添加成功!")
def take_order(menu_data):
"""處理顧客點餐"""
table_id = input("請輸入桌號: ")
current_order = []
while True:
print("\n當前菜單:")
display_menu(menu_data)
dish_id = input("輸入菜品編號(輸入q結束): ").strip()
if dish_id.lower() == 'q':
break
if dish_id not in menu_data:
print("無效的菜品編號!")
continue
quantity = int(input("輸入數量: "))
current_order.append({
"dish_id": dish_id,
"quantity": quantity
})
if current_order:
save_order(table_id, current_order)
print(f"桌號{table_id}點餐完成!")
def save_order(table_id, order_items):
"""保存訂單數據"""
orders = load_orders()
orders[table_id] = order_items
with open("orders.json", "w") as f:
json.dump(orders, f, indent=4)
def generate_bill(table_id):
"""生成指定桌號的賬單"""
orders = load_orders()
menu = load_menu()
if table_id not in orders:
print("該桌號無訂單記錄!")
return
print(f"\n==== 桌號 {table_id} 賬單 ====")
total = 0
bill_data = []
for item in orders[table_id]:
dish = menu[item["dish_id"]]
subtotal = dish["price"] * item["quantity"]
bill_data.append([
dish["name"],
dish["price"],
item["quantity"],
subtotal
])
total += subtotal
print(tabulate(bill_data,
headers=["菜品", "單價", "數量", "小計"],
tablefmt="grid"))
print(f"\n總計: ¥{total:.2f}")
print("="*30)
def main():
menu_data = load_menu()
while True:
print("\n=== 簡易點菜系統 ===")
print("1. 菜品管理")
print("2. 顧客點餐")
print("3. 生成賬單")
print("4. 退出系統")
choice = input("請選擇操作: ").strip()
if choice == "1":
manage_menu(menu_data)
elif choice == "2":
take_order(menu_data)
elif choice == "3":
table_id = input("輸入桌號: ")
generate_bill(table_id)
elif choice == "4":
print("感謝使用!")
break
else:
print("無效輸入!")
if __name__ == "__main__":
main()
# 測試添加菜品
add_dish({
"003": {
"name": "麻婆豆腐",
"price": 28,
"category": "熱菜"
}
})
# 測試點餐流程
test_order = {
"table5": [
{"dish_id": "001", "quantity": 1},
{"dish_id": "003", "quantity": 2}
]
}
save_order("table5", test_order["table5"])
# 驗證賬單生成
generate_bill("table5")
# Tkinter示例
from tkinter import *
root = Tk()
Label(root, text="菜品管理").pack()
root.mainloop()
# SQLite示例
import sqlite3
conn = sqlite3.connect('restaurant.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS menu (id TEXT PRIMARY KEY, name TEXT, price REAL)")
# Flask示例
from flask import Flask
app = Flask(__name__)
@app.route('/menu')
def show_menu():
return jsonify(load_menu())
通過本項目的開發,我們實現了:
完整代碼已托管至GitHub(示例倉庫地址),包含詳細的注釋和README使用說明。
附錄:完整代碼結構
restaurant_system/
├── data/
│ ├── menu.json
│ └── orders.json
├── modules/
│ ├── __init__.py
│ ├── menu_manager.py
│ ├── order_system.py
│ └── bill_generator.py
├── main.py
└── requirements.txt
安裝依賴:pip install tabulate
“`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。