python
class MilkCandySoftware:
def init(self):
# 初始化奶糖库存和原材料self.inventory = {"plain_milk_candy": 50,"strawberry_milk_candy": 30,"chocolate_milk_candy": 20}self.ingredients = {"milk_powder": 100, # 单位:克"sugar": 80,"strawberry_flavor": 50,"chocolate_powder": 40}self.prices = {"plain_milk_candy": 0.5,"strawberry_milk_candy": 0.7,"chocolate_milk_candy": 0.8}def display_menu(self):"""显示主菜单"""print("\n=== 奶糖管理系统 ===")print("1. 查看库存")print("2. 制作奶糖")print("3. 销售奶糖")print("4. 查看原材料")print("5. 退出系统")def display_inventory(self):"""显示奶糖库存"""print("\n=== 奶糖库存 ===")for candy, quantity in self.inventory.items():print(f"{candy.replace('_', ' ').title()}: {quantity} 个")def display_ingredients(self):"""显示原材料库存"""print("\n=== 原材料库存 ===")for ingredient, quantity in self.ingredients.items():print(f"{ingredient.replace('_', ' ').title()}: {quantity} 克")def make_candy(self, candy_type, quantity):"""制作奶糖"""# 定义每种奶糖所需的原材料recipes = {"plain_milk_candy": {"milk_powder": 10, "sugar": 5},"strawberry_milk_candy": {"milk_powder": 12, "sugar": 6, "strawberry_flavor": 3},"chocolate_milk_candy": {"milk_powder": 15, "sugar": 7, "chocolate_powder": 4}}if candy_type not in recipes:print("无效的奶糖类型!")return Falserecipe = recipes[candy_type]# 检查原材料是否足够for ingredient, needed in recipe.items():if self.ingredients[ingredient] < needed * quantity:print(f"原材料不足!需要 {needed*quantity} 克 {ingredient.replace('_', ' ').title()},但只有 {self.ingredients[ingredient]} 克")return False# 扣除原材料for ingredient, needed in recipe.items():self.ingredients[ingredient] -= needed * quantity# 增加奶糖库存self.inventory[candy_type] += quantityprint(f"成功制作 {quantity} 个 {candy_type.replace('_', ' ').title()}!")return Truedef sell_candy(self, candy_type, quantity):"""销售奶糖"""if candy_type not in self.inventory:print("无效的奶糖类型!")return Falseif self.inventory[candy_type] < quantity:print(f"库存不足!只有 {self.inventory[candy_type]} 个 {candy_type.replace('_', ' ').title()}")return Falseself.inventory[candy_type] -= quantitytotal_price = self.prices[candy_type] * quantityprint(f"成功销售 {quantity} 个 {candy_type.replace('_', ' ').title()},收入 {total_price:.2f} 元")return Truedef run(self):"""运行主程序"""while True:self.display_menu()choice = input("请选择操作 (1-5): ")if choice == "1":self.display_inventory()elif choice == "2":print("\n=== 制作奶糖 ===")print("可选类型: plain_milk_candy, strawberry_milk_candy, chocolate_milk_candy")candy_type = input("请输入奶糖类型: ").lower()try:quantity = int(input("请输入制作数量: "))if quantity <= 0:print("数量必须大于0!")continueself.make_candy(candy_type, quantity)except ValueError:print("请输入有效的数字!")elif choice == "3":print("\n=== 销售奶糖 ===")print("可选类型: plain_milk_candy, strawberry_milk_candy, chocolate_milk_candy")candy_type = input("请输入奶糖类型: ").lower()try:quantity = int(input("请输入销售数量: "))if quantity <= 0:print("数量必须大于0!")continueself.sell_candy(candy_type, quantity)except ValueError:print("请输入有效的数字!")elif choice == "4":self.display_ingredients()elif choice == "5":print("感谢使用奶糖管理系统,再见!")breakelse:print("无效的选择,请重新输入!")
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号