题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
# 定义售货系统类
class Store():
# 初始化必要的属性
def __init__(self):
# 初始化默认值
# 商品
self.products = {
'A1':{
'name':'A1',
'price':2,
'count':0,
},
'A2':{
'name':'A2',
'price':3,
'count':0,
},
'A3':{
'name':'A3',
'price':4,
'count':0,
},
'A4':{
'name':'A4',
'price':5,
'count':0,
},
'A5':{
'name':'A5',
'price':8,
'count':0,
},
'A6':{
'name':'A6',
'price':6,
'count':0,
},
}
# 系统钱盒
self.money_box = {
'1元':{
'price':1,
'count':0
},
'2元':{
'price':2,
'count':0
},
'5元':{
'price':5,
'count':0
},
'10元':{
'price':10,
'count':0
}
}
# 余额
self.coin = 0
# 系统初始化命令处理函数
def init(self, cmd):
products,money = cmd.split(' ')
pro_pos = list(self.products)
money_pos = list(self.money_box)
# 初始化商品信息
for index,item in enumerate(products.split('-')):
self.products[pro_pos[index]]['count'] = int(item)
# 初始化钱盒信息
for index,item in enumerate(money.split('-')):
self.money_box[money_pos[index]]['count'] = int(item)
print("S001:Initialization is successful")
# 投币处理函数 cmd = '1'
def add_coins(self, cmd):
cmd = cmd + '元'
if cmd not in list(self.money_box):
print("E002:Denomination error")
return
if cmd in list(self.money_box)[2:]:
cur_min_money_price = self.money_box['1元']['count'] + self.money_box['2元']['price']*self.money_box['2元']['count']
if cur_min_money_price < self.money_box[cmd]['price']:
print("E003:Change is not enough, pay fail")
return
if self.check_is_product_empty():
print("E005:All the goods sold out")
else:
self.money_box[cmd]['count'] +=1
self.coin +=self.money_box[cmd]['price']
print("S002:Pay success,balance={}".format(self.coin))
# 购买
def buy(self, cmd):
if cmd not in list(self.products):
print("E006:Goods does not exist")
elif self.products[cmd]['count'] == 0:
print("E007:The goods sold out")
elif self.coin < self.products[cmd]['price']:
print("E008:Lack of balance")
else:
self.coin -= self.products[cmd]['price']
self.products[cmd]['count'] -=1
print("S003:Buy success,balance={}".format(self.coin))
# 退币
def coin_out(self):
if self.coin ==0:
print("E009:Work failure")
else:
total = []
while self.coin>=self.money_box['10元']['price']:
if self.money_box['10元']['count'] >0:
self.coin -= 10
self.money_box['10元']['count'] -=1
total.append('10元')
else:
break
while self.money_box['5元']['price'] <= self.coin:
if self.money_box['5元']['count'] >0:
self.coin -= 5
self.money_box['5元']['count'] -=1
total.append('5元')
else:
break
while self.money_box['2元']['price'] <= self.coin:
if self.money_box['2元']['count'] >0:
self.coin -= 2
self.money_box['2元']['count'] -=1
total.append('2元')
else:
break
while self.money_box['1元']['price'] <= self.coin:
if self.money_box['1元']['count'] >0:
self.coin -= 1
self.money_box['1元']['count'] -=1
total.append('1元')
else:
break
if self.coin > 0:
self.coin = 0
for item in list(self.money_box):
if item == "1元":
print("1 yuan coin number={}".format(total.count("1元")))
elif item == "2元":
print("2 yuan coin number={}".format(total.count("2元")))
elif item == "5元":
print("5 yuan coin number={}".format(total.count("5元")))
elif item == "10元":
print("10 yuan coin number={}".format(total.count("10元")))
# 查询商品
def query_product(self):
# 先将商品整理成可以排序的格式
por_list = []
for _,item in self.products.items():
por_list.append(item)
for index in range(len(por_list)-1):
for key in range(index+1,len(por_list)):
if por_list[key]['count'] > por_list[index]['count']:
por_list[index],por_list[key] = por_list[key],por_list[index]
for item in por_list:
print(item['name'],end=' ')
print(item['price'],end=' ')
print(item['count'])
# 查询钱盒
def query_money(self):
for _,v in self.money_box.items():
print("{} yuan coin number={}".format(v['price'] ,v['count']))
# 查询
def query(self, cmd):
if cmd == '0':
self.query_product()
if cmd == '1':
self.query_money()
else:
print("E010:Parameter error")
# 检查库存是否为空
def check_is_product_empty(self):
for _,v in self.products.items():
if v['count'] != 0:
return False
return True
store = Store()
input = input()
cmd_list = input.split(';')
for cmd in cmd_list:
if cmd.startswith('r'):
# 初始化操作
store.init(cmd[2:])
elif cmd.startswith('p'):
# 投币
store.add_coins(cmd[2:])
elif cmd.startswith('b'):
# 购买
store.buy(cmd[2:])
elif cmd.startswith('c'):
# 退币
store.coin_out()
elif cmd.startswith('q'):
# 查询
store.query(cmd[2:])
#自动售货系统#
查看2道真题和解析