敲一个以上的代码
时间: 2024-02-12 10:04:35 浏览: 22
好的,以下是一个基于Python语言的库存管理系统的代码实现:
```python
import json
class Goods:
def __init__(self, id, name, origin, quantity, price, total_value):
self.id = id
self.name = name
self.origin = origin
self.quantity = quantity
self.price = price
self.total_value = total_value
def __str__(self):
return f"{self.id}\t{self.name}\t{self.origin}\t{self.quantity}\t{self.price}\t{self.total_value}"
class InventoryManagement:
def __init__(self):
self.goods_list = []
def load_goods(self):
with open("goods.json", "r") as f:
goods_data = json.load(f)
for goods in goods_data:
self.goods_list.append(Goods(**goods))
def save_goods(self):
with open("goods.json", "w") as f:
goods_data = [vars(goods) for goods in self.goods_list]
json.dump(goods_data, f)
def add_goods(self):
id = input("请输入产品编号:")
name = input("请输入产品名称:")
origin = input("请输入产地:")
quantity = int(input("请输入数量:"))
price = float(input("请输入价格:"))
total_value = quantity * price
goods = Goods(id, name, origin, quantity, price, total_value)
self.goods_list.append(goods)
def delete_goods(self):
id = input("请输入产品编号:")
for goods in self.goods_list:
if goods.id == id:
self.goods_list.remove(goods)
def edit_goods(self):
id = input("请输入产品编号:")
for goods in self.goods_list:
if goods.id == id:
goods.name = input("请输入产品名称:")
goods.origin = input("请输入产地:")
goods.quantity = int(input("请输入数量:"))
goods.price = float(input("请输入价格:"))
goods.total_value = goods.quantity * goods.price
def purchase_goods(self):
id = input("请输入产品编号:")
for goods in self.goods_list:
if goods.id == id:
goods.quantity += int(input("请输入进货数量:"))
goods.price = float(input("请输入进货价格:"))
goods.total_value = goods.quantity * goods.price
def sell_goods(self):
id = input("请输入产品编号:")
for goods in self.goods_list:
if goods.id == id:
quantity = int(input("请输入出货数量:"))
if quantity <= goods.quantity:
goods.quantity -= quantity
sell_price = float(input("请输入销售价格:"))
sell_value = quantity * sell_price
profit = sell_value - quantity * goods.price
print(f"销售额:{sell_value},利润:{profit}")
else:
print("库存不足")
def query_goods(self):
id = input("请输入产品编号:")
for goods in self.goods_list:
if goods.id == id:
print(goods)
def print_goods_list(self):
print("编号\t名称\t产地\t数量\t价格\t总价值")
for goods in self.goods_list:
print(goods)
def show_menu(self):
print("欢迎使用库存管理系统")
print("1. 添加商品")
print("2. 删除商品")
print("3. 修改商品")
print("4. 进货")
print("5. 出货")
print("6. 查找商品")
print("7. 显示所有商品")
print("0. 退出系统")
def run(self):
self.load_goods()
while True:
self.show_menu()
choice = input("请输入您的选择:")
if choice == "1":
self.add_goods()
elif choice == "2":
self.delete_goods()
elif choice == "3":
self.edit_goods()
elif choice == "4":
self.purchase_goods()
elif choice == "5":
self.sell_goods()
elif choice == "6":
self.query_goods()
elif choice == "7":
self.print_goods_list()
elif choice == "0":
self.save_goods()
print("谢谢使用,再见!")
break
else:
print("无效选择,请重新输入!")
if __name__ == "__main__":
inventory_management = InventoryManagement()
inventory_management.run()
```
以上代码实现了一个基于菜单的库存管理系统,使用了面向对象的编程方法,将商品类和库存管理类抽象成了两个类。程序具有添加、删除、修改、进货、出货、查询、显示所有商品等功能,同时将库存信息保存在外部文件中。