python代码模拟一个工厂的库存管理软件
  X5zJxoD00Cah 2023年11月02日 86 0


下面是一个简单的工厂库存管理软件的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格式保存在文件中,并在程序启动时加载和保存。用户可以通过主菜单选择不同的操作。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  gBkHYLY8jvYd   2023年12月09日   30   0   0 cii++数据
X5zJxoD00Cah