商品入库:现要对华为和小米两种手机产品进行入库,本案例要求编写一个模拟商品入库的程序,可以在控制台输入入库商品的数量,最后打印出仓库中所有商品详细信息以及所有商品的总库存数和库存商品总金额。 商品信息如下:1)品牌型号2)尺寸3)价格4) 配置5)库存6)总价
时间: 2024-02-27 20:54:02 浏览: 47
好的,以下是对应的代码实现:
```python
class Product:
def __init__(self, brand, model, size, price, config, quantity):
self.brand = brand
self.model = model
self.size = size
self.price = price
self.config = config
self.quantity = quantity
self.total_price = price * quantity
def __repr__(self):
return f"{self.brand} {self.model} ({self.size}) - {self.config} - {self.price}元 - 库存{self.quantity} - 总价{self.total_price}元"
class Inventory:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def print_inventory(self):
total_quantity = 0
total_price = 0
for product in self.products:
print(product)
total_quantity += product.quantity
total_price += product.total_price
print(f"总库存数: {total_quantity}")
print(f"库存商品总金额: {total_price}元")
if __name__ == '__main__':
inventory = Inventory()
# 添加华为手机到库存
huawei_p40 = Product("华为", "P40", "6.1英寸", 4488, "8GB RAM + 128GB ROM", 0)
inventory.add_product(huawei_p40)
# 添加小米手机到库存
xiaomi_10 = Product("小米", "10", "6.67英寸", 3799, "8GB RAM + 128GB ROM", 0)
inventory.add_product(xiaomi_10)
# 入库操作
huawei_quantity = int(input("请输入华为手机的入库数量: "))
huawei_p40.quantity = huawei_quantity
huawei_p40.total_price = huawei_quantity * huawei_p40.price
xiaomi_quantity = int(input("请输入小米手机的入库数量: "))
xiaomi_10.quantity = xiaomi_quantity
xiaomi_10.total_price = xiaomi_quantity * xiaomi_10.price
# 打印库存信息
inventory.print_inventory()
```
以上代码定义了两个类:`Product` 和 `Inventory`。`Product` 表示商品,包含品牌、型号、尺寸、价格、配置、库存和总价等信息;`Inventory` 表示库存,包含多个商品。在 `Inventory` 类中,定义了添加商品和打印库存信息的方法。
在 `__main__` 中,首先创建了 `Inventory` 对象,并添加了华为 P40 和小米 10 两种手机到库存中。然后通过输入获取用户输入的入库数量,并更新商品的库存和总价。最后打印整个库存的信息,包括每个商品的详细信息、总库存数和库存商品总金额。