cscie160.hw1
Class Elevator

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

public class Elevator
extends java.lang.Object

CSCIE160

Assignment 1: Elevator part 1

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

By David Cheung


Field Summary
 int Capacity
          The maximum number of people in an Elevator.
 int currentDirection
          The current direction of the elevator: 1=up, -1=down and 0=not moving.
 int currentFloor
          The current floor that the Elevator is on.
 boolean[] destRequests
          Stores the destination requests for each floor and the value would be boolean.
 int numOfFloors
          The number of floors in the building.
 int[] numOfPassDestForEachFloor
          Stores the number of passengers destined for each floor.
 int numOfPassengers
          The number of passengers currently in the elevator
 
Constructor Summary
Elevator()
          Initializes class by creating an array with size of numOfFloors + 1.
 
Method Summary
 void boardPassenger(int Floor)
          This increments numb of passenger destined there by one, and increase the number of passengers in elevator by one.
static void main(java.lang.String[] argv)
          Starts the main part of the class, start with adding passengers and then use a FOR loop to do a 1-round sweep.
 void move()
           Moves Elevator from its current floor to the next floor.
 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

Capacity

public final int Capacity
The maximum number of people in an Elevator. This is hardcoded to 10.

numOfFloors

public final int numOfFloors
The number of floors in the building. This is hardcoded to 7.

currentFloor

public int currentFloor
The current floor that the Elevator is on.

currentDirection

public int currentDirection
The current direction of the elevator: 1=up, -1=down and 0=not moving.

numOfPassengers

public int numOfPassengers
The number of passengers currently in the elevator

destRequests

public boolean[] destRequests
Stores the destination requests for each floor and the value would be boolean. Therefore the size of this array should be numOfFloors.

numOfPassDestForEachFloor

public int[] numOfPassDestForEachFloor
Stores the number of passengers destined for each floor. The size of array should be # of floor, and each value is number of passengers.
Constructor Detail

Elevator

public Elevator()
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.
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.

Subtracts numOfPassengers from numOfPassDestForEachFloor[currentFloor] and clears numOfPassDestForeachFloor to 0.


boardPassenger

public void boardPassenger(int Floor)
This increments numb of passenger destined there by one, and increase the number of passengers in elevator by one.
Parameters:
Floor - Destination floor for a passenger.

main

public static void main(java.lang.String[] argv)
Starts the main part of the class, start with adding passengers and then use a FOR loop to do a 1-round 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.