/* * Building with elevators and people for simulation * Created on May 10, 2004 * @author kim */ public class Building { // number of floors in building public final int NUM_FLOORS; // number of elevators in building public final int NUM_ELEVATORS; // array of elevators private Elevator[] lift; /** * Create building with * @param numFloors -- number of floors * @param numElevators -- number of elevators * @param elevatorCapacity -- each elevator's capacity * @param serviceFloorMS -- length of time elevator should stay on floor * post: elevators start on successive floors. */ public Building(int numFloors, int numElevators, int elevatorCapacity, int serviceFloorMS, int travelTimeMS) { NUM_FLOORS = numFloors; NUM_ELEVATORS = numElevators; lift = new Elevator[numElevators]; for (int liftNum = 0; liftNum < numElevators; liftNum++) { lift[liftNum] = new Elevator("lift "+liftNum, NUM_FLOORS, liftNum % numFloors, elevatorCapacity, this, serviceFloorMS, travelTimeMS); } } // post: start all of the elevators running public void startElevators(){ for (int liftNum = 0; liftNum < NUM_ELEVATORS; liftNum++) { lift[liftNum].start(); } } // post: stop all of the elevators from running public void stopElevators(){ for (int liftNum = 0; liftNum < NUM_ELEVATORS; liftNum++) { lift[liftNum].stopElevator(); } } // post: notified all of those waiting that an elevator // has arrived at currentFloor public synchronized void tellAt() { notifyAll(); } /* * post: Returns the first elevator to reach the current floor that is * going the right direction. Wait if necessary for one to arrive. */ public synchronized Elevator callElevator(int personFloor, boolean goingUp){ while (true) { for (int liftNum = 0; liftNum < NUM_ELEVATORS; liftNum++) { if(lift[liftNum].getCurrentFloor() == personFloor && lift[liftNum].isGoingUp() == goingUp) { return lift[liftNum]; } } try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitForElevatorToCome() { try{ wait(); } catch (InterruptedException e) { e.printStackTrace(); } } }