cscie160.hw3
Class Elevator

java.lang.Object
  |
  +--cscie160.hw3.Elevator

public class Elevator
extends java.lang.Object

CSCIE160

Assignment 3: Elevator part 3

This is a partial simulation of an Elevator by using the sweep algorithm.

By David Cheung


Field Summary
 Floor[] arrayOfFloors
          Stores the array of floors that the Elevator travels upon.
 java.util.LinkedList[] llPassDestForEachFloor
          Stores passengers destined for each floor.
 
Constructor Summary
Elevator(Floor[] arrFloors)
          Initializes class by creating an array with size of numOfFloors + 1.
 
Method Summary
 void boardPassenger(Passenger aPass)
          This increments number of passenger destined there by one, and increase the number of passengers in elevator by one.
 int getCurrentDirection()
          getCurrentDirection returns the integer value of the direction: 1 = up, -1 = down, 0 = idle.
 int getCurrentFloor()
          getCurrentFloor gets the current floor of the elevator
 int getNumOfPassengers()
          The number of passengers currently in the elevator
static void main(java.lang.String[] argv)
          Starts the main part of the class, start with adding passengers in the Elevator and then add a bunch of passengers on some floors.
 void move()
           Moves Elevator from its current floor to the next floor.
 void registerDownRequest(int numFloor)
          Sets the destRequests array.
 void registerUpRequest(int numFloor)
          Sets the destUpRequests array.
 void stop()
           Does not actually stop the elevator in real time but just to allow the simulation to process the numbers when stop is called.
 java.lang.String toString()
          Prints the current status of the elevator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

llPassDestForEachFloor

public java.util.LinkedList[] llPassDestForEachFloor
Stores passengers destined for each floor. The size of array should be # of floor, and each slot is a linklist of passengers.

Modified from numOfPassDestForEachFloor, which was an array of int. Now it is an array of linklist. This can be implemented with a hashtable as well,but it seems to me link list works too.


arrayOfFloors

public Floor[] arrayOfFloors
Stores the array of floors that the Elevator travels upon.
Constructor Detail

Elevator

public Elevator(Floor[] arrFloors)
Initializes class by creating an array with size of numOfFloors + 1. Sets initial direction to UP.
Method Detail

move

public void move()

Moves Elevator from its current floor to the next floor. This depends on which direction the floor moves.

If the floor is at the top, this will switch the direction of the floor to down (-1) and up (1) if the elevator is at the lowest floor.

Calls the stop function if there is a floor that needs to be stopped.

Calls toString function at the end of this.

See Also:
stop(), toString()

toString

public java.lang.String toString()
Prints the current status of the elevator. Called by move each time move is executed.

Added for HW 2, this prints the number of people on each of the floors of the elevator.

Overrides:
toString in class java.lang.Object
See Also:
move()

stop

public void stop()

Does not actually stop the elevator in real time but just to allow the simulation to process the numbers when stop is called.

Now it is changed so that unloadPassengers does the passenger pulling and loading the passengers to the elevator.

Before calling unloadPassenger stop would set the destRequests off for that floor.

See Also:
Floor.unloadPassengers(Elevator)

boardPassenger

public void boardPassenger(Passenger aPass)
                    throws ElevatorFullException
This increments number of passenger destined there by one, and increase the number of passengers in elevator by one.

Called by Floor.unloadPassengers March 14, 2001: Add support for passenger instead of using integer.

Parameters:
Passenger - A passenger to be boarded.
See Also:
Floor.unloadPassengers(cscie160.hw3.Elevator)

registerUpRequest

public void registerUpRequest(int numFloor)
Sets the destUpRequests array. Called by Floor.unloadPassengers.
Parameters:
numFloor - The floor number of the floor that is making the request.
See Also:
Floor.unloadPassengers(cscie160.hw3.Elevator)

registerDownRequest

public void registerDownRequest(int numFloor)
Sets the destRequests array. Called by Floor.unloadPassengers.
Parameters:
numFloor - The floor number of the floor that is making the request.
See Also:
Floor.unloadPassengers(cscie160.hw3.Elevator)

getNumOfPassengers

public int getNumOfPassengers()
The number of passengers currently in the elevator

used to be a data member now it is a method.


getCurrentFloor

public int getCurrentFloor()
getCurrentFloor gets the current floor of the elevator

getCurrentDirection

public int getCurrentDirection()
getCurrentDirection returns the integer value of the direction: 1 = up, -1 = down, 0 = idle.

main

public static void main(java.lang.String[] argv)
Starts the main part of the class, start with adding passengers in the Elevator and then add a bunch of passengers on some floors.

Used a FOR loop to do a 2 round trip sweep. Note: I used a FOR loop instead of WHILE loop because FOR loop allows me to look at the data after the program ends. Whereas WHILE loop will generate a lot of unnecessary data.