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
Constructor Summary |
Train(java.lang.String dest)
Constructs Train with its destination. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
destination
private java.lang.String destination
train
private LinkedList<CargoCar> train
Train
public Train(java.lang.String dest)
- Constructs Train with its destination.
- Parameters:
dest
- train destination
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