题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
class Vending_Machine(object):
def __init__(self):
# 投币余额
self.amts = 0
self.goods = {
"A1": [2, 0],
"A2": [3, 0],
"A3": [4, 0],
"A4": [5, 0],
"A5": [8, 0],
"A6": [6, 0]
}
self.box = {"1": 0, "2": 0, "5": 0, "10": 0}
# 投币
def insert_coin(self, amt):
if amt not in [10, 5, 2, 1]:
print("E002:Denomination error")
elif amt > 2 and amt > self.box["1"] + self.box["2"] * 2:
print("E003:Change is not enough, pay fail")
elif sum([i[1][1] for i in self.goods.items()]) == 0:
print("E005:All the goods sold out")
else:
self.amts += amt
self.box[str(amt)] += 1
print(f"S002:Pay success,balance={self.amts}")
# 购买商品
def purchase_goods(self, good):
if good not in [key for key in self.goods]:
print("E006:Goods does not exist")
elif self.goods[good][1] == 0:
print("E007:The goods sold out")
elif self.amts < self.goods[good][0]:
print("E008:Lack of balance")
else:
self.goods[good][1] -= 1
self.amts -= self.goods[good][0]
print(f"S003:Buy success,balance={self.amts}")
# 退币
def return_coin(self):
res = []
if self.amts == 0:
print("E009:Work failure")
return
if self.amts >= 10 and self.box["10"] > 0:
m, self.amts = divmod(self.amts, 10)
if m > self.box["10"]:
self.amts += (m - self.box["10"]) * 10 + self.amts
res.append(f"10 yuan coin number={self.box['10']}")
self.box["10"] = 0
else:
self.box["10"] -= m
res.append(f"10 yuan coin number={m}")
else:
res.append(f"10 yuan coin number=0")
if self.amts >= 5 and self.box["5"] > 0:
m, self.amts = divmod(self.amts, 5)
if m > self.box["5"]:
self.amts += (m - self.box["5"]) * 5 + self.amts
res.append(f"5 yuan coin number={self.box['5']}")
self.box["5"] = 0
else:
self.box["5"] -= m
res.append(f"5 yuan coin number={m}")
else:
res.append(f"5 yuan coin number=0")
if self.amts >= 2 and self.box["2"] > 0:
m, self.amts = divmod(self.amts, 2)
if m > self.box["2"]:
self.amts += (m - self.box["2"]) * 2 + self.amts
res.append(f"2 yuan coin number={self.box['2']}")
self.box["2"] = 0
else:
self.box["2"] -= m
res.append(f"2 yuan coin number={m}")
else:
res.append(f"2 yuan coin number=0")
if self.amts >= 1 and self.box["1"] > 0:
if self.amts > self.box["1"]:
# 则说明自动售货即钱不够了
self.amts = 0
res.append(f"1 yuan coin number={self.box['1']}")
self.box["1"] = 0
else:
# 说明自动售货即钱够
self.box["1"] -= self.amts
res.append(f"1 yuan coin number={self.amts}")
self.amts = 0
else:
res.append(f"1 yuan coin number=0")
for line in reversed(res):
print(line)
# 查询
def check(self, opt):
goods = sorted(self.goods.items(), key=lambda x: x[1][1], reverse=True)
coins = sorted(self.box.items(), key=lambda x: int(x[0]))
# 查询库存商品
if opt == "0":
for tem in goods:
print("{} {} {}".format(tem[0], tem[1][0], tem[1][1]))
# 查询存钱盒
if opt == "1":
for coin in coins:
print("{} yuan coin number={}".format(coin[0], coin[1]))
# 初始化商品和存钱罐
def set_goods_box(self, goods_nums, coin_nums):
a, b, c, d, e, f = goods_nums
self.goods = {
"A1": [2, a],
"A2": [3, b],
"A3": [4, c],
"A4": [5, d],
"A5": [8, e],
"A6": [6, f],
}
h, i, j, k = coin_nums
self.box = {"1": h, "2": i, "5": j, "10": k}
# r 22-18-21-21-7-20 3-23-10-6;c;q0;p 1;b A6;c;b A5;b A1;c;q1;p 5;
vendint = Vending_Machine()
command = input().split(";")[:-1]
# command = 'r 26-3-26-2-14-10 2-1-15-18;p 5;'.split(';')[:-1]
# command = 'r 22-18-21-21-7-20 3-23-10-6;q 1;q 0;'.split(';')[:-1]
for line in command:
line = line.split()
if line[0] == "r":
goods_nums = list(map(int, line[1].split("-")))
coin_nums = list(map(int, line[2].split("-")))
vendint.set_goods_box(goods_nums, coin_nums)
print("S001:Initialization is successful")
elif line[0] == "p":
vendint.insert_coin(int(line[1]))
elif line[0] == "b":
vendint.purchase_goods(line[1])
elif line[0] == "c":
vendint.return_coin()
elif line[0] == "q":
vendint.check(line[1])
else:
print("E010:Parameter error")
SHEIN希音公司福利 284人发布
