设计一组程序,需要具备一下功能:1.输入商品编号,名字,类型,价格。2.输入商品的生产公司,商品的数量。3.可添加,删除商品。4.可查询商品名称。5.可进货,具备进货商品数量。
时间: 2024-03-20 14:40:58 浏览: 21
好的,我可以帮您设计这个程序。这个程序可以使用面向对象编程的方式实现,需要设计以下几个类:
1. 商品类(Product):包含商品编号、名称、类型、价格、生产公司和数量等属性。同时需要实现添加、删除、查询商品、进货等操作方法。
2. 商品管理类(ProductManager):负责管理所有商品对象,包含商品的增删改查等操作方法。
3. 用户界面类(UserInterface):提供用户界面,接收用户输入,并调用商品管理类的相关方法进行操作。
下面是一个简单的实现示例:
```python
class Product:
def __init__(self, id, name, type, price, company, count):
self.id = id
self.name = name
self.type = type
self.price = price
self.company = company
self.count = count
def __str__(self):
return f"{self.id}\t{self.name}\t{self.type}\t{self.price}\t{self.company}\t{self.count}"
class ProductManager:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def remove_product(self, product):
self.products.remove(product)
def find_product_by_name(self, name):
for product in self.products:
if product.name == name:
return product
return None
def purchase_product(self, name, count):
product = self.find_product_by_name(name)
if product:
product.count += count
else:
print("商品不存在")
def list_products(self):
for product in self.products:
print(product)
class UserInterface:
def __init__(self):
self.manager = ProductManager()
def run(self):
while True:
print("1.添加商品\n2.删除商品\n3.查询商品\n4.进货\n5.列出所有商品\n6.退出")
choice = input("请选择操作:")
if choice == "1":
id = input("请输入商品编号:")
name = input("请输入商品名称:")
type = input("请输入商品类型:")
price = float(input("请输入商品价格:"))
company = input("请输入商品生产公司:")
count = int(input("请输入商品数量:"))
product = Product(id, name, type, price, company, count)
self.manager.add_product(product)
print("商品添加成功")
elif choice == "2":
name = input("请输入要删除的商品名称:")
product = self.manager.find_product_by_name(name)
if product:
self.manager.remove_product(product)
print("商品删除成功")
else:
print("商品不存在")
elif choice == "3":
name = input("请输入要查询的商品名称:")
product = self.manager.find_product_by_name(name)
if product:
print(product)
else:
print("商品不存在")
elif choice == "4":
name = input("请输入要进货的商品名称:")
count = int(input("请输入进货数量:"))
self.manager.purchase_product(name, count)
print("进货成功")
elif choice == "5":
self.manager.list_products()
elif choice == "6":
break
else:
print("无效操作")
if __name__ == "__main__":
ui = UserInterface()
ui.run()
```
在这个程序中,我们定义了一个Product类来表示商品对象,一个ProductManager类来管理所有商品对象,以及一个UserInterface来提供用户界面。用户可以通过输入数字选择需要执行的操作,比如添加、删除、查询商品,进货或列出所有商品。程序会根据用户的选择调用相应的方法来执行操作。