赞
踩
以下是一个简单的仓库管理系统的示例代码,使用Python编写:
-
- class Item:
- def __init__(self, name, quantity):
- self.name = name
- self.quantity = quantity
-
- class Warehouse:
- def __init__(self):
- self.items = []
-
- def add_item(self, item):
- self.items.append(item)
-
- def remove_item(self, item):
- self.items.remove(item)
-
- def update_item(self, item, quantity):
- item.quantity = quantity
-
- def list_items(self):
- for item in self.items:
- print(f"{item.name}: {item.quantity}")
-
- warehouse = Warehouse()
-
- while True:
- print("1. Add item")
- print("2. Remove item")
- print("3. Update item quantity")
- print("4. List items")
- print("5. Exit")
-
- choice = input("Enter your choice: ")
-
- if choice == "1":
- name = input("Enter item name: ")
- quantity = int(input("Enter quantity: "))
- item = Item(name, quantity)
- warehouse.add_item(item)
- print("Item added successfully.")
- elif choice == "2":
- name = input("Enter item name: ")
- for item in warehouse.items:
- if item.name == name:
- warehouse.remove_item(item)
- print("Item removed successfully.")
- break
- else:
- print("Item not found.")
- elif choice == "3":
- name = input("Enter item name: ")
- for item in warehouse.items:
- if item.name == name:
- quantity = int(input("Enter new quantity: "))
- warehouse.update_item(item, quantity)
- print("Item quantity updated successfully.")
- break
- else:
- print("Item not found.")
- elif choice == "4":
- print("Items in warehouse:")
- warehouse.list_items()
- elif choice == "5":
- print("Exiting...")
- break
- else:
- print("Invalid choice. Please try again.")
这个仓库管理系统使用了面向对象编程的思想,定义了两个类 `Item` 和 `Warehouse`,分别表示仓库中的物品和仓库本身。`Warehouse` 类包含了添加、删除、更新和列出物品等方法。程序主要逻辑是一个无限循环,每次循环根据用户的选择执行相应的操作。用户可以通过输入数字选择要执行的操作,比如添加物品、删除物品、更新物品数量或者列出仓库中的所有物品。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。