Package train :: Module sleeping_car :: Class SleepingCar
[hide private]
[frames] | no frames]

Class SleepingCar

object --+
         |
        SleepingCar

The class SleepingCar represents the sleeping cars of a train in a train ticket reservation system. Each sleeping car is divided in cabins which have SleepingCar.BEDS_PER_CABIN places (beds) for passengers. The cabins are numbered from one upwards.

This implementation works in such a way that whenever anyone reserves any places in the car, at least one full cabin is reserved.

In this simple model, no data is stored about who has reserved which places (beds) in the cars. Cars will only know if places are reserved or not.

Instance Methods [hide private]
 
__init__(self, number_of_cabins)
Creates a new sleeping car with the given number of cabins.
int
get_number_of_places(self)
Tells how many places (beds) this car has for passengers.
float
get_degree_of_reservation(self)
Tells how many percent of the car's passenger places have been reserved.
int
get_number_of_free_places(self)
Tells how many free (unreserved) places (beds) this car has for passengers.
int
get_number_of_cabins(self)
Tells how many cabins there are in this car.
int
get_number_of_free_beds_in_cabin(self, cabin_number)
Tells how many free (unreserved) places (beds) there are in the indicated cabin.
boolean
cabin_is_empty(self, cabin_number)
Tells if the indicated cabin is empty.
boolean
reserve_cabin(self, cabin_number)
Reserves all the places (beds) in one cabin.
boolean
reserve_places(self, number_of_people)
Reserves places (beds) for a group.
int
count_empty_cabins(self)
Returns the number of empty cabins in the car.
int
find_empty_cabin(self)
Searches for the first empty cabin in the car.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  BEDS_PER_CABIN = 3
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, number_of_cabins)
(Constructor)

 

Creates a new sleeping car with the given number of cabins.

Parameters:
  • number_of_cabins (int) - the number of cabins in the new car
Overrides: object.__init__

get_number_of_places(self)

 

Tells how many places (beds) this car has for passengers.

Returns: int
the total number of passenger places in the car

get_degree_of_reservation(self)

 

Tells how many percent of the car's passenger places have been reserved.

Returns: float
the degree of reservation for this car (percentage: 0.0 - 100.0).

get_number_of_free_places(self)

 

Tells how many free (unreserved) places (beds) this car has for passengers.

Returns: int
the number of free passenger places in the car

get_number_of_cabins(self)

 

Tells how many cabins there are in this car.

Returns: int
the number of cabins in the car

get_number_of_free_beds_in_cabin(self, cabin_number)

 

Tells how many free (unreserved) places (beds) there are in the indicated cabin.

Parameters:
  • cabin_number (int) - a cabin number (>=1)
Returns: int
the number of free places in the cabin

cabin_is_empty(self, cabin_number)

 

Tells if the indicated cabin is empty. That is, tells whether none of its beds have been reserved or not.

Parameters:
  • cabin_number (int) - a cabin number (>=1)
Returns: boolean
a boolean value indicating if the cabin is empty

reserve_cabin(self, cabin_number)

 

Reserves all the places (beds) in one cabin. If the cabin was not originally empty, this method does nothing but return False.

Parameters:
  • cabin_number (int) - a cabin number (>=1)
Returns: boolean
a boolean value indicating if the cabin was successfully reserved

reserve_places(self, number_of_people)

 

Reserves places (beds) for a group. For a sleeping car, a group reservation means that whole (originally empty) cabins are reserved so that all the members of the group fit in them. E.g. if the group size is 7, and each cabin has three beds, three cabins will be reserved.

The cabins to be reserved are selected simply so that the smallest possible empty cabin numbers are determined, and those cabins are reserved. The cabins need not be adjacent to each other.

If it is not possible to reserve suitable places for all members of the group, no places are reserved at all.

Parameters:
  • number_of_people (int) - the size of the group for which places are to be reserved
Returns: boolean
True if the group reservation was successful, False if not

count_empty_cabins(self)

 

Returns the number of empty cabins in the car.

Returns: int
the number of empty cabins in the car

find_empty_cabin(self)

 

Searches for the first empty cabin in the car.

Returns: int
the smallest empty cabin number in the car or a negative value, if no empty cabins can be found