一个简化的车站自动售票系统,简化车站售票系统,一个简化的车站自动售票系


一个简化的车站自动售票系统的基本功能描述如下:

(1)乘客按照以下三步操作购票:选定目的地;投入钱币;获得一张票;

(2)当且仅当乘客选定目的地后,系统才接受投钱;每次投入的钱只购买一张票;

(3)只要投入的钱不少于所需的票价,且票库中有所要求的票,则应尽快出票;

(4)如需找钱,则在出票的同时应退还多余的钱;

(5)如果乘客投入的钱不够票价,或者票库中没有所要求的票时,系统将全额退钱,并允许乘客另选目的地,继续购票;

(6)出票结束(包括退还多余的钱)后,系统应保存销售记录,并等待乘客购票。

# coding: UTF-8import os, sysimport msvcrtimport randomimport timedest = {1 : "Beijing", 2 : "Tianjin", 3 : "Shanghai", 4 : "Xi'an"}price = [10, 20, 30, 40]class Ticket():    def __init__(self, destination, price):        self.destination = destination        self.price = price        self.num = random.randint(0, 10)    def __str__(self):        return "Destination: " + self.destination + " \\r\\nPrice: " + str(self.price)    def isAvailable(self):        if self.num > 0:            return True        else:            return False    def buy(self, money):        if money > t.price:            print "TICKET:"            print str(t)            change = int(money) - t.price            print "Change: " + str(change)            print "Success!"            raw_input("Press <Enter>")            os.system("cls")            self.save(money, change)        elif money < t.price:            print "The amount you paid is not enough. "            print "Return: " + str(money)            raw_input("Press <Enter>")            os.system("cls")        else:            print "TICKET:"            print str(t)             print "Success!"            raw_input("Press <Enter>")            os.system("cls")              self.save(money)     def save(self, money, change = 0):        file = open('ticket.log', 'a')        s = self.__str__() + " Get: " + str(money) + " Change: " + str(change) + "\\r\\nTime: " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + "\\r\\n\\r\\n"        file.write(s)        file.close()if __name__ == '__main__':    while True:        print "----------Welcome to Ticket System!----------"        print "----------Choose your destination: ----------"        print "-----------------1. Beijing------------------"        print "-----------------2. Tianjin------------------"        print "-----------------3. Shanghai-----------------"        print "-----------------4. Xi'an--------------------"        print "-----------------0. Exit---------------------"        ch = sys.stdin.readline()        os.system("cls")        if int(ch) > 0 and int(ch) < 5:            destination = dest[int(ch)]            p = price[int(ch) -1]            t = Ticket(destination, p)            #print "Ticket info:"            #print str(t)            if t.isAvailable():                print str(t)                ch = raw_input("Take one?(y/n)")                if ch == 'y':                    money = raw_input("Enter the amount you pay: ")                    t.buy(int(money))                else:                    os.system("cls")                    continue            else:                print "The tickets are not enough!"        elif int(ch) == 0:            sys.exit(0)        else:            print "Input the right choice, please."#该片段来自于http://byrx.net

评论关闭