Class Elevator

java.lang.Object
  extended by Cell
      extended by Elevator

public class Elevator
extends Cell

Represents an elevator in an elevator simulation. The elevator travels between floors picking up and dropping off passengers. It maintains a priority queue of passengers riding in the elevator. It extends the basic Cell class, which handles position information.


Nested Class Summary
static class Elevator.Direction
          Represents the current traveling state of an elevator.
 
Field Summary
private  ElevatorBank bank
          A link to the elevator bank
private  int currentFloor
          The current floor of the elevator
private  Elevator.Direction direction
          The current direction of motion of the elevator
static int DOOR_TIME
          the penalty incurred from stopping at a floor
static int FLOOR_DMAX
          the maximum number of floors one can move at a time
private  PassengerGroup passengers
          The priority queue of passengers in the elevator
private  int waitTime
          The wait time at each floor
 
Fields inherited from class Cell
type, x, y
 
Constructor Summary
Elevator(ElevatorBank bank, int capacity)
          Create an elevator in an elevator bank with a maximum capacity.
 
Method Summary
 void draw(Graphics g, int x0, int y0, int scale)
          Draws an elevator as a stack of floors.
private  int emptyRule()
          Determines what an elevator does when it is empty.
 int getCurrentFloor()
           
 Elevator.Direction getDirection()
          Gets the Direction the elevator is traveling.
 boolean isNeighbor(Cell c)
           
static void main(String[] args)
          Tests the elevator class by creating a new elevator and adding a few passengers.
private  int nonEmptyRule()
          Determines what an elevator does when it is not empty.
private  void openDoors()
          Default implementation for handling the case where the doors open on a particular floor.
 void setCurrentFloor(int floor)
           
 void setDirection(Elevator.Direction dir)
          Sets the direction the elevator is traveling.
 String toString()
          Returns a string representation of this elevator.
 void updateState(Landscape scape)
          Updates the elevator by dropping off or picking up passengers at the current floor, if needed, or moving one floor in either direction.
 
Methods inherited from class Cell
getCol, getRow, getType, getX, getY, setPosition, setType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DOOR_TIME

public static int DOOR_TIME
the penalty incurred from stopping at a floor


FLOOR_DMAX

public static int FLOOR_DMAX
the maximum number of floors one can move at a time


currentFloor

private int currentFloor
The current floor of the elevator


direction

private Elevator.Direction direction
The current direction of motion of the elevator


passengers

private PassengerGroup passengers
The priority queue of passengers in the elevator


bank

private ElevatorBank bank
A link to the elevator bank


waitTime

private int waitTime
The wait time at each floor

Constructor Detail

Elevator

public Elevator(ElevatorBank bank,
                int capacity)
Create an elevator in an elevator bank with a maximum capacity.

Parameters:
bank - the elevator bank this elevator belongs to
capacity - the maximum number of passengers in this elevator
Method Detail

getCurrentFloor

public int getCurrentFloor()

setCurrentFloor

public void setCurrentFloor(int floor)

getDirection

public Elevator.Direction getDirection()
Gets the Direction the elevator is traveling.

Returns:
the direction

setDirection

public void setDirection(Elevator.Direction dir)
Sets the direction the elevator is traveling. This modifies the ordering used on the passengers riding the elevator, and so should only be called when the elevator is empty.

Parameters:
dir - the new direction for the elevator

toString

public String toString()
Returns a string representation of this elevator. This includes the elevator's current floor, direction, number of passengers, current wait time before the doors close, and the passenger with highest priority. For example:

Elevator ( 0, UP): 2 pass, 0 wait, Passenger: [ 0 -> 3, 0]

Overrides:
toString in class Cell

openDoors

private void openDoors()
Default implementation for handling the case where the doors open on a particular floor.

Note that the direction for this elevator should be set before this function is called, otherwise we risk not ordering passengers properly as they board.

First, let as many passengers off at this floor as want to get off, then let anyone on who wants to travel in the same direction as the elevator.


emptyRule

private int emptyRule()
Determines what an elevator does when it is empty.

If the elevator is currently at a floor where it can pick up passengers, do so. The elevator turns around at the bottom and top floors. Otherwise, the elevator continues in the direction it was headed.

Returns a negative number to move down, positive number to move up, and 0 to stay put.

Returns:
an int that determines what direction to move

nonEmptyRule

private int nonEmptyRule()
Determines what an elevator does when it is not empty.

If the current floor is one where passengers need to be let off, the elevator opens its doors. Otherwise, the elevator continues in the direction it was headed.

Returns a negative number to move down, positive number to move up, and 0 to stay put.

Returns:
an int that indicates what direction to move

updateState

public void updateState(Landscape scape)
Updates the elevator by dropping off or picking up passengers at the current floor, if needed, or moving one floor in either direction.

Specified by:
updateState in class Cell

isNeighbor

public boolean isNeighbor(Cell c)
Specified by:
isNeighbor in class Cell

draw

public void draw(Graphics g,
                 int x0,
                 int y0,
                 int scale)
Draws an elevator as a stack of floors. Each floor is a rectangle labeled with the floor number. The elevator carriage is drawn as a solid rectangle on the elevator's current floor. If the elevator has passengers, the carriage has a filled in circle within it.

Specified by:
draw in class Cell

main

public static void main(String[] args)
Tests the elevator class by creating a new elevator and adding a few passengers.

Parameters:
args - not used