diff --git a/餐厅点餐系统 b/餐厅点餐系统 new file mode 100644 index 0000000..0a94932 --- /dev/null +++ b/餐厅点餐系统 @@ -0,0 +1,185 @@ +class Restaurant: + def __init__(self): + self.customers = [] # 所有顾客信息列表 + self.items = { # 所有菜品和价格字典 + '炒饭': 10, + '炒面': 12, + '炒鸡蛋': 5, + '牛肉面': 15, + '汉堡': 18, + '可乐': 3, + '雪碧': 3, + '果汁': 5 + } + + def register(self): + name = input('请输入用户名: ') + password = input('请设置密码: ') + + for customer in self.customers: + if customer['name'] == name: + print('该用户名已被注册') + return + + self.customers.append({ + 'name': name, + 'password': password, + 'cart': {} # 购物车初始化为空字典 + }) + + print('注册成功!') + + def login(self): + name = input('请输入用户名: ') + password = input('请输入密码: ') + + for customer in self.customers: + if customer['name'] == name and customer['password'] == password: + print('登录成功!') + self.current_user = customer + return + + print('用户名或密码错误!') + + def show_menu(self): + print('-' * 40) + print('{:<20}{:<10}'.format('菜品', '价格')) + print('-' * 40) + + for item, price in self.items.items(): + print('{:<20}{:<10}'.format(item, price)) + + print('-' * 40) + + def add_to_cart(self): + if not hasattr(self, 'current_user'): + print('您还未登录,登录后才能选择菜品!') + return + + item_name = input('请输入要加入购物车的菜品名称: ') + item_price = self.items.get(item_name) + + if not item_price: + print('抱歉,我们没有这道菜!') + return + + while True: + item_quantity = input('请输入添加到购物车的数量: ') + + if not item_quantity.isdigit() or int(item_quantity) <= 0: + print('输入有误,请输入一个正整数!') + else: + item_quantity = int(item_quantity) + break + + cart = self.current_user['cart'] + + if item_name in cart: + cart[item_name] += item_quantity + else: + cart[item_name] = item_quantity + + print('{0} x {1} 已经添加到购物车!'.format(item_name, item_quantity)) + + def show_cart(self): + if not hasattr(self, 'current_user'): + print('您还未登录,登录后才能查看购物车!') + return + + cart = self.current_user['cart'] + + if not cart: + print('购物车为空!') + return + + print('-' * 20) + print('{:<10}{:<10}'.format('菜品', '数量')) + print('-' * 20) + + for item, quantity in cart.items(): + print('{:<10}{:<10}'.format(item, quantity)) + + print('-' * 20) + + def remove_from_cart(self): + if not hasattr(self, 'current_user'): + print('您还未登录,登录后才能删除购物车中的菜品!') + return + + cart = self.current_user['cart'] + + if not cart: + print('购物车为空!') + return + + item_name = input('请输入要删除的菜品名称: ') + + if item_name not in cart: + print('购物车中没有这道菜!') + return + + del cart[item_name] + print('{0} 已经从购物车中移除!'.format(item_name)) + + def checkout(self): + if not hasattr(self, 'current_user'): + print('您还未登录,登录后才能进行结算!') + return + + cart = self.current_user['cart'] + + if not cart: + print('您还没选择任何菜品!') + return + + total_price = 0 + + for item, quantity in cart.items(): + item_price = self.items.get(item) + total_price += item_price * quantity + + print('-' * 20) + print('您的订单总价格为: {}'.format(total_price)) + print('-' * 20) + + # 结算完成后清空购物车 + self.current_user['cart'] = {} + + def run(self): + while True: + print('1.客户注册') + print('2.客户登录') + print('3.查看菜单') + print('4.添加菜品到购物车') + print('5.查看购物车') + print('6.删除购物车中的菜品') + print('7.进行结算') + print('8.退出') + + choice = input('请选择操作(输入数字): ') + + if choice == '1': + self.register() + elif choice == '2': + self.login() + elif choice == '3': + self.show_menu() + elif choice == '4': + self.add_to_cart() + elif choice == '5': + self.show_cart() + elif choice == '6': + self.remove_from_cart() + elif choice == '7': + self.checkout() + elif choice == '8': + break + else: + print('无效的选择,请重新输入!') + + +if __name__ == '__main__': + restaurant = Restaurant() + restaurant.run() + +