使用Python实现一个小型的航空订票系统(1)

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


#这只是一个半成品,只是实现了,一个完整的订票过程,对于,如果刷票,自己研究 #简单过程 # 第一、getRandAndCookie() 获得cook 和一个随机数用于登录 # 第二、getEnterRandCode() 得到登录时的识别码 # 第三、setuseandpassword(randcode,use,password) 发送随机数、识别码和用户及密码。由于随机数只在内部使用,所以定义成了全局变量, # 第四、GetTrainList() 得到所有车站列表,'@bjb|北京北|VAP|beijingbei|bjb|0' 其中有中文、拼音、拼音缩写、所一个ID(唯一),其主要是可以,通过上面的列表,找到它的唯一ID,TranCityToId('南昌') # 第五、GetTrainNumList(date,fromstationid,tostationid,starttime) 得到哪到哪的所在车次,消息格式如下,其中所以,一下车次的的ID:"id":"650000K1060I" # {"end_station_name":"北京西","end_time":"16:18","id":"650000K1060I","start_station_name":"深圳","start_time":"10:54","value":"K106"} # 通过ChangeToTrainNumId('K106')得到车次ID # 第六、QueryTrain(fromstationid,tostationid,date,stationNum,starttime) 就是点击查询按键,得到是否有能预订,格式如下 #       南昌         20:12,    北京西        07:38,11:26,--,--,--,--,10,有,有,--,有,有,--,<a name='btn130_2' class='btn130_2' # 通过choiceSubmitNum(stationNum,trainsubmitinfo)提取出getSelected()消息 # 第七、submitRequest(choiceSubmitNum(stationNum,trainsubmitinfo),date,starttime) 就是点击预订按钮 # 第八、getrandCheckCode()得到提交订单的识别码 # 第十、CheckInMyTicket(info,randcode,peoples)点击提交,如果成功的话,就会返回{"errMsg":"Y"} # 出于,网络是UTF8格式,所以,必须# -*- coding: utf-8 -*-,(当然,自己转换也是可以的) # 出于这一个控制台信息,所以,识别码的图片在脚本同一目录 #得到头信息
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值