Repository navigation
Expand file tree
/
Copy pathFlight.rb
More file actions
73 lines (65 loc) · 2.51 KB
/
Copy pathFlight.rb
File metadata and controls
73 lines (65 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#require_relative 'Airport'
require './Date'
# The Flight class represents a single, direct flight between two airports
class Flight
attr_reader :flightCode,:seatprice, :date, :departureTime, :flightDuration, :departureAirport, :destinationAirport, :seats
def initialize(connCode, date)
@flightCode = connCode[0,6]
if (date.class.to_s.eql?("Date"))
@date = date
else
@date = Date.new(date, nil)
end
@departureTime = MyTime.new(connCode[6,5])
@flightDuration = MyTime.new(connCode[11,5])
@departureAirport = nil
@destinationAirport = nil
# @adameus = Adameus.new
@seats = 0
@seatprice = 9999
end
# Returns the cost of a seat on this flight if there is still room for another
# passenger, in all other cases nil gets returned.
# Prices change all the time, thus every time the price for a particular seat on
# this flight is requested a new database call is made.
# NOTE TO SELF: curiously enough the call to .to_s for date is not required
def price(seatClass)
seatInfo = $adameus.seats(date, flightCode, seatClass).chomp
#puts seatInfo
if (seatInfo.size != 8) then return nil end
@seats = seatInfo[0,3]
@seatprice = (seatInfo[3,5]).to_i
if (@seats.to_i < 1) then return nil
else return @seatprice
end
end
# since departure and destination airports of a particular flight
# are not supposed to change, these are at most requested once from the database.
def departure
if (departureAirport.nil?) then setAirports end
return @departureAirport
end
# since departure and destination airports of a particular flight
# are not supposed to change, these are at most requested once from the database.
def destination
if (destinationAirport.nil?) then setAirports end
return @destinationAirport
end
# The database is queried for the airports associated with this direct flight
def setAirports
airportCodes = $adameus.flight_airports(flightCode)
@departureAirport = Airport.new(airportCodes[0,3])
@destinationAirport = Airport.new(airportCodes[3,3])
end
end
#-SOME-TESTS--------------------------------------------------------------------
#date = '2012-01-15'
#adameus = Adameus.new
#conns = adameus.connections('VIE', 'BRU', date).split(/\n/)
#myFlight = Flight.new(conns[1], date)
#puts myFlight.price('F')
#puts myFlight.departure
#puts myFlight.destination
#puts myFlight.departureTime
#puts myFlight.flightDuration
#-------------------------------------------------------------------------------