SleepingCar

class SleepingCar(val numberOfCabins: Int) extends TrainCar

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

When created, all the cabins in a sleeping car are unreserved. This changes as reservations are made.

This implementation is intended to work in such a way that whenever anyone reserves any places in the car, at least one full cabin is reserved. Multiple reservations can never share a cabin.

In this simple implementation, no data is stored about who has reserved which cabins.

Value parameters:
numberOfCabins

the number of cabins in this sleeping car

See also:
Companion:
object
trait TrainCar
class Object
trait Matchable
class Any

Value members

Concrete methods

Returns the number of empty cabins in the car.

Returns the number of empty cabins in the car.

def isEmptyCabin(cabinNumber: Int): Boolean

Determines if the indicated cabin is empty, that is, whether none of its beds have been reserved or not.

Determines if the indicated cabin is empty, that is, whether none of its beds have been reserved or not.

Value parameters:
cabinNumber

a cabin number (>=1 and no bigger than the number of cabins)

def numberOfFreeBedsInCabin(cabinNumber: Int): Int

Returns the number of free (unreserved) places (beds) there are in the given cabin.

Returns the number of free (unreserved) places (beds) there are in the given cabin.

Value parameters:
cabinNumber

a cabin number (>=1 and no bigger than the number of cabins)

Returns the number of free, unreserved places (beds) in this car.

Returns the number of free, unreserved places (beds) in this car.

Returns the number of places (beds) this car has for passengers. This is a positive number.

Returns the number of places (beds) this car has for passengers. This is a positive number.

def reserveCabin(cabinNumber: Int): Boolean

Reserves all the places (beds) in one cabin. Returns a Boolean value indicating whether the reservation was successful. If the cabin was not originally empty, this method does nothing but return false.

Reserves all the places (beds) in one cabin. Returns a Boolean value indicating whether the reservation was successful. If the cabin was not originally empty, this method does nothing but return false.

Value parameters:
cabinNumber

a cabin number (>=1 and no bigger than the number of cabins)

def reservePlaces(numberOfPeople: Int): Boolean

Reserves places (beds) for a group of people whose size is indicated by the parameter. For a sleeping car, a group reservation means that a number of entire empty cabins is reserved so that all the members of the group fit in them. For instance, if the group size is seven, and each cabin has three beds, three whole cabins will be reserved.

Reserves places (beds) for a group of people whose size is indicated by the parameter. For a sleeping car, a group reservation means that a number of entire empty cabins is reserved so that all the members of the group fit in them. For instance, if the group size is seven, and each cabin has three beds, three whole cabins will be reserved.

The cabins to be reserved are selected simply so that the smallest available numbers of empty cabins are determined, and those cabins are reserved. The cabins need not be adjacent to each other, but must all be in this car.

If it is not possible to reserve suitable cabins for all members of the group, no places are reserved at all. The return value of this method indicates whether the places were reserved or not.

Inherited methods

Returns a figure between 0 and 100 that indicates how many percent of the car’s passenger places have been reserved.

Returns a figure between 0 and 100 that indicates how many percent of the car’s passenger places have been reserved.

Inherited from:
TrainCar

Concrete fields