Adding a second class

One of the things we'd like to do with our flight is accept seat bookings. To do that we need to know the seating layout, and for that we need to know the type of aircraft. Let's make a second class to model different kinds of aircraft:

class Aircraft:

def __init__(self, registration, model, num_rows,
num_seats_per_row):

self._registration = registration
self._model = model
self._num_rows = num_rows
self._num_seats_per_row = num_seats_per_row

def registration(self):
return self._registration

def model(self):
return self._model

The initializer creates four attributes for the aircraft: registration number, a model name, the number of rows of seats, and the number of seats per row. In a production code scenario we could validate these arguments to ensure, for example, that the number of rows is not negative.

This is straightforward enough, but for the seating plan we'd like something a little more in line with our booking system. Rows in aircraft are numbered from one, and the seats within each row are designated with letters from an alphabet which omits I to avoid confusion with 1:


Figure 8.1: Aircraft seating plan.

We'll add a seating_plan() method which returns the allowed rows and seats as a 2-tuple containing a range object and a string of seat letters:

def seating_plan(self):
return (range(1, self._num_rows + 1),
"ABCDEFGHJK"[:self._num_seats_per_row])

It's worth pausing for a second to make sure you understand how this function works. The call to the range() constructor produces a range object which can be used as an iterable series of row numbers, up to the number of rows in the plane. The string and its slice method return a string with one character per seat. These two objects – the range and the string – are bundled up into a tuple.

Let's construct a plane with a seating plan:

  >>> from airtravel import *
>>> a = Aircraft("G-EUPT", "Airbus A319", num_rows=22,
num_seats_per_row=6)

>>> a.registration()
'G-EUPT'
>>> a.model()
'Airbus A319'
>>> a.seating_plan()
(range(1, 23), 'ABCDEF')

See how we used keyword arguments for the rows and seats for documentary purposes. Recall the ranges are half-open, so 23 is correctly one-beyond-the-end of the range.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset