74 lines
1.8 KiB
Java
74 lines
1.8 KiB
Java
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 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);
|
|
switch (direction) {
|
|
case UP:
|
|
currentFloor++;
|
|
break;
|
|
case DOWN:
|
|
currentFloor--;
|
|
break;
|
|
}
|
|
queue.computeEvent(new ReachedFloorEvent(currentFloor, direction));
|
|
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("Next instruction: " + next);
|
|
if (next == -1 || next == currentFloor) {
|
|
System.out.println("Stopping.");
|
|
elevator.stop();
|
|
}
|
|
else if (next > currentFloor) {
|
|
System.out.println("Going up.");
|
|
elevator.goUp();
|
|
}
|
|
else if (next < currentFloor) {
|
|
System.out.println("Going down.");
|
|
elevator.goDown();
|
|
}
|
|
}
|
|
}
|