Train

class Train(val description: String)

The class Train represents trains which consist of a number of passenger cars. The cars are numbered from one upwards (starting from the head of the train).

A train object is mutable. New cars can be added to it with the addCar method, but initially a train has no cars. Trains have a maximum length (in cars) which they must not exceed.

A train may contain cars of various types, that is, instances of any class that mixes in the TrainCar trait.

Value parameters:
description

a description of the train, e.g. "From Helsinki to Oulu, leaves at 8:13"

Companion:
object
class Object
trait Matchable
class Any

Value members

Concrete methods

Adds the given car to the end of the train, if possible.

Adds the given car to the end of the train, if possible.

Value parameters:
car

the car to be added

Returns:

true if the car was added, false if the length would have become excessive

See also:
def car(numberOfCar: Int): Option[TrainCar]

Returns the car indicated by the given number, wrapped in an Option. Returns None if the given number is too low or too high.

Returns the car indicated by the given number, wrapped in an Option. Returns None if the given number is too low or too high.

Value parameters:
numberOfCar

the number of the car that should be returned (must be between 1 and the number of cars in the train or the result will be None)

def length: Int

Returns the number of (passenger) cars there are in this train.

Returns the number of (passenger) cars there are in this train.

Concrete fields