编写一个五金管理系统的代码可以涉及多个方面,具体取决于你希望实现的功能。以下是一个简单的Python示例,展示如何创建一个基本的五金管理系统。这个系统可以添加、查看、搜索和删除五金产品的信息。 示例代码
python
- class HardwareItem:
- def __init__(self, name, category, price, quantity):
- self.name = name
- self.category = category
- self.price = price
- self.quantity = quantity
- def __str__(self):
- return f"{self.name} ({self.category}) - ${self.price} - Qty: {self.quantity}"
- class HardwareInventory:
- def __init__(self):
- self.inventory = []
- def add_item(self, item):
- self.inventory.append(item)
- print(f"Added {item.name} to inventory.")
- def view_inventory(self):
- if not self.inventory:
- print("Inventory is empty.")
- else:
- for item in self.inventory:
- print(item)
- def search_item(self, name):
- for item in self.inventory:
- if item.name.lower() == name.lower():
- print(item)
- return
- print(f"Item '{name}' not found in inventory.")
- def remove_item(self, name):
- for item in self.inventory:
- if item.name.lower() == name.lower():
- self.inventory.remove(item)
- print(f"Removed {item.name} from inventory.")
- return
- print(f"Item '{name}' not found in inventory.")
- def main():
- inventory = HardwareInventory()
- while True:
- print("\nHardware Inventory Management")
- print("1. Add Item")
- print("2. View Inventory")
- print("3. Search Item")
- print("4. Remove Item")
- print("5. Exit")
- choice = input("Enter your choice: ")
- if choice == '1':
- name = input("Enter item name: ")
- category = input("Enter item category: ")
- price = float(input("Enter item price: "))
- quantity = int(input("Enter item quantity: "))
- item = HardwareItem(name, category, price, quantity)
- inventory.add_item(item)
- elif choice == '2':
- inventory.view_inventory()
- elif choice == '3':
- name = input("Enter item name to search: ")
- inventory.search_item(name)
- elif choice == '4':
- name = input("Enter item name to remove: ")
- inventory.remove_item(name)
- elif choice == '5':
- print("Exiting the system.")
- break
- else:
- print("Invalid choice. Please try again.")
- if __name__ == "__main__":
- main()
复制代码
功能说明使用方法扩展功能你可以根据需要扩展这个系统,例如: 将库存数据保存到文件或数据库中。 添加更多的五金产品属性(如品牌、型号等)。 实现库存数量的自动更新。 添加用户权限管理功能。
|