进销存系统是一个基本的商业管理系统,用于跟踪库存、销售和采购活动。以下是一个简单的进销存系统的Python实现,这个系统包括商品管理、采购入库、销售出库以及库存查询的功能。
首先,我们定义一个Product类来表示商品:
python复制代码
| class Product:  | |
| def __init__(self, id, name, price, stock):  | |
| self.id = id  | |
| self.name = name  | |
| self.price = price  | |
| self.stock = stock  | |
| def __str__(self):  | |
| return f"Product(id={self.id}, name={self.name}, price={self.price}, stock={self.stock})" | 
然后,我们定义一个InventorySystem类来表示进销存系统:
python复制代码
| class InventorySystem:  | |
| def __init__(self):  | |
| self.products = {}  | |
| def add_product(self, product):  | |
| if product.id in self.products:  | |
| print("Product already exists.")  | |
| return  | |
| self.products[product.id] = product  | |
| print(f"Added product: {product}")  | |
| def purchase_product(self, product_id, quantity):  | |
| if product_id not in self.products:  | |
| print("Product does not exist.")  | |
| return  | |
| product = self.products[product_id]  | |
| if quantity < 0:  | |
| print("Quantity cannot be negative.")  | |
| return  | |
| product.stock += quantity  | |
| print(f"Purchased {quantity} of {product.name}. New stock: {product.stock}")  | |
| def sell_product(self, product_id, quantity):  | |
| if product_id not in self.products:  | |
| print("Product does not exist.")  | |
| return  | |
| product = self.products[product_id]  | |
| if quantity < 0:  | |
| print("Quantity cannot be negative.")  | |
| return  | |
| if quantity > product.stock:  | |
| print("Insufficient stock.")  | |
| return  | |
| product.stock -= quantity  | |
| print(f"Sold {quantity} of {product.name}. Remaining stock: {product.stock}")  | |
| def get_product_stock(self, product_id):  | |
| if product_id not in self.products:  | |
| print("Product does not exist.")  | |
| return  | |
| product = self.products[product_id]  | |
| print(f"Stock of {product.name}: {product.stock}") | 
现在,我们可以使用这个进销存系统了:
python复制代码
| # 创建进销存系统实例  | |
| inventory_system = InventorySystem()  | |
| # 添加商品  | |
| product1 = Product(1, "Laptop", 1000, 10)  | |
| inventory_system.add_product(product1)  | |
| # 采购商品  | |
| inventory_system.purchase_product(1, 5)  | |
| # 销售商品  | |
| inventory_system.sell_product(1, 3)  | |
| # 查询库存  | |
| inventory_system.get_product_stock(1) | 
这个简单的进销存系统只提供了最基本的功能,并且没有进行数据库存储,所以每次程序重启后数据都会丢失。在实际应用中,你可能需要添加更多的功能,比如支持多个仓库、支持商品分类、支持用户权限管理、使用数据库进行持久化存储等。同时,你也需要考虑并发控制和数据一致性等问题。