Class TrainHub

java.lang.Object
  extended by TrainHub

public class TrainHub
extends java.lang.Object

This class represents a train hub and provides the common operations needed for managing the incoming and outgoing trains. It has a LinkedList of Train as a member variable and manages it. COMPLETE THIS CLASS AND HAND IN THIS FILE

See Also:
LinkedList, Train, Config

Field Summary
private  LinkedList<Train> trains
          The internal data structure of a hub is a linked list of Trains
 
Constructor Summary
TrainHub()
          Constructs and initializes TrainHub object
 
Method Summary
 boolean departAllTrains()
          This method deletes all the trains.
 boolean departTrain(java.lang.String dest)
          This method is used to depart the train to the given destination.
 boolean displayAllTrains()
          This method is used to display all the departing trains in the train hub.
 boolean displayTrain(java.lang.String dest)
          Display the specific train for a destination.
 Train findTrain(java.lang.String dest)
          This method tries to find the train in the list of trains, departing to the given destination city.
 int getWeight(java.lang.String name)
          This method iterates through all the trains in the list and finds the sum of weights of given cargo in all trains.
 void processIncomingTrain(Train train)
          This method processes the incoming train.
 CargoCar removeCargo(java.lang.String dest, java.lang.String name)
          This method removes the first cargo car going to the given destination city and carrying the given cargo.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

trains

private LinkedList<Train> trains
The internal data structure of a hub is a linked list of Trains

Constructor Detail

TrainHub

public TrainHub()
Constructs and initializes TrainHub object

Method Detail

processIncomingTrain

public void processIncomingTrain(Train train)
This method processes the incoming train. It iterates through each of the cargo car of the incoming train. If there is an outgoing train in the train list going to the destination city of the cargo car, then it removes the cargo car from the incoming train and adds the cargo car at the correct location in the outgoing train. The correct location is to become the first of the matching cargo cars, with the cargo cars in alphabetical order by their cargo name. If there is no train going to the destination city, it creates a new train and adds the cargo to this train.

Parameters:
train - Incoming train (list or cargo cars)

findTrain

public Train findTrain(java.lang.String dest)
This method tries to find the train in the list of trains, departing to the given destination city.

Parameters:
dest - Destination city for which train has to be found.
Returns:
Pointer to the train if the train going to the given destination exists. Otherwise returns null.

removeCargo

public CargoCar removeCargo(java.lang.String dest,
                            java.lang.String name)
This method removes the first cargo car going to the given destination city and carrying the given cargo.

Parameters:
dest - Destination city
name - Cargo name
Returns:
If there is a train going to the the given destination and is carrying the given cargo, it returns the cargo car. Else it returns null.

getWeight

public int getWeight(java.lang.String name)
This method iterates through all the trains in the list and finds the sum of weights of given cargo in all trains.

Parameters:
name - Name of the cargo
Returns:
Total weight of given cargo in all departing trains.

departTrain

public boolean departTrain(java.lang.String dest)
This method is used to depart the train to the given destination. To depart the train, one needs to delete the train from the list.

Parameters:
dest - Destination city for which corresponding train has to be departed/deleted.
Returns:
True if train to the given destination city exists. If not, then return false.

departAllTrains

public boolean departAllTrains()
This method deletes all the trains.

Returns:
True if there was at least one train to delete. False if there was no train to delete.

displayTrain

public boolean displayTrain(java.lang.String dest)
Display the specific train for a destination.

Parameters:
dest - Destination city for the train the to be displayed.
Returns:
True if train to the given destination city exists. If not, then return false.

displayAllTrains

public boolean displayAllTrains()
This method is used to display all the departing trains in the train hub. Train should be displayed in the specified format.

Returns:
True if there is at least one train to print. False if there is no train to print.