设计一个饮品自动售货系统,要求能实现商品添加、删除、修改、查询、计算消费全领等功能尽量在6月10之前
1条回答 默认 最新
- 语言-逆行者 2023-06-06 23:04关注
基于new bing的编写,这是结合文件的,文件随操作自动生成的:
import json class Product: def __init__(self, name, price, quantity): self.name = name self.price = price self.quantity = quantity class VendingMachine: def __init__(self): self.products = {} self.balance = 0 self.update_products_from_file() def add_product(self, product, quantity): """ 添加商品 """ if product in self.products: self.products[product].quantity += quantity else: self.products[product] = Product(product.name, product.price, quantity) self.save_products_to_file() def remove_product(self, product, quantity): """ 删除商品 """ if product not in self.products: print("该商品不存在") elif quantity > self.products[product].quantity: print("库存不足") else: self.products[product].quantity -= quantity self.save_products_to_file() def update_product(self, product, new_price): """ 修改商品信息 """ if product not in self.products: print("该商品不存在") else: self.products[product].price = new_price self.save_products_to_file() def find_product(self, product_name): """ 查询商品信息 """ for product in self.products.values(): if product.name == product_name: return product print("该商品不存在") def display_products(self): """ 展示所有商品信息 """ for product in self.products.values(): print(f"商品名:{product.name},价格:{product.price}元,数量:{product.quantity}") def insert_money(self, amount): """ 插入金额 """ self.balance += amount def buy_product(self, product_name): """ 购买商品 """ product = self.find_product(product_name) if product: if self.balance >= product.price: self.balance -= product.price product.quantity -= 1 print(f"购买成功,找零{self.balance}元") self.save_products_to_file() else: print("余额不足,请先充值") else: print("该商品不存在") def calculate_total_revenue(self): """ 计算总收入 """ revenue = 0 for product in self.products.values(): revenue += product.price * product.quantity return revenue def save_products_to_file(self): with open('products.json', 'w') as f: json.dump([product.__dict__ for product in self.products.values()], f) def update_products_from_file(self): try: with open('products.json', 'r') as f: products_data = json.load(f) for product_data in products_data: product = Product(product_data['name'], product_data['price'], product_data['quantity']) self.products[product] = product except FileNotFoundError: pass vm = VendingMachine() while True: print("\n**********欢迎使用自动售货机**********") print("请选择操作类型:") print("1. 添加商品") print("2. 删除商品") print("3. 修改商品信息") print("4. 查询商品信息") print("5. 展示所有商品信息") print("6. 插入金额") print("7. 购买商品") print("8. 计算总收入") print("9. 退出系统") choice = input("请输入操作编号:") if choice == "1": name = input("请输入商品名称:") price = float(input("请输入商品价格:")) quantity = int(input("请输入商品数量:")) product = Product(name, price, quantity) vm.add_product(product, quantity) elif choice == "2": name = input("请输入商品名称:") quantity = int(input("请输入商品数量:")) product = vm.find_product(name) if product: vm.remove_product(product, quantity) elif choice == "3": name = input("请输入商品名称:") new_price = float(input("请输入新的商品价格:")) product = vm.find_product(name) if product: vm.update_product(product, new_price) elif choice == "4": name = input("请输入商品名称:") vm.find_product(name) elif choice == "5": vm.display_products() elif choice == "6": amount = float(input("请输入插入金额:")) vm.insert_money(amount) elif choice == "7": name = input("请输入要购买的商品名称:") vm.buy_product(name) elif choice == "8": revenue = vm.calculate_total_revenue() print(f"总收入:{revenue}元") elif choice == "9": print("谢谢使用,再见!") break else: print("输入有误,请重新输入!")
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 stata安慰剂检验作图但是真实值不出现在图上
- ¥15 c程序不知道为什么得不到结果
- ¥40 复杂的限制性的商函数处理
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题