创建ComputerShop类,参数:list列表,存储内容有库存、品牌、价格
(例如:list = [{‘count’:11,‘brand’:‘拯救者’,‘price’:5999},
{‘count’:21,‘brand’:‘外星人’,‘price’:7999]),
money为用户开店进货后剩余金额,创建对象时需要指定该金额。
方法有:
1、查找商品,让用户输入指定品牌,查找到后打印该品牌电脑的信息;
2、售卖商品,用户输入商品名称后,在库存中查找信息,判断库存,
然后卖出(默认一台一台的卖),卖出结果为库存该商品-1,店铺余额money增加;
3、商品进货,输入进货名称,进货价为商品价格-1000,
判断店铺金额是否满足进货金额,不满足重新输入,满足后,店铺余额减少,指定商品数量增加
4、打印店铺信息,将剩余商品的名称、价格、库存以及店铺余额
打印出来
class ComputerShop(object):
def __init__(self):
self.list =[{'count':30,'brand':'拯救者','price':5999},
{'count':20,'brand':'外星人','price':7999},
{'count':10,'brand':'苹果','price':6999},
{'count':17,'brand':'华硕','price':6888}]
self.money = 100000
def find(self):
name = input('请输入您要查找电脑的品牌:')
for l in self.list:
for j in l.values():
if j == name:
print('品牌:'+name,'\n单价:%d'%l['price'])
def sell(self):
name = input('请输入您要购买电脑的品牌:')
for l in self.list:
for j in l.values():
if j == name:
if l['count'] > 0:
print('请您收好物品')
print('此次花费:%d'%l['price'])
l['count'] -= 1
self.money += l['price']
else:
print('库存不足')
#stock 进货
def stock(self):
name = input('请输入您要进货的电脑的品牌:')
while True:
num = int(input('请输入您要进货的电脑的数量:'))
for l in self.list:
for j in l.values():
if j == name:
if self.money >= num *(l['price'] - 1000):
print('进货成功')
self.money -= num *(l['price'] - 1000)
l['count'] += num
break
def print(self):
for l in self.list:
for j in l.keys():
print('')
print(l)
a = ComputerShop()
a.find()
a.sell()
a.stock()
a.print()