基于Python宠物用品库存管理系统开发
- 其他
- 2025-09-22 09:21:01

Python 宠物用品库存管理系统开发 一、项目背景与需求分析
在宠物行业蓬勃发展的当下,宠物用品店的商品种类繁多,库存管理变得尤为重要。为了提高管理效率、减少人为错误,我们可以开发一个宠物用品库存管理系统。该系统需要具备商品信息管理、库存数量管理、进货与销售记录等功能,同时要能够方便地查询和统计数据。
二、系统设计 2.1 功能模块设计 商品信息管理:包括商品的添加、删除、修改和查询。库存管理:实时更新商品的库存数量,处理进货和销售操作。记录管理:记录每次进货和销售的详细信息,包括日期、商品名称、数量、价格等。统计查询:可以根据不同条件查询商品信息、库存数量和交易记录。 2.2 数据结构设计 商品类(Product):包含商品的基本信息,如商品 ID、名称、价格、库存数量等。交易记录类(TransactionRecord):记录每次进货或销售的详细信息,包括交易 ID、日期、商品 ID、数量、交易类型(进货或销售)等。 三、代码实现 import datetime # 商品类 class Product: def __init__(self, product_id, name, price, stock): self.product_id = product_id self.name = name self.price = price self.stock = stock def update_stock(self, quantity): self.stock += quantity # 交易记录类 class TransactionRecord: def __init__(self, transaction_id, date, product_id, quantity, transaction_type): self.transaction_id = transaction_id self.date = date self.product_id = product_id self.quantity = quantity self.transaction_type = transaction_type # 宠物用品库存管理系统类 class PetSupplyInventorySystem: def __init__(self): self.products = {} self.transactions = {} self.next_product_id = 1 self.next_transaction_id = 1 # 添加商品 def add_product(self, name, price, stock): product_id = self.next_product_id product = Product(product_id, name, price, stock) self.products[product_id] = product self.next_product_id += 1 print(f"商品 {name} 已成功添加,商品 ID 为 {product_id}。") # 删除商品 def delete_product(self, product_id): if product_id in self.products: del self.products[product_id] print(f"商品 ID 为 {product_id} 的商品已成功删除。") else: print(f"未找到商品 ID 为 {product_id} 的商品。") # 修改商品信息 def update_product(self, product_id, name=None, price=None, stock=None): if product_id in self.products: product = self.products[product_id] if name: product.name = name if price: product.price = price if stock: product.stock = stock print(f"商品 ID 为 {product_id} 的商品信息已更新。") else: print(f"未找到商品 ID 为 {product_id} 的商品。") # 查询商品信息 def query_product(self, product_id): if product_id in self.products: product = self.products[product_id] print(f"商品 ID: {product.product_id}") print(f"商品名称: {product.name}") print(f"商品价格: {product.price}") print(f"商品库存: {product.stock}") else: print(f"未找到商品 ID 为 {product_id} 的商品。") # 进货操作 def purchase(self, product_id, quantity): if product_id in self.products: product = self.products[product_id] product.update_stock(quantity) transaction_id = self.next_transaction_id date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") transaction = TransactionRecord(transaction_id, date, product_id, quantity, "进货") self.transactions[transaction_id] = transaction self.next_transaction_id += 1 print(f"商品 ID 为 {product_id} 的商品进货 {quantity} 件成功。") else: print(f"未找到商品 ID 为 {product_id} 的商品。") # 销售操作 def sell(self, product_id, quantity): if product_id in self.products: product = self.products[product_id] if product.stock >= quantity: product.update_stock(-quantity) transaction_id = self.next_transaction_id date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") transaction = TransactionRecord(transaction_id, date, product_id, quantity, "销售") self.transactions[transaction_id] = transaction self.next_transaction_id += 1 print(f"商品 ID 为 {product_id} 的商品销售 {quantity} 件成功。") else: print(f"商品 ID 为 {product_id} 的商品库存不足,当前库存为 {product.stock} 件。") else: print(f"未找到商品 ID 为 {product_id} 的商品。") # 查询交易记录 def query_transaction(self, transaction_id): if transaction_id in self.transactions: transaction = self.transactions[transaction_id] print(f"交易 ID: {transaction.transaction_id}") print(f"交易日期: {transaction.date}") print(f"商品 ID: {transaction.product_id}") print(f"交易数量: {transaction.quantity}") print(f"交易类型: {transaction.transaction_type}") else: print(f"未找到交易 ID 为 {transaction_id} 的交易记录。") # 测试代码 if __name__ == "__main__": system = PetSupplyInventorySystem() # 添加商品 system.add_product("宠物狗粮", 50, 100) system.add_product("宠物猫砂", 30, 200) # 进货操作 system.purchase(1, 50) # 销售操作 system.sell(1, 20) # 查询商品信息 system.query_product(1) # 查询交易记录 system.query_transaction(1) 四、代码解释 4.1 Product 类该类表示商品,包含商品的基本信息,如商品 ID、名称、价格和库存数量。update_stock 方法用于更新商品的库存数量。
4.2 TransactionRecord 类该类表示交易记录,包含交易 ID、日期、商品 ID、数量和交易类型。
4.3 PetSupplyInventorySystem 类该类是宠物用品库存管理系统的核心类,包含以下主要方法:
add_product:添加新商品。delete_product:删除指定 ID 的商品。update_product:修改指定 ID 商品的信息。query_product:查询指定 ID 商品的信息。purchase:处理进货操作,更新商品库存并记录交易信息。sell:处理销售操作,更新商品库存并记录交易信息。query_transaction:查询指定 ID 的交易记录。 五、总结与扩展通过以上代码,我们实现了一个简单的宠物用品库存管理系统。该系统可以方便地管理商品信息、库存数量和交易记录。在实际应用中,我们可以进一步扩展系统功能,如添加用户认证、生成报表、设置库存预警等,以满足更复杂的业务需求。
基于Python宠物用品库存管理系统开发由讯客互联其他栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“基于Python宠物用品库存管理系统开发”