Class Dispatch

java.lang.Object
  extended by Dispatch

public class Dispatch
extends Object

Responsible for injecting and receiving passengers from the elevator simulation. This class is the source for Passengers, adding them to the elevator bank at each update. This class is also the sink for Passengers, accepting them after they disembark from an elevator.

This class is a singleton; only one instance can exist, accessed through the Dispatch.instance() method.


Field Summary
private  ElevatorBank bank
           
private  int consumeCount
           
private  List<Passenger> dispatched
           
private  BufferedReader fileIn
           
private  BufferedWriter fileOut
           
private static Dispatch instance
           
private  int iteration
           
private  Landscape landscape
           
private  int maxWaitTime
           
private  int minWaitTime
           
private  String nextLine
           
private  double pGround
           
private  double pOther
           
private  int produceCount
           
private static Random rand
           
private  int spawnTries
           
private  int totalWaitTime
           
 
Constructor Summary
private Dispatch()
          Builds a dispatch; private to control when the single instance is instantiated.
 
Method Summary
 void closeFiles()
          Closes any files opened by this dispatch.
 void consumePassenger(Passenger user, Elevator lift)
          Receives a passenger from the simulation.
 String formatStatistics()
          Returns a string containing statistics.
static Dispatch instance()
          Returns a reference to the single instance of this class.
private  void producePassenger(Passenger user)
          Injects a passenger into the simulation; this adds the passenger to the elevator bank, initially waiting at their start floor.
 void readFromFile(String filename)
          Sets up this class to read simulation data in from a file.
private  Passenger readPassenger()
          Attempts to read information for a single passenger in from the input file.
 void setBank(ElevatorBank bank)
          Sets the elevator bank that this Dispatch spawns passengers for.
 void setGroundProbability(double pGround)
          Sets the probability that a passenger will be generated on the ground floor.
 void setLandscape(Landscape scape)
          Sets the landscape that this Dispatch spawns passengers in.
 void setOtherProbability(double pOther)
          Sets the probability that a passenger will be generated on an floor other than the ground floor.
 void setSpawnTries(int spawnTries)
          Sets the number of times that passengers could get spawned in each iteration.
private  void spawnPassengers()
          Spawns new passengers randomly and adds them to the simulation.
 void updateState()
          Instructs the dispatch to move to the next iteration of the simulation, spawning new passengers and adding them to the simulation.
 void writeToFile(String filename)
          Sets up this class to write simulation data out to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

private static Dispatch instance

rand

private static Random rand

bank

private ElevatorBank bank

landscape

private Landscape landscape

iteration

private int iteration

maxWaitTime

private int maxWaitTime

minWaitTime

private int minWaitTime

consumeCount

private int consumeCount

produceCount

private int produceCount

totalWaitTime

private int totalWaitTime

dispatched

private List<Passenger> dispatched

fileIn

private BufferedReader fileIn

fileOut

private BufferedWriter fileOut

nextLine

private String nextLine

pGround

private double pGround

pOther

private double pOther

spawnTries

private int spawnTries
Constructor Detail

Dispatch

private Dispatch()
Builds a dispatch; private to control when the single instance is instantiated.

Method Detail

instance

public static Dispatch instance()
Returns a reference to the single instance of this class.

Returns:
the dispatch

updateState

public void updateState()
Instructs the dispatch to move to the next iteration of the simulation, spawning new passengers and adding them to the simulation.


spawnPassengers

private void spawnPassengers()
Spawns new passengers randomly and adds them to the simulation. This will potentially spawn passengers on the ground floor headed up and on other floors headed down or between floors.


readPassenger

private Passenger readPassenger()
Attempts to read information for a single passenger in from the input file. If the file is open and the next line specifies passenger information for the current iteration, a Passenger with the information on that line is created and returned. Otherwise, returns null.

Returns:
a new passenger object or null if one should not be created at this time

producePassenger

private void producePassenger(Passenger user)
Injects a passenger into the simulation; this adds the passenger to the elevator bank, initially waiting at their start floor.

Parameters:
user - the passenger to add to the elevator bank.

consumePassenger

public void consumePassenger(Passenger user,
                             Elevator lift)
Receives a passenger from the simulation. Checks that the passenger has spent an appropriate amount of time in the simulation. Accumulates statistics for the time the users have spent with the elevator.

Parameters:
user - the passenger getting off the elevator
lift - the elevator the passenger is getting off

readFromFile

public void readFromFile(String filename)
Sets up this class to read simulation data in from a file. This controls when and how many users get added to the simulation.

Parameters:
filename - the file name to read data in from

writeToFile

public void writeToFile(String filename)
Sets up this class to write simulation data out to a file. This enables saving when and how many users get added to the simulation so that the same exact conditions can be played back at a later date.

Parameters:
filename - the file name to write data out to

formatStatistics

public String formatStatistics()
Returns a string containing statistics. This includes the number of passengers produced and consumed and the min/max/mean wait time for all passengers that have reached their destination.


closeFiles

public void closeFiles()
Closes any files opened by this dispatch.


setBank

public void setBank(ElevatorBank bank)
Sets the elevator bank that this Dispatch spawns passengers for.

Parameters:
bank - the elevator bank

setLandscape

public void setLandscape(Landscape scape)
Sets the landscape that this Dispatch spawns passengers in.

Parameters:
scape - the landscape

setGroundProbability

public void setGroundProbability(double pGround)
Sets the probability that a passenger will be generated on the ground floor. This probability will be evaluated with a random number spawnTries number of times in each iteration. A higher number means there is a greater chance that a passenger is spawned.

Parameters:
pGround - the probability, in the range 0.0 – 1.0

setOtherProbability

public void setOtherProbability(double pOther)
Sets the probability that a passenger will be generated on an floor other than the ground floor. This probability will be evaluated with a random number spawnTries number of times in each iteration. A higher number means there is a greater chance that a passenger is spawned.

Parameters:
pOther - the probability, in the range 0.0 – 1.0

setSpawnTries

public void setSpawnTries(int spawnTries)
Sets the number of times that passengers could get spawned in each iteration. A higher number will mean more passengers get spawned at each iteration.

Parameters:
spawnTries - the number of spawning attempts