我只写了两个文件,分别为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()