用pathon写一个商品管理系统
时间: 2024-04-07 18:31:20 浏览: 186
好的,一个商品管理系统需要涉及以下功能:
1. 商品的添加、修改、删除
2. 商品信息的查询和展示
3. 商品库存的管理
4. 商品销售记录的管理
以下是一个简单的示例代码:
```python
class Product:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
class Inventory:
def __init__(self):
self.product_list = []
def add_product(self, product):
self.product_list.append(product)
def remove_product(self, product):
self.product_list.remove(product)
def get_product(self, product_name):
for product in self.product_list:
if product.name == product_name:
return product
return None
def display_products(self):
for product in self.product_list:
print(f"{product.name} - Price: {product.price} - Quantity: {product.quantity}")
class SalesRecord:
def __init__(self, product, quantity):
self.product = product
self.quantity = quantity
class SalesHistory:
def __init__(self):
self.sales_list = []
def add_sale(self, sale):
self.sales_list.append(sale)
def display_sales(self):
for sale in self.sales_list:
print(f"{sale.product.name} - Quantity: {sale.quantity}")
inventory = Inventory()
sales_history = SalesHistory()
while True:
print("1. Add product")
print("2. Remove product")
print("3. Get product")
print("4. Display products")
print("5. Sell product")
print("6. Display sales history")
print("7. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter product name: ")
price = float(input("Enter product price: "))
quantity = int(input("Enter product quantity: "))
product = Product(name, price, quantity)
inventory.add_product(product)
elif choice == 2:
name = input("Enter product name: ")
product = inventory.get_product(name)
if product:
inventory.remove_product(product)
print(f"{name} has been removed from inventory.")
else:
print(f"{name} is not in inventory.")
elif choice == 3:
name = input("Enter product name: ")
product = inventory.get_product(name)
if product:
print(f"{product.name} - Price: {product.price} - Quantity: {product.quantity}")
else:
print(f"{name} is not in inventory.")
elif choice == 4:
inventory.display_products()
elif choice == 5:
name = input("Enter product name: ")
product = inventory.get_product(name)
if product:
quantity = int(input("Enter quantity: "))
if product.quantity >= quantity:
product.quantity -= quantity
sales_record = SalesRecord(product, quantity)
sales_history.add_sale(sales_record)
print(f"{quantity} {name}(s) sold.")
else:
print("Not enough stock.")
else:
print(f"{name} is not in inventory.")
elif choice == 6:
sales_history.display_sales()
elif choice == 7:
break
```
这里使用了面向对象的编程思想,将商品、库存、销售记录分别定义为类,并且通过一个循环菜单来实现用户与系统的交互。具体实现可以根据需求进行修改和扩展,这里只是提供一个简单的示例。
阅读全文