package simulation; import commandSystem.BasicInstructionQueue; import Events.CallFromElevatorEvent; import Events.CallFromFloorEvent; import commandSystem.Direction; import commandSystem.ElevatorListener; import simulation.Elevator; import Events.*; public class Simulation implements ElevatorListener { private boolean running = false; private int currentFloor = 0; private int secondToWait = 1; private BasicInstructionQueue queue = new BasicInstructionQueue(); private Elevator elevator; public Simulation(Elevator elevator) { this.elevator = elevator; } public void elevatorCall(int floor) { System.out.println("Call from elevator: " + floor); queue.computeEvent(new CallFromElevatorEvent(floor)); update(); } public void floorCall(int floor, Direction direction) { System.out.println("Call from floor: " + floor + " going " + direction); queue.computeEvent(new CallFromFloorEvent(floor, direction)); update(); } public void floorChange(Direction direction) { System.out.println("Floor reached: " + direction); boolean haveToWait; switch (direction) { case UP: currentFloor++; break; case DOWN: currentFloor--; break; } haveToWait = queue.computeEvent(new ReachedFloorEvent(currentFloor, direction)); if (haveToWait) { long t1 = System.nanoTime(); elevator.stop(); running = false; while ((System.nanoTime() - t1) / 1_000_000_000 < secondToWait); } update(); } public void emergency() { elevator.emergencyStop(); } public void cancelEmergency() { queue = new BasicInstructionQueue(); elevator.cancelEmergency(); } private void update() { final int next = queue.getNextInstruction(); System.out.println("Current floor: " + currentFloor); System.out.println("Next instruction: " + next); if (next == -1 || next == currentFloor) { System.out.println("Stopping."); elevator.stop(); running = false; return; } else if (next > currentFloor) { System.out.print("Going up."); running = true; elevator.goUp(); } else if (next < currentFloor) { System.out.print("Going down."); running = true; elevator.goDown(); } if (next == currentFloor + 1 || next == currentFloor - 1) { System.out.print("\b (slowly)."); running = true; elevator.stopNextFloor(); } System.out.println("\n"); } }