Class Train

java.lang.Object
  extended by Train
All Implemented Interfaces:
java.lang.Iterable<CargoCar>

public class Train
extends java.lang.Object
implements java.lang.Iterable<CargoCar>

This class represents a train. It has a destination and a linked list of CargoCar objects. It implements Iterable by returning a direct access iterator to its linked list of cargo cars. Several methods, such as getDestination(), removeCargo() and getWeight(), are provided to manage a train object. COMPLETE THIS CLASS AND HAND IN THIS FILE

See Also:
LinkedList, CargoCar

Field Summary
private  java.lang.String destination
           
private  LinkedList<CargoCar> train
           
 
Constructor Summary
Train(java.lang.String dest)
          Constructs Train with its destination.
 
Method Summary
 void add(CargoCar cargoCar)
           
 void add(int pos, CargoCar newCargo)
           
 java.lang.String getDestination()
          Get the destination of this train.
 int getWeight(java.lang.String cargoName)
          Get the total weight of a cargo in this train.
 LinkedListIterator<CargoCar> iterator()
           
 CargoCar removeCargo(java.lang.String cargoName)
          Remove the first CargoCar from this train which has the same cargo name with the argument.
 void setDestination(java.lang.String newDest)
          Set a new destination for this train.
 java.lang.String toString()
          Returns Train with a String format as following.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

destination

private java.lang.String destination

train

private LinkedList<CargoCar> train
Constructor Detail

Train

public Train(java.lang.String dest)
Constructs Train with its destination.

Parameters:
dest - train destination
Method Detail

getDestination

public java.lang.String getDestination()
Get the destination of this train.

Returns:
train destination

setDestination

public void setDestination(java.lang.String newDest)
Set a new destination for this train.

Parameters:
newDest - new train destination

removeCargo

public CargoCar removeCargo(java.lang.String cargoName)
Remove the first CargoCar from this train which has the same cargo name with the argument. If there are multiple CargoCar objects with the same name, remove the first one.

Parameters:
The - name of the cargo that you wish to remove.
Returns:
removed CargoCar object if you successfully removed a cargo, otherwise null

getWeight

public int getWeight(java.lang.String cargoName)
Get the total weight of a cargo in this train.

Parameters:
the - name of the cargo to sum
Returns:
total weight of specified cargo in this train

add

public void add(CargoCar cargoCar)

add

public void add(int pos,
                CargoCar newCargo)

iterator

public LinkedListIterator<CargoCar> iterator()
Specified by:
iterator in interface java.lang.Iterable<CargoCar>

toString

public java.lang.String toString()
Returns Train with a String format as following.

{ENGINE_START}{destination}{ENGINE_END}{CARGO_LINK}{cargo}:{weight}{CARGO_LINK}{cargo}:{weight}...

By default, {ENGINE_START} = ( , {ENGINE_END} = ) and {CARGO_LINK} = -> (defined in Config). So if you did not modify Config class, it will generate a String with following format.

({destination})->{cargo}:{weight}->{cargo}:{weight}...

Overrides:
toString in class java.lang.Object
Returns:
train as a string format