add emergency management callbacks and emergency cancel button

This commit is contained in:
COLIN Cyril 2019-10-16 13:41:16 +02:00
parent f58b035252
commit 7a1ca1ef65
4 changed files with 29 additions and 14 deletions

View File

@ -5,4 +5,6 @@ public interface ElevatorListener {
public void elevatorCall(int floor);
public void floorCall(int floor, Direction direction);
public void floorChange(Direction direction);
public void emergency();
public void cancelEmergency();
}

View File

@ -9,6 +9,7 @@ import commandSystem.ElevatorListener;
@SuppressWarnings("serial")
class ElevatorPanel extends JPanel {
private JButton emergencyStop = new JButton("Emergency stop.");
private JButton cancelEmergencyStop = new JButton("Cancel emergency stop.");
private JButton[] buttons;
public ElevatorPanel(int nbFloors, ElevatorListener l) {
@ -26,10 +27,17 @@ class ElevatorPanel extends JPanel {
});
this.add(buttons[i]);
}
emergencyStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.emergency();
}
});
add(emergencyStop);
}
public void addEmergencyStopListener(ActionListener l) {
emergencyStop.addActionListener(l);
cancelEmergencyStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.cancelEmergency();
}
});
add(cancelEmergencyStop);
}
}

View File

@ -82,9 +82,6 @@ public class Elevator {
break;
case UP:
height += step;
System.out.println("height: " + height
+ ", prevFloor: " + prevFloor
+ ", floor: " + Math.floor(height / floorHeight));
/* Top reached. */
if (height > 1-step) {

View File

@ -20,19 +20,19 @@ public class Simulation implements ElevatorListener {
}
public void elevatorCall(int floor) {
System.out.println("elevator call " + floor);
System.out.println("Call from elevator: " + floor);
queue.computeEvent(new CallFromElevatorEvent(floor));
update();
}
public void floorCall(int floor, Direction direction) {
System.out.println("floor call " + floor + " " + 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 change " + direction);
System.out.println("Floor reached: " + direction);
switch (direction) {
case UP:
currentFloor++;
@ -45,19 +45,27 @@ public class Simulation implements ElevatorListener {
update();
}
public void emergency() {
System.out.println("Emergency declared.");
}
public void cancelEmergency() {
System.out.println("Emergency cancelled.");
}
private void update() {
final int next = queue.getNextInstruction();
System.out.println("next: " + next);
System.out.println("Next instruction: " + next);
if (next == -1 || next == currentFloor) {
System.out.println("stopping");
System.out.println("Stopping.");
elevator.stop();
}
else if (next > currentFloor) {
System.out.println("going up");
System.out.println("Going up.");
elevator.goUp();
}
else if (next < currentFloor) {
System.out.println("going down");
System.out.println("Going down.");
elevator.goDown();
}
}