题目描述
某驿站商品库存管理系统,要求:
1.商品的信息(商品编号id、商品名称sname、商品库存量kcl、商品销售量xsl)
2.能够录入商品信息,能够显示所有商品信息,
3.能够按商品名称、商品类别等查询,可以查询输出库存量小于10的商品
4.能够按销售量或库存号进行排序并在屏幕上打印排序结果
5.能够添加、删除、修改(增加库存、商品销售)商品的信息
6.能够完成分类统计功能
源代码
class Product:
def __init__(self, id, name, category, stock, sales):
self.id = id
self.name = name
self.category = category
self.stock = stock
self.sales = sales
def display(self):
print(f"ID: {self.id}, Name: {self.name}, Category: {self.category}, Stock: {self.stock}, Sales: {self.sales}")
class Inventory:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def remove_product(self, product):
self.products.remove(product)
def update_product_stock(self, product, new_stock):
product.stock = new_stock
def update_product_sales(self, product, new_sales):
product.sales = new_sales
def sort_by_stock(self):
self.products = sorted(self.products, key=lambda p: p.stock)
def sort_by_sales(self):
self.products = sorted(self.products, key=lambda p: p.sales)
def search_by_name(self, name):
results = []
for product in self.products:
if product.name == name:
results.append(product)
return results
def search_by_category(self, category):
results = []
for product in self.products:
if product.category == category:
results.append(product)
return results
def filter_low_stock(self):
results = []
for product in self.products:
if product.stock < 10:
results.append(product)
return results
def summary_by_category(self):
summary = {}
for product in self.products:
if product.category not in summary:
summary[product.category] = {"count": 0, "stock": 0, "sales": 0}
summary[product.category]["count"] += 1
summary[product.category]["stock"] += product.stock
summary[product.category]["sales"] += product.sales
return summary
# 创建商品库存管理系统实例
inventory = Inventory()
# 添加商品信息
product1 = Product(1, "Apple", "Fruit", 50, 20)
product2 = Product(2, "Banana", "Fruit", 30, 10)
product3 = Product(3, "Carrot", "Vegetable", 20, 5)
inventory.add_product(product1)
inventory.add_product(product2)
inventory.add_product(product3)
# 显示所有商品信息
print("All Products:")
for product in inventory.products:
product.display()
# 查询商品信息
print("Search by Name: Apple")
results = inventory.search_by_name("Apple")
for product in results:
product.display()
print("Search by Category: Fruit")
results = inventory.search_by_category("Fruit")
for product in results:
product.display()
# 查询低库存商品信息
print("Low Stock Products:")
results = inventory.filter_low_stock()
for product in results:
product.display()
# 对商品按库存量排序
inventory.sort_by_stock()
print("Products Sorted by Stock:")
for product in inventory.products:
product.display()
# 对商品按销售量排序
inventory.sort_by_sales()
print("Products Sorted by Sales:")
for product in inventory.products:
product.display()
# 更新商品信息
inventory.update_product_stock(product1, 60)
inventory.update_product_sales(product1, 25)
# 删除商品信息
inventory.remove_product(product3)
# 统计每个类别的商品数量、库存量和销售量
summary = inventory.summary_by_category()
print("Summary by Category:")
for category, stats in summary.items():
print(f"Category: {category}, Count: {stats['count']}, Stock: {stats['stock']}, Sales: {stats['sales']}")
这个库存管理系统包含了商品类Product
和库存类Inventory
。其中,Product
类包含了商品的基本信息,如编号、名称、类别、库存量和销售量,Inventory
类则包含了对商品进行各种操作的方法。
在代码中,我们首先创建了一个Inventory
实例,然后添加了三个商品信息。接着,我们展示了如何显示所有商品信息,查询商品信息,查询低库存商品信息,对商品按库存量或销售量进行排序,更新商品信息以及删除商品信息。最后,我们还展示了如何对每个类别的商品进行统计。
© 版权声明
本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!
THE END