Class ElevatorBank

java.lang.Object
  extended by ElevatorBank

public class ElevatorBank
extends Object

Represents a collection of elevators that operate independently. The bank maintains a list of all the elevators in the bank; an individual elevator can be accessed by ElevatorBank.getElevator(index).

The bank also maintains queues of passengers on each floor that have requested to travel to a different floor. There are two queues for each floor, one for passengers traveling to a higher floor and one for passengers traveling to a lower floor.


Field Summary
private  Queue[] downQueues
           
private  Elevator[] elevators
           
private  Queue[] upQueues
           
 
Constructor Summary
ElevatorBank(int lifts, int floors, int capacity)
          Initializes an elevator bank with the given number of elevators and floors.
 
Method Summary
 void addPassenger(Passenger user)
          Adds a passenger to the bank by placing them in the proper queue on their start floor.
 Elevator getElevator(int index)
          Gets the elevator with the given index.
 int getElevatorCount()
           
 int getFloorCount()
           
 Queue getFloorQueue(Elevator lift)
          Gets a queue of passengers waiting to board an elevator.
 Passenger getNearestPassenger(Elevator lift)
          Returns a reference to the passenger that is closest to the given elevator in the direction it is traveling.
private  boolean isFloorValid(int floor)
          Determines whether a given floor index is valid for this bank.
static void main(String[] args)
          Tests the elevator bank by creating a new bank, adding passengers to it, and seeing where an elevator would stop traveling up and down.
 String toString()
          Returns a string representation of the elevator bank.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

elevators

private Elevator[] elevators

upQueues

private Queue[] upQueues

downQueues

private Queue[] downQueues
Constructor Detail

ElevatorBank

public ElevatorBank(int lifts,
                    int floors,
                    int capacity)
Initializes an elevator bank with the given number of elevators and floors.

Parameters:
lifts - the number of elevators
floors - the number of floors
Method Detail

getFloorCount

public int getFloorCount()

getElevatorCount

public int getElevatorCount()

getElevator

public Elevator getElevator(int index)
Gets the elevator with the given index.

Parameters:
index - the elevator index
Returns:
the elevator, or null if the index is invalid

isFloorValid

private boolean isFloorValid(int floor)
Determines whether a given floor index is valid for this bank.

Parameters:
floor - the floor index
Returns:
true if the floor is valid, false otherwise

addPassenger

public void addPassenger(Passenger user)
Adds a passenger to the bank by placing them in the proper queue on their start floor. This method checks to make sure the passenger has valid start and end floors for this elevator bank.

Parameters:
user - the passenger to add

getNearestPassenger

public Passenger getNearestPassenger(Elevator lift)
Returns a reference to the passenger that is closest to the given elevator in the direction it is traveling. If the elevator is idle, this is the nearest passenger traveling in either direction. If the elevator is moving in a direction, this is the closest passenger in that direction.

Parameters:
lift - the elevator to consider
Returns:
the closest passenger or null if a suitable one does not exist.

getFloorQueue

public Queue getFloorQueue(Elevator lift)
Gets a queue of passengers waiting to board an elevator. This queue contains passengers at the given elevator's current floor who would like to travel in the same direction the elevator is traveling. In other words, this is the queue from which passengers would board the given elevator if there is room.

Note that this method prefers the upwards queue when the elevator direction is idle.

Parameters:
lift - the elevator that is stopped at a floor
Returns:
the queue of passengers or null if the elevator's floor is invalid

toString

public String toString()
Returns a string representation of the elevator bank. This string contains the number of elevators and floors in the elevator bank as well as the count of passengers waiting in each queue on each floor. For example:

 Bank ( 3 els,   5 fls):
    4:  0 u,  1 d
    3:  1 u,  0 d
    2:  0 u,  1 d
    1:  1 u,  0 d
    0:  0 u,  0 d
 

Overrides:
toString in class Object

main

public static void main(String[] args)
Tests the elevator bank by creating a new bank, adding passengers to it, and seeing where an elevator would stop traveling up and down.

Parameters:
args - not used