下面是一个简单的工厂库存管理软件的Python代码示例:
import json
# 初始化库存数据
inventory = {
"products": [
{"id": 1, "name": "Product 1", "quantity": 10},
{"id": 2, "name": "Product 2", "quantity": 5},
{"id": 3, "name": "Product 3", "quantity": 3}
]
}
# 保存库存数据到文件
def save_inventory():
with open("inventory.json", "w") as file:
json.dump(inventory, file)
# 加载库存数据
def load_inventory():
global inventory
try:
with open("inventory.json", "r") as file:
inventory = json.load(file)
except FileNotFoundError:
save_inventory()
# 显示库存
def show_inventory():
print("Current Inventory:")
for product in inventory["products"]:
print(f"ID: {product['id']}, Name: {product['name']}, Quantity: {product['quantity']}")
# 添加产品
def add_product():
id = int(input("Enter product ID: "))
name = input("Enter product name: ")
quantity = int(input("Enter product quantity: "))
product = {"id": id, "name": name, "quantity": quantity}
inventory["products"].append(product)
save_inventory()
print("Product added successfully.")
# 更新产品数量
def update_quantity():
id = int(input("Enter product ID: "))
quantity = int(input("Enter new quantity: "))
for product in inventory["products"]:
if product["id"] == id:
product["quantity"] = quantity
save_inventory()
print("Quantity updated successfully.")
return
print("Product not found.")
# 删除产品
def delete_product():
id = int(input("Enter product ID: "))
for product in inventory["products"]:
if product["id"] == id:
inventory["products"].remove(product)
save_inventory()
print("Product deleted successfully.")
return
print("Product not found.")
# 主菜单
def main_menu():
while True:
print("\n===== Inventory Management =====")
print("1. Show Inventory")
print("2. Add Product")
print("3. Update Quantity")
print("4. Delete Product")
print("0. Exit")
choice = input("Enter your choice: ")
if choice == "1":
show_inventory()
elif choice == "2":
add_product()
elif choice == "3":
update_quantity()
elif choice == "4":
delete_product()
elif choice == "0":
break
else:
print("Invalid choice. Please try again.")
# 加载库存数据
load_inventory()
# 运行主菜单
main_menu()
这个代码示例实现了一个简单的工厂库存管理软件,包括显示库存、添加产品、更新产品数量和删除产品等功能。库存数据以JSON格式保存在文件中,并在程序启动时加载和保存。用户可以通过主菜单选择不同的操作。
标签:product,python,Product,管理软件,inventory,print,id,模拟,quantity From: https://blog.51cto.com/u_16055028/7253413