3
社区成员




import datetime
class CrispyDateSystem:
def __init__(self):
self.inventory = {
"原味脆枣": 100,
"蜂蜜脆枣": 80,
"椒盐脆枣": 60,
"紫薯脆枣": 50
}
self.sales_records = []
self.profit = 0.0
def display_inventory(self):
"""显示当前库存"""
print("\n=== 当前脆枣库存 ===")
for product, quantity in self.inventory.items():
print(f"{product}: {quantity}包")
print("===================")
def sell_product(self, product_name, quantity, price_per_pack):
"""销售脆枣"""
if product_name not in self.inventory:
print(f"错误: {product_name} 不在库存中!")
return False
if self.inventory[product_name] < quantity:
print(f"错误: {product_name} 库存不足! 当前库存: {self.inventory[product_name]}")
return False
# 更新库存
self.inventory[product_name] -= quantity
# 记录销售
total_price = quantity * price_per_pack
self.profit += total_price
sale_record = {
"product": product_name,
"quantity": quantity,
"price_per_pack": price_per_pack,
"total_price": total_price,
"date": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
self.sales_records.append(sale_record)
print(f"成功销售 {quantity} 包 {product_name}, 总价: {total_price}元")
return True
def display_sales_records(self):
"""显示销售记录"""
print("\n=== 销售记录 ===")
if not self.sales_records:
print("暂无销售记录")
return
for record in self.sales_records:
print(f"{record['date']} - {record['product']}: {record['quantity']}包 x {record['price_per_pack']}元 = {record['total_price']}元")
print("===============")
def display_profit(self):
"""显示总利润"""
print(f"\n当前总利润: {self.profit:.2f}元")
def restock(self, product_name, quantity):
"""补货"""
if product_name not in self.inventory:
print(f"错误: {product_name} 不在库存中!")
return False
self.inventory[product_name] += quantity
print(f"成功补货 {product_name} {quantity}包")
return True
# 示例使用
if __name__ == "__main__":
system = CrispyDateSystem()
while True:
print("\n=== 脆枣管理系统 ===")
print("1. 查看库存")
print("2. 销售脆枣")
print("3. 补货")
print("4. 查看销售记录")
print("5. 查看总利润")
print("6. 退出")
choice = input("请选择操作(1-6): ")
if choice == "1":
system.display_inventory()
elif choice == "2":
product = input("输入产品名称: ")
qty = int(input("输入销售数量: "))
price = float(input("输入单价(元): "))
system.sell_product(product, qty, price)
elif choice == "3":
product = input("输入产品名称: ")
qty = int(input("输入补货数量: "))
system.restock(product, qty)
elif choice == "4":
system.display_sales_records()
elif choice == "5":
system.display_profit()
elif choice == "6":
print("退出系统")
break
else:
print("无效选择,请重新输入")