题解 | #模拟销售商品系统#

自动售货系统

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

class A_sys():
    def __init__(self):
        self.items = [0,0,0,0,0,0]
        self.amounts = [0,0,0,0]
        self.balance = 0
        self.items_index = {"A1":0,"A2":1,"A3":2,"A4":3,"A5":4,"A6":5,}
        self.items_p = {"A1":2,"A2":3,"A3":4,"A4":5,"A5":8,"A6":6,}
        self.tuibi_mount = [0,0,0,0]
        
    def init_asys(self, order): #初始化商品数量和钱币张数
        a = order.split()
        b = a[1].split("-")
        c = a[2].split("-")
        self.items = list(map(int, b))
        self.amounts = list(map(int, c))
        print("S001:Initialization is successful")
        
    def toubi(self, order):
        a = order.split()[1]
        if a not in ["1","2","5","10"]:
            print("E002:Denomination error")
        else:
            if self.amounts[0] * 1 +self.amounts[1] * 2 < int(a) and a not in ("1", "2"):
                print("E003:Change is not enough, pay fail")
            else:
                if self.items == [0,0,0,0,0,0]:
                    print("E005:All the goods sold out")
                else:
                    self.balance += int(a)
                    if a == "1":
                        self.amounts[0] += 1
                    elif a == "2":
                        self.amounts[1] += 1
                    elif a == "5":
                        self.amounts[2] += 1
                    elif a == "10":
                        self.amounts[3] += 1
#                     print(self.amounts, a)
                    print("S002:Pay success,balance={}".format(self.balance))
                    
    def buy(self,order):
        a = order.split()[1]
        if a not in self.items_index.keys():
            print("E006:Goods does not exist")
        else:
            if self.items[self.items_index[a]] == 0:
                print("E007:The goods sold out")
            else:
                if self.balance < self.items_p[a]:
                    print("E008:Lack of balance")
                else:
                    self.balance -= self.items_p[a]
                    self.items[self.items_index[a]] -= 1
                    print("S003:Buy success,balance={}".format(self.balance))
                    
    def tuibi(self,order):
        if self.balance == 0:
            print("E009:Work failure")
        else:
            if self.amounts[3] >= self.balance // 10:
                self.tuibi_mount[3] = self.balance // 10
                self.balance -= self.tuibi_mount[3] * 10
                self.amounts[3] -= self.tuibi_mount[3]
            else:
                self.tuibi_mount[3] = self.amounts[3]
                self.balance -= self.tuibi_mount[3] * 10
                self.amounts[3] -= self.tuibi_mount[3]
            if self.amounts[2] >= self.balance // 10:
                self.tuibi_mount[2] = self.balance // 5
                self.balance -= self.tuibi_mount[2] * 5
                self.amounts[2] -= self.tuibi_mount[2]
            else:
                self.tuibi_mount[2] = self.amounts[2]
                self.balance -= self.tuibi_mount[2] * 5
                self.amounts[2] -= self.tuibi_mount[2]
            if self.amounts[1] >= self.balance // 2:
                self.tuibi_mount[1] = self.balance // 2
                self.balance -= self.tuibi_mount[1] * 2
                self.amounts[1] -= self.tuibi_mount[1]
            else:
                self.tuibi_mount[1] = self.amounts[1]
                self.balance -= self.tuibi_mount[1] * 2
                self.amounts[1] -= self.tuibi_mount[1]
            if self.amounts[0] >= self.balance // 1:
                self.tuibi_mount[0] = self.balance // 1
                self.balance -= self.tuibi_mount[0] * 1
                self.amounts[0] -= self.tuibi_mount[0]
            else:
                self.tuibi_mount[0] = self.amounts[0]
                self.balance -= self.tuibi_mount[0] * 1
                self.amounts[0] -= self.tuibi_mount[0]
            print("1 yuan coin number={}".format(self.tuibi_mount[0]))
            print("2 yuan coin number={}".format(self.tuibi_mount[1]))
            print("5 yuan coin number={}".format(self.tuibi_mount[2]))
            print("10 yuan coin number={}".format(self.tuibi_mount[3]))
#         
    def selsct(self,order):
        n = order.split()
#         print(n)
        if len(n) == 2:
            m = n[1]        
            if m == "0":
                count_dic = {}
                a = list(self.items_index.keys())
                for i, j in enumerate(self.items):
                    if j not in count_dic.keys():
                        count_dic[j] = a[i]
                    else:
                        count_dic[j] += " {}".format(a[i])
                b = self.items
                b.sort(reverse=True)
                for i in b:
                    if " " not in count_dic[i]:
                        print(count_dic[i],self.items_p[count_dic[i]],i)
                    else:
                        for j in count_dic[i].split():
                             print(j,self.items_p[j],i)
            elif m == "1":
                print("1 yuan coin number={}".format(self.amounts[0]))
                print("2 yuan coin number={}".format(self.amounts[1]))
                print("5 yuan coin number={}".format(self.amounts[2]))
                print("10 yuan coin number={}".format(self.amounts[3]))
            else:
                print("E010:Parameter error")
        else:
            print("E010:Parameter error")
        
#以下为调试代码
AA = A_sys()
ipp=input().strip(";").split(";")
for ip in ipp:
    if ip[0] == "r":
        AA.init_asys(ip)
    if ip[0] == "p":
        AA.toubi(ip)
    if ip[0] == "b":
        AA.buy(ip)
    if ip[0] == "c":
        AA.tuibi(ip)
    if ip[0] == "q":
        AA.selsct(ip)
        
# AA.init_asys("r 22-18-21-21-7-20 3-23-10-6")
# AA.init_asys("r 0-0-0-0-0-0 2-4-0-0")
# AA.toubi("P 10")
# print(AA.balance)
全部评论

相关推荐

牛客868257804号:九个中铁八个中建
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务