Train Docs
Fall 2025
API

# SleepingCar

o1.train.SleepingCar
See the SleepingCar companion object
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.

## Parameters

numberOfCabins

  the number of cabins in this sleeping car

## Attributes

See also

  SleepingCar.BedsPerCabin

Companion
  object
Graph
Supertypes
  trait TrainCar
  class Object
  trait Matchable
  class Any

## Members list

### Value members

#### Concrete methods

def emptyCabinCount: Int

Returns the number of empty cabins in the car.

Returns the number of empty cabins in the car.

## Attributes

def isEmptyCabin(`cabinNumber`: Int): Boolean

Determines if the indicated cabin is empty. That is, determines whether it is the case that none of the beds in the cabin has been reserved.

Determines if the indicated cabin is empty. That is, determines whether it is the case that none of the beds in the cabin has been reserved.

## Parameters

cabinNumber

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

## Attributes

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.

(Since reserving even one bed from a cabin causes all the beds in the cabin to be reserved, this method will always return either zero or SleepingCar.BedsPerCabin.)

## Parameters

cabinNumber

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

## Attributes

def numberOfFreePlaces: Int

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

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

(Since reserving even one bed from a cabin causes all the beds in the cabin to be reserved, this method will always return a multiple of SleepingCar.BedsPerCabin.)

## Attributes

def numberOfPlaces: Int

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.

## Attributes

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`.

## Parameters

cabinNumber

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

## Attributes

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.

## Attributes

#### Inherited methods

def fullness: Double

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.

## Attributes

Inherited from:
  TrainCar

#### Concrete fields

val numberOfCabins: Int