求一个饮料自动售卖系统

设计一个饮品自动售货系统,要求能实现商品添加、删除、修改、查询、计算消费全领等功能尽量在6月10之前

基于new bing的编写,这是结合文件的,文件随操作自动生成的:

img

img

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("输入有误,请重新输入!")