题解 | #自动售货系统#

自动售货系统

https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

class xx:
    def __init__(self):
        self.good = {
            "A1": [2, 0],
            "A2": [3, 0],
            "A3": [4, 0],
            "A4": [5, 0],
            "A5": [8, 0],
            "A6": [6, 0],
        }
        self.mon = {1: 0, 2: 0, 5: 0, 10: 0}
        self.balance = 0

    def pars(self, a):

        if a[0] == "r":
            self.r(a[1:])
        elif a[0] == "p":
            self.p(a[1:])
        elif a[0] == "b":
            self.b(a[1:])
        elif a[0] == "c":
            self.c()
        elif a[0] == "q":
            self.q(a[1:])

    def r(self, a):
        b, c = a.split()
        b = b.split("-")
        c = c.split("-")
        for i in range(len(b)):
            self.good["A" + str(i + 1)][1] = int(b[i])
        self.mon[1] = int(c[0])
        self.mon[2] = int(c[1])
        self.mon[5] = int(c[2])
        self.mon[10] = int(c[3])
        print("S001:Initialization is successful")

    def p(self, a):
        if a not in [" 1", " 2", " 5", " 10"]:
            print("E002:Denomination error")
        elif a in [" 5", " 10"] and self.mon[1] + 2 * self.mon[2] < int(a[1:]):
            print("E003:Change is not enough, pay fail")
        elif (
            self.good["A1"][1] == 0
            and self.good["A2"][1] == 0
            and self.good["A3"][1] == 0
            and self.good["A4"][1] == 0
            and self.good["A5"][1] == 0
            and self.good["A6"][1] == 0
        ):
            print("E005:All the goods sold out")
        else:
            self.balance += int(a[1:])
            self.mon[int(a[1:])] += 1
            print("S002:Pay success,balance={}".format(self.balance))

    def b(self, a):
        if a not in [" A1", " A2", " A3", " A4", " A5", " A6"]:
            print("E006:Goods does not exist")
        elif self.good[a[1:]][1] == 0:
            print("E007:The goods sold out")
        elif self.balance < self.good[a[1:]][0]:
            print("E008:Lack of balance")
        else:
            self.good[a[1:]][1] -= 1
            self.balance -= self.good[a[1:]][0]
            print("S003:Buy success,balance={}".format(self.balance))

    def c(self):
        if self.balance == 0:
            print("E009:Work failure")
        else:
            ls = [0, 0, 0, 0]
            while self.balance > 0:
                if self.balance >= 10 and self.mon[10] >= 1:
                    self.balance -= 10
                    self.mon[10] -= 1
                    ls[0] += 1
                elif self.balance >= 5 and self.mon[5] >= 1:
                    self.balance -= 5
                    self.mon[5] -= 1
                    ls[1] += 1
                elif self.balance >= 2 and self.mon[2] >= 1:
                    self.balance -= 2
                    self.mon[2] -= 1
                    ls[2] += 1
                elif self.balance >= 1 and self.mon[1] >= 1:
                    self.balance -= 1
                    self.mon[1] -= 1
                    ls[3] += 1
                else:
                    break
            print("1 yuan coin number={}".format(ls[3]))
            print("2 yuan coin number={}".format(ls[2]))
            print("5 yuan coin number={}".format(ls[1]))
            print("10 yuan coin number={}".format(ls[0]))

    def q(self, a):
        if a not in [" 0", " 1"]:
            print("E010:Parameter error")
        if a == " 0":
            for i in sorted(self.good.items(), key=lambda x: x[1][1], reverse=True):
                print("{} {} {}".format(i[0], i[1][0], i[1][1]))
        if a == " 1":
            print("1 yuan coin number={}".format(self.mon[1]))
            print("2 yuan coin number={}".format(self.mon[2]))
            print("5 yuan coin number={}".format(self.mon[5]))
            print("10 yuan coin number={}".format(self.mon[10]))


while True:
    try:
        a = input().split(";")
        yy = xx()
        for i in a:
            if i:
                yy.pars(i)
    except:
        break

全部评论

相关推荐

11-02 09:49
已编辑
货拉拉_测试(实习员工)
热爱生活的仰泳鲈鱼求你们别卷了:没事楼主,有反转查看图片
点赞 评论 收藏
分享
10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务