基于Python设计实现一个商品管理系统,商品需具有商品编号,商品名称,商品类型,商品价格,生产公司,库存等信息。
时间: 2024-03-05 07:52:37 浏览: 10
好的,我来为你提供一个基于Python设计实现的商品管理系统,满足你的需求。具体如下:
```
class Product:
def __init__(self, id, name, type, price, company, stock):
self.id = id
self.name = name
self.type = type
self.price = price
self.company = company
self.stock = stock
class Inventory:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def remove_product(self, id):
for product in self.products:
if product.id == id:
self.products.remove(product)
return True
return False
def update_product(self, id, name, type, price, company, stock):
for product in self.products:
if product.id == id:
product.name = name
product.type = type
product.price = price
product.company = company
product.stock = stock
return True
return False
def search_product(self, id):
for product in self.products:
if product.id == id:
return product
return None
def view_inventory(self):
for product in self.products:
print(f"ID: {product.id}, Name: {product.name}, Type: {product.type}, Price: {product.price}, Company: {product.company}, Stock: {product.stock}")
class Menu:
def __init__(self):
self.inventory = Inventory()
def add_product(self):
id = int(input("Enter product ID: "))
name = input("Enter product name: ")
type = input("Enter product type: ")
price = float(input("Enter product price: "))
company = input("Enter product company: ")
stock = int(input("Enter product stock: "))
product = Product(id, name, type, price, company, stock)
self.inventory.add_product(product)
print("Product added successfully!")
def remove_product(self):
id = int(input("Enter product ID: "))
if self.inventory.remove_product(id):
print("Product removed successfully!")
else:
print("Product not found!")
def update_product(self):
id = int(input("Enter product ID: "))
name = input("Enter product name: ")
type = input("Enter product type: ")
price = float(input("Enter product price: "))
company = input("Enter product company: ")
stock = int(input("Enter product stock: "))
if self.inventory.update_product(id, name, type, price, company, stock):
print("Product updated successfully!")
else:
print("Product not found!")
def search_product(self):
id = int(input("Enter product ID: "))
product = self.inventory.search_product(id)
if product:
print(f"ID: {product.id}, Name: {product.name}, Type: {product.type}, Price: {product.price}, Company: {product.company}, Stock: {product.stock}")
else:
print("Product not found!")
def view_inventory(self):
self.inventory.view_inventory()
def show_menu(self):
while True:
print("\n1. Add product")
print("2. Remove product")
print("3. Update product")
print("4. Search product")
print("5. View inventory")
print("6. Exit")
choice = input("Enter your choice: ")
if choice == "1":
self.add_product()
elif choice == "2":
self.remove_product()
elif choice == "3":
self.update_product()
elif choice == "4":
self.search_product()
elif choice == "5":
self.view_inventory()
elif choice == "6":
break
else:
print("Invalid choice!")
menu = Menu()
menu.show_menu()
```
以上代码实现了一个商品管理系统,包括商品的添加、删除、修改、查询和查看库存等功能,你可以根据自己的需要进行修改和扩展。