库存管理系统代码

编写库存管理系统的代码是一个相对庞大而复杂的任务,因此无法在单个回答中提供完整的代码。然而,我可以为你提供一个简单的库存管理系统的示例,以供参考。

python
class Product: def __init__(self, product_id, name, price, quantity): self.product_id = product_id self.name = name self.price = price self.quantity = quantity class InventoryManagementSystem: def __init__(self): self.products = [] def add_product(self, product): self.products.append(product) def remove_product(self, product_id): for product in self.products: if product.product_id == product_id: self.products.remove(product) break def update_quantity(self, product_id, quantity): for product in self.products: if product.product_id == product_id: product.quantity = quantity break def display_inventory(self): for product in self.products: print(f"Product ID: {product.product_id}, Name: {product.name}, Price: ${product.price}, Quantity: {product.quantity}") # 示例用法 inventory_system = InventoryManagementSystem() # 添加产品 product1 = Product(1, "Laptop", 800, 10) product2 = Product(2, "Printer", 150, 5) inventory_system.add_product(product1) inventory_system.add_product(product2) # 显示库存 print("Initial Inventory:") inventory_system.display_inventory() # 更新库存数量 inventory_system.update_quantity(1, 8) # 显示更新后的库存 print("\nUpdated Inventory:") inventory_system.display_inventory() # 移除产品 inventory_system.remove_product(2) # 显示最终库存 print("\nFinal Inventory:") inventory_system.display_inventory()

为了

python
import sqlite3 class Product: def __init__(self, product_id, name, price, quantity): self.product_id = product_id self.name = name self.price = price self.quantity = quantity class InventoryManagementSystem: def __init__(self, db_name="inventory.db"): self.connection = sqlite3.connect(db_name) self.cursor = self.connection.cursor() self._create_table() def _create_table(self): self.cursor.execute(''' CREATE TABLE IF NOT EXISTS products ( product_id INTEGER PRIMARY KEY, name TEXT, price REAL, quantity INTEGER ) ''') self.connection.commit() def add_product(self, product): self.cursor.execute(''' INSERT INTO products (product_id, name, price, quantity) VALUES (?, ?, ?, ?) ''', (product.product_id, product.name, product.price, product.quantity)) self.connection.commit() def remove_product(self, product_id): self.cursor.execute('DELETE FROM products WHERE product_id = ?', (product_id,)) self.connection.commit() def update_quantity(self, product_id, quantity): self.cursor.execute('UPDATE products SET quantity = ? WHERE product_id = ?', (quantity, product_id)) self.connection.commit() def display_inventory(self): self.cursor.execute('SELECT * FROM products') products = self.cursor.fetchall() for product in products: print(f"Product ID: {product[0]}, Name: {product[1]}, Price: ${product[2]}, Quantity: {product[3]}") def close_connection(self): self.connection.close() # 示例用法 inventory_system = InventoryManagementSystem() # 添加产品 product1 = Product(1, "Laptop", 800, 10) product2 = Product(2, "Printer", 150, 5) inventory_system.add_product(product1) inventory_system.add_product(product2) # 显示库存 print("Initial Inventory:") inventory_system.display_inventory() # 更新库存数量 inventory_system.update_quantity(1, 8) # 显示更新后的库存 print("\nUpdated Inventory:") inventory_system.display_inventory() # 移除产品 inventory_system.remove_product(2) # 显示最终库存 print("\nFinal Inventory:") inventory_system.display_inventory() # 关闭数据库连接 inventory_system.close_connection()

这个示例使用了SQLite数据库来持久化存储产品信息。实际应用中可能需要更复杂的数据库模型、异常处理、用户交互等。此示例主要用于说明如何使用数据库支持来改进库存管理系统的持久性。