forked from seeditsolution/pythonprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflightbook.py
More file actions
312 lines (251 loc) · 9.66 KB
/
Copy pathflightbook.py
File metadata and controls
312 lines (251 loc) · 9.66 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
class Flight:
"""A flight with a particular passenger aircraft."""
def __init__(self, number, aircraft):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))
if not number[:2].isupper():
raise ValueError("Invalid airline code '{}'".format(number))
if not (number[2:].isdigit() and int(number[2:]) <= 9999):
raise ValueError("Invalid route number '{}'".format(number))
self._number = number
self._aircraft = aircraft
rows, seats = self._aircraft.seating_plan()
self._seating = [None] + [{letter: None for letter in seats} for _
in rows]
def number(self):
return self._number
def airline(self):
return self._number[:2]
def aircraft_model(self):
return self._aircraft.model()
class Aircraft:
def __init__(self, registration, model, maxrows, maxseatperrow):
self._registration = registration
self._model = model
self._maxrows =maxrows
self._maxseatperrow = maxseatperrow
def registration(self):
return self._registration
def model(self):
return self._model
def seating_plan(self):
return (range(1, self._maxrows + 1), "ABCDEFGHJK"[:self._maxseatperrow])
class AirbusA319:
def __init__(self, registration):
self._registration = registration
def registration(self):
return self._registration
def model(self):
return "Airbus A319"
def seating_plan(self):
return range(1, 23), "ABCDEF"
class Boeing777:
def __init__(self, registration):
self._registration = registration
def registration(self):
return self._registration
def model(self):
return "Boeing 777"
def seating_plan(self):
return range(1, 56), "ABCDEGHJK"
class Aircraft:
def num_seats(self):
rows, row_seats = self.seating_plan()
return len(rows) * len(row_seats)
class AirbusA319:
def __init__(self, registration):
self._registration = registration
def registration(self):
return self._registration
def model(self):
return "Airbus A319"
def seating_plan(self):
return range(1, 23), "ABCDEF"
def num_seats(self):
rows, row_seats = self.seating_plan()
return len(rows) * len(row_seats)
class Boeing777:
def __init__(self, registration):
self._registration = registration
def registration(self):
return self._registration
def model(self):
return "Boeing 777"
def seating_plan(self):
return range(1, 56), "ABCDEGHJK"
def num_seats(self):
rows, row_seats = self.seating_plan()
return len(rows) * len(row_seats)
class Aircraft:
def num_seats(self):
rows, row_seats = self.seating_plan()
return len(rows) * len(row_seats)
class AirbusA319(Aircraft):
def __init__(self, registration):
self._registration = registration
def registration(self):
return self._registration
def model(self):
return "Airbus A319"
def seating_plan(self):
return range(1, 23), "ABCDEF"
class Boeing777(Aircraft):
def __init__(self, registration):
self._registration = registration
def registration(self):
return self._registration
def model(self):
return "Boeing 777"
def seating_plan(self):
return range(1, 56), "ABCDEGHJK"
class Aircraft:
def __init__(self, registration):
self._registration = registration
def registration(self):
return self._registration
def num_seats(self):
rows, row_seats = self.seating_plan()
return len(rows) * len(row_seats)
class AirbusA319(Aircraft):
def model(self):
return "Airbus A319"
def seating_plan(self):
return range(1, 23), "ABCDEF"
class Boeing777(Aircraft):
def model(self):
return "Boeing 777"
def seating_plan(self):
return range(1, 56), "ABCDEGHJK"
class Flight:
"""A flight with a particular passenger aircraft."""
def __init__(self, number, aircraft):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))
if not number[:2].isupper():
raise ValueError("Invalid airline code'{}'".format(number))
if not (number[2:].isdigit() and int(number[2:]) <= 9999):
raise ValueError("Invalid route number'{}'".format(number))
self._number = number
self._aircraft = aircraft
rows, seats = self._aircraft.seating_plan()
self._seating = [None] + [{letter: None for letter in seats} for _ in rows]
def number(self):
return self._number
def airline(self):
return self._number[:2]
def aircraft_model(self):
return self._aircraft.model()
def allocate_seat(self, seat, passenger):
"""Allocate a seat to a passenger.
Args:
seat: A seat designator such as '12C' or '21F'.
passenger: The passenger name.
Raises:
ValueError: If the seat is unavailable.
"""
rows, seat_letters = self._aircraft.seating_plan()
letter = seat[-1]
if letter not in seat_letters:
raise ValueError("Invalid seat letter {}".format(letter))
row_text = seat[:-1]
try:
row = int(row_text)
except ValueError:
raise ValueError("Invalid seat row {}".format(row_text))
if row not in rows:
raise ValueError("Invalid row number {}".format(row))
if self._seating[row][letter] is not None:
raise ValueError("Seat {} already occupied".format(seat))
self._seating[row][letter] = passenger
def _parse_seat(self, seat):
"""Parse a seat designator into a valid row and letter.
Args:
seat: A seat designator such as 12F
Returns:
A tuple containing an integer,string for row and seat.
"""
row_numbers, seat_letters = self._aircraft.seating_plan()
letter = seat[-1]
if letter not in seat_letters:
raise ValueError("Invalid seat letter {}".format(letter))
row_text = seat[:-1]
try:
row = int(row_text)
except ValueError:
raise ValueError("Invalid seat row {}".format(row_text))
if row not in row_numbers:
raise ValueError("Invalid row number {}".format(row))
return row, letter
def relocate_passenger(self, from_seat, to_seat):
"""Relocate a passenger to a different seat.
Args:
from_seat: The existing seat designator for the
passenger to be moved.
to_seat: The new seat designator.
"""
from_row, from_letter = self._parse_seat(from_seat)
if self._seating[from_row][from_letter] is None:
raise ValueError("No passenger to relocate in seat {}".format(from_seat))
to_row, to_letter = self._parse_seat(to_seat)
if self._seating[to_row][to_letter] is not None:
raise ValueError("Seat {} already occupied".format(to_seat))
self._seating[to_row][to_letter] = self._seating[from_row][from_letter]
self._seating[from_row][from_letter] = None
def num_available_seats(self):
return sum( sum(1 for s in row.values() if s is None)
for row in self._seating if row is not None )
def make_flights():
f = Flight("BA758", AirbusA319("VT-AAA"))
f.allocate_seat('12A', 'Harry Potter')
f.allocate_seat('15F', 'Hermione Granger')
f.allocate_seat('15E', 'Ron Weasely')
f.allocate_seat('1C', 'Draco Malfoy')
f.allocate_seat('1D', 'Gregory Goyle')
g = Flight("AF72", Boeing777("VT-AAB"))
g.allocate_seat('55K', 'Sirius Black')
g.allocate_seat('33G', 'Remus Lupin')
g.allocate_seat('4B', 'James Potter')
g.allocate_seat('4A', 'Lily Potter')
return f, g
def make_flight():
a = Flight("AI758", Aircraft("VT-AAA", "Airbus A319", maxrows=22, maxseatperrow=6))
a.allocate_seat('12A', 'Harry')
a.allocate_seat('15F', 'Hermione')
a.allocate_seat('15E', 'Ron')
a.allocate_seat('1C', 'Draco')
a.allocate_seat('1D', 'Goyle')
return a
def allocate_seat(seat, passenger):
"""Allocate a seat to a passenger.
Args:
seat: A seat designator such as '12C' or '21F'.
passenger: The passenger name.
Raises:
ValueError: If the seat is unavailable.
"""
rows, seat_letters = self._aircraft.seating_plan()
letter = seat[-1]
if letter not in seat_letters:
raise ValueError("Invalid seat letter {}".format(letter))
row_text = seat[:-1]
try:
row = int(row_text)
except ValueError:
raise ValueError("Invalid seat row {}".format(row_text))
if row not in rows:
raise ValueError("Invalid row number {}".format(row))
if self._seating[row][letter] is not None:
raise ValueError("Seat {} already occupied".format(seat))
self._seating[row][letter] = passenger
def allocate_seat(self, seat, passenger):
"""Allocate a seat to a passenger.
Args:
seat: A seat designator such as '12C' or '21F'.
passenger: The passenger name.
Raises:
ValueError: If the seat is unavailable.
"""
row, letter = self._parse_seat(seat)
if self._seating[row][letter] is not None:
raise ValueError("Seat {} already occupied".format(seat))
self._seating[row][letter] = passenger