使用Python實現一個小型的航空訂票系統(1)

來源:互聯網
上載者:User

我唯寫了兩個檔案,分別為Classes.py和main.py.前一個用來定義類,後一個為main函數

#-*- coding:utf-8 -*-
#建立Airline Company類
class AirlineCompany:
    def __init__(self,namestr = '',idstr = ''):#簡潔的建構函式重載的能力
        self.name = namestr
        self.id = idstr
        self.airplanelist = []
    def getListOfAirplane(self):
        return self.airplanelist
    def getName(self):
        return self.name
    def getId(self):
        return self.id

#對Airplane的類的定義
class Airplane:
    def __init__(self,AirplaneID = '',departureLocation = '',arrivalLocation = '',departureTime = '',arrivalTime = '',company = None):
        self.AirplaneID = AirplaneID
        self.departureLocation = departureLocation
        self.arrivalLocation = arrivalLocation
        self.departureTime = departureTime
        self.arrivalTime = arrivalTime
        self.company = company
        self.seatlist = []
        if self.company != None:
            self.company.airplanelist.append(self)
    def setAirlineCompany(self,company2):
        if self.company == None:
            self.company = company2
            company2.airplanelist.append(self)
    def setSeatTicket(self,seat):
        seat.airplane = self
        self.seatlist.append(seat)#追加在後面
    def getAirplaneID(self):
        return self.AirplaneID
    def getDepartureLocation(self):
        return self.departureLocation
    def getArrivalLocation(self):
        return self.arrivalLocation
    def getDepartureTime(self):
        return self.departureTime
    def getArrivalTime(self):
        return self.arrivalTime
    def getListOfSeat(self):
        return self.seatlist

#對Customer類的定義
class Customer:
    def __init__(self,name = '',age = 20,seatlist = []):
        self.name = name
        self.age = age
        self.seatlist = seatlist
    def getListOfTickets(self):
        return self.seatlist
    def getName(self):
        return self.name
    def getAge(self):
        return self.age
    def buy(self,airplane1,sid):#這裡我覺得買票應該返回一張票,並且應輸入想要的座位號
        for seat in airplane1.seatlist:
            if seat.seatID == sid:
                seat.customer = self
                seat.vacancy = False
                break
#對Seat類的定義
class Seat:
    def __init__(self,seatID = '',price = 0,vacancy = True,customer = None,airplane = None):
        self.seatID = seatID
        self.price = price
        self.vacancy = vacancy
        self.customer = customer
        self.airplane = airplane
        if self.customer != None:
            self.customer.seatlist.append(self)
        if self.airplane != None:
            self.airplane.seatlist.append(self)
    def getPassenger(self):
        return self.customer
    def getAirplane(self):
        return self.airplane
    def getSeatID(self):
        return self.seatID
    def getPrice(self):
        return self.price
    def getVacancy(self):
        return self.vacancy
    def setVacancy(self,vacancy):
        self.vacancy = vacancy
    def setPassenger(self,customer):
        self.customer = customer

#對TravelAgency的定義
class TravelAgency:
    company1 = None
    company22 = None
    def main(self):
    #a. create four instances of Airplane (such as A380) b. create certain number of Seat for each airplane (such as F4)
        airplane1 = Airplane('A380',departureLocation = 'Beijing',arrivalLocation = 'USA',departureTime = '2012-02-02 12:56',arrivalTime = '2012-02-03 01:12')
        seat1 = Seat('F4',airplane = airplane1)
        seat11= Seat('E4',airplane = airplane1)
        
        airplane2 = Airplane('A381',departureLocation = 'MyHeart',arrivalLocation = 'YourHeart',departureTime = 'Never',arrivalTime = 'Whenever')
        seat2 = Seat('F5',airplane = airplane2)
        seat22 = Seat('E5',airplane = airplane2)
        
        airplane3 = Airplane('A382',departureLocation = 'Wuhan',arrivalLocation = 'Nanjing',departureTime = '2012-07-06 23:56',arrivalTime = '2012-07-08 01:13')
        seat3 = Seat('F6',airplane = airplane3)
        seat33 = Seat('E6',airplane = airplane3)
        
        airplane4 = Airplane('A383',departureLocation = 'Guangdong',arrivalLocation = 'Xian',departureTime = '2012-09-08 23:34',arrivalTime = '2012-09-09 02:23')
        seat4 = Seat('F7',airplane = airplane4)
        seat44 = Seat('E7',airplane = airplane4)
        
        seat5 = Seat('F8',airplane = airplane4)
        seat55 = Seat('E8',airplane = airplane4)
        
        
        TravelAgency.company1 = AirlineCompany('COM1','CC')
        TravelAgency.company22 = AirlineCompany('COM2','CD')
        
        
        airplane1.setAirlineCompany(TravelAgency.company1)
        airplane2.setAirlineCompany(TravelAgency.company1)
        
        
        
        airplane3.setAirlineCompany(TravelAgency.company22)
        airplane4.setAirlineCompany(TravelAgency.company22)
        
        customer1 = Customer('customer1',20)
        customer2 = Customer('customer2',22)
        customer3 = Customer('customer3',24)
        customer4 = Customer('customer4',26)
        customer5 = Customer('customer5',28)
        
        #alloe every customer buy two tickets
        customer1.buy(airplane1,seat1.seatID)
        customer1.buy(airplane1,seat11.seatID)
        
        customer2.buy(airplane2,seat2.seatID)
        customer2.buy(airplane2,seat22.seatID)
        
        customer3.buy(airplane3,seat3.seatID)
        customer3.buy(airplane3,seat33.seatID)
        
        customer4.buy(airplane4,seat4.seatID)
        customer4.buy(airplane4,seat44.seatID)
        
        customer5.buy(airplane4,seat5.seatID)
        customer5.buy(airplane4,seat55.seatID)
    def displayTickets(self):
        print TravelAgency.company1.name
        for ap in TravelAgency.company1.airplanelist:
            print '\t',ap.AirplaneID,ap.departureLocation,'-to-',ap.arrivalLocation,ap.departureTime,'-to-',ap.arrivalTime
            for se in ap.seatlist:
                print '\t\t',se.seatID,'   ',se.customer.name
        
        print TravelAgency.company22.name
        for ap in TravelAgency.company22.airplanelist:
            print '\t',ap.AirplaneID,ap.departureLocation,'-to-',ap.arrivalLocation,ap.departureTime,'-to-',ap.arrivalTime
            for se in ap.seatlist:
                print '\t\t',se.seatID,'   ',se.customer.name
    def displayCustomer(self):
        for ap in TravelAgency.company1.airplanelist:
            for se in ap.seatlist:
                print se.customer.name,se.customer.age,se.airplane.company.id,se.airplane.AirplaneID,se.seatID
        for ap in TravelAgency.company22.airplanelist:
            for se in ap.seatlist:
                print se.customer.name,se.customer.age,se.airplane.company.id,se.airplane.AirplaneID,se.seatID
                
            
               

下面為main:

from Classes import *
ta = TravelAgency()
ta.main()
ta.displayTickets()
ta.displayCustomer()        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.