This commit is contained in:
Oriane 2019-10-16 16:35:06 +02:00
commit 9aeb29c895
14 changed files with 1974 additions and 112 deletions

BIN
Cahier_des_charges.pdf Normal file

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

View File

@ -4,10 +4,10 @@ import commandSystem.Direction;
public class CallFromElevatorEvent implements Event {
private final int wantedFloor;
private final int requestedFloor;
public CallFromElevatorEvent(int wantedFloor) {
this.wantedFloor = wantedFloor;
public CallFromElevatorEvent(int requestedFloor) {
this.requestedFloor = requestedFloor;
}
@Override
@ -17,7 +17,7 @@ public class CallFromElevatorEvent implements Event {
@Override
public int getRequestedFloor() {
return wantedFloor;
return requestedFloor;
}
@Override

View File

@ -12,7 +12,7 @@ public class BasicInstructionQueue implements InstructionQueue {
private boolean emergencyState;
private int currentFloor;
private Direction direction;
private Direction currentDirection;
public BasicInstructionQueue() {
@ -20,22 +20,7 @@ public class BasicInstructionQueue implements InstructionQueue {
downQueue = new ArrayList<Integer>();
emergencyState = false;
currentFloor = 0;
direction = Direction.NONE;
}
/**
* Adds a new floor to the argument's <b>queue</b> then sorts the elements.
*
* @param queue the list to be sorted.
* @param floor the index of the floor that will be added.
* @param reversed if set to true, the queue will be sorted by descending order.
*/
private void appSort(ArrayList<Integer> queue, int floor, boolean reversed) {
queue.add(floor);
if (reversed)
Collections.sort(queue, Collections.reverseOrder());
else
Collections.sort(queue);
currentDirection = Direction.UP;
}
public void computeEvent(Event event) {
@ -79,7 +64,7 @@ public class BasicInstructionQueue implements InstructionQueue {
/*
* If the requested floor is under the elevator, adds the instruction to the
* downQueue then sorts it. if it's above, adds the instruction to the upQueue
* then sorts it. If it's the same floor, doesn't do anything
* then sorts it. If it's the same floor, doesn't do anything.
*/
case CALLFROMELEVATOR:
if (event.getRequestedFloor() > currentFloor && !upQueue.contains(event.getRequestedFloor()))
@ -94,7 +79,7 @@ public class BasicInstructionQueue implements InstructionQueue {
*/
case EMERGENCYSTOP:
emergencyState = true;
clearQueue();
clearQueues();
break;
/*
@ -109,8 +94,9 @@ public class BasicInstructionQueue implements InstructionQueue {
*/
case REACHEDFLOORSIGNAL:
currentFloor = event.getCurrentFloor();
direction = event.getCurrentDirection();
removeInstruction(currentFloor);
currentDirection = event.getCurrentDirection();
if (currentFloor == getNextInstruction())
removeInstruction(currentFloor);
break;
default:
@ -118,32 +104,6 @@ public class BasicInstructionQueue implements InstructionQueue {
}
}
/**
* Removes one instruction from the queue - has to be called when the elevator
* reaches targeted floor.
*
* @param floor the reached floor
* @param direction the direction of the elevator
*/
private void removeInstruction(int floor) {
switch (direction) {
case UP:
if (upQueue.contains(floor))
upQueue.remove(upQueue.indexOf(floor));
else // we go up to the top of descending queue
downQueue.remove(downQueue.indexOf(floor));
break;
case DOWN:
if (downQueue.contains(floor))
downQueue.remove(downQueue.indexOf(floor));
else // we go down to the bottom of ascending queue
upQueue.remove(upQueue.indexOf(floor));
break;
default:
break;
}
}
/**
* Returns the next floor the elevator has to go to
*
@ -151,7 +111,10 @@ public class BasicInstructionQueue implements InstructionQueue {
*/
public int getNextInstruction() {
int nextFloor = -1;
switch (direction) {
//System.out.println("" + upQueue + downQueue);
switch (currentDirection) {
// get the first element of upQueue that is ABOVE the elevator's current floor
case UP:
for (int i = 0; i < upQueue.size() && nextFloor < 0; i++) {
@ -185,7 +148,60 @@ public class BasicInstructionQueue implements InstructionQueue {
return nextFloor;
}
private void clearQueue() {
/**
* Adds a new floor to the argument's <b>queue</b> then sorts the elements.
*
* @param queue the list to be sorted.
* @param floor the index of the floor that will be added.
* @param reversed if set to true, the queue will be sorted by descending order.
*/
private void appSort(ArrayList<Integer> queue, int floor, boolean reversed) {
queue.add(floor);
if (reversed)
Collections.sort(queue, Collections.reverseOrder());
else
Collections.sort(queue);
}
/**
* Removes one instruction from the queue - has to be called when the elevator
* reaches targeted floor.
*
* @param floor the reached floor.
* @param currentDirection the direction of the elevator.
*/
private void removeInstruction(int floor) {
switch (currentDirection) {
/*
* Elevator is headed upwards.
*/
case UP:
if (upQueue.contains(floor))
upQueue.remove(upQueue.indexOf(floor));
else
downQueue.remove(downQueue.indexOf(floor));
break;
/*
* Elevator is headed downwards.
*/
case DOWN:
if (downQueue.contains(floor))
downQueue.remove(downQueue.indexOf(floor));
else
upQueue.remove(upQueue.indexOf(floor));
break;
default:
break;
}
}
/**
* Clears both queues.
*/
private void clearQueues() {
downQueue.removeAll(downQueue);
upQueue.removeAll(upQueue);
}

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

@ -17,4 +17,4 @@ public interface InstructionQueue {
* @return the next floor if it exists, else -1
*/
public int getNextInstruction();
}
}

View File

@ -7,21 +7,24 @@ import Events.ReachedFloorEvent;
public class Test {
private static boolean running = false;
private static BasicInstructionQueue queue;
private static InstructionQueue queue;
private static Direction currentDirection;
private static int currentFloor;
private static Direction currentDirection = Direction.UP;
private static int currentFloor = 0;
public static void iter() {
int nextDestination = queue.getNextInstruction();
if (!running) {
// would be -1 if the queue doesn't have any next instruction
//System.out.println("next dest : " + nextDestination);
if (nextDestination != -1) {
running = true;
currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
}
} else {
//System.out.println("not running");
// if we just reached a new floor
if (currentFloor == nextDestination) {
queue.computeEvent(new ReachedFloorEvent(currentFloor, currentDirection));
@ -32,16 +35,14 @@ public class Test {
}
}
System.out.println("elevator floor " + currentFloor + " direction " +
(currentDirection == Direction.UP? "up" : "down") + " running: " + running);
(currentDirection) + " running: " + running);
}
public static void main(String[] args) {
//TODO envoyer les notifs de changements d'étages
queue = new BasicInstructionQueue();
iter();
iter();
queue.computeEvent(new CallFromFloorEvent(2, Direction.DOWN));
iter();
System.out.println("someone calls from floor 2 to go down");
iter();
queue.computeEvent(new CallFromFloorEvent(1, Direction.UP));

View File

@ -26,7 +26,8 @@ public class ElevatorApplication implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
canvas.setElevatorY((canvas.getElevatorY() + 1) % canvas.getHeight());
elevator.update();
canvas.repaint();
}
private void createAndShowGUI() {
@ -42,11 +43,13 @@ public class ElevatorApplication implements ActionListener {
canvas.setAlignmentY(Component.TOP_ALIGNMENT);
pane.add(canvas);
pane.add(Box.createRigidArea(new Dimension(40, 0)));
FloorPanels fp = new FloorPanels(5, simulation);
fp.setAlignmentY(Component.TOP_ALIGNMENT);
pane.add(fp);
pane.add(Box.createRigidArea(new Dimension(20, 0)));
pane.add(Box.createRigidArea(new Dimension(40, 0)));
ElevatorPanel elevatorPanel = new ElevatorPanel(5, simulation);
elevatorPanel.setAlignmentY(Component.TOP_ALIGNMENT);

View File

@ -11,7 +11,6 @@ class ElevatorCanvas extends JPanel {
private final static int HEIGHT = 50;
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200);
private int y = 0;
private Elevator elevator;
public ElevatorCanvas(Elevator elevator) {
@ -46,15 +45,7 @@ class ElevatorCanvas extends JPanel {
else {
g.setColor(Color.BLACK);
}
g.fillRect(0, y, WIDTH, HEIGHT);
}
public void setElevatorY(int y) {
this.y = y;
repaint();
}
public int getElevatorY() {
return y;
g.fillRect(0, 200 - (int) Math.floor(elevator.getHeight() * 200) - HEIGHT, WIDTH, HEIGHT);
Toolkit.getDefaultToolkit().sync();
}
}

View File

@ -1,6 +1,7 @@
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import commandSystem.ElevatorListener;
@ -9,6 +10,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) {
@ -25,11 +27,20 @@ class ElevatorPanel extends JPanel {
}
});
this.add(buttons[i]);
this.add(Box.createRigidArea(new Dimension(0, 5)));
}
emergencyStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.emergency();
}
});
add(emergencyStop);
}
public void addEmergencyStopListener(ActionListener l) {
emergencyStop.addActionListener(l);
this.add(Box.createRigidArea(new Dimension(0, 5)));
cancelEmergencyStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
l.cancelEmergency();
}
});
add(cancelEmergencyStop);
}
}

View File

@ -22,6 +22,7 @@ class FloorPanels extends JPanel {
floors[i] = new JPanel();
floors[i].setLayout(new BoxLayout(floors[i], BoxLayout.LINE_AXIS));
floors[i].add(new JLabel("" + i));
floors[i].add(Box.createRigidArea(new Dimension(5, 0)));
if (i < nbFloors - 1) {
final int j = i;
JButton upButton = new JButton("");
@ -31,6 +32,7 @@ class FloorPanels extends JPanel {
}
});
floors[i].add(upButton);
floors[i].add(Box.createRigidArea(new Dimension(5, 0)));
}
if (i > 0) {
final int j = i;
@ -43,6 +45,7 @@ class FloorPanels extends JPanel {
floors[i].add(downButton);
}
this.add(floors[i]);
this.add(Box.createRigidArea(new Dimension(0, 5)));
}
}
}

View File

@ -23,6 +23,7 @@ public class Elevator {
height = (double) currentFloor / nbFloors;
floorHeight = 1. / nbFloors;
step = floorHeight / PRECISION;
System.out.println("step: " + step + ", floorHeight: " + floorHeight);
}
public void setActionListener(ElevatorListener listener) {
@ -41,7 +42,7 @@ public class Elevator {
direction = Direction.DOWN;
}
public void stopElevator() {
public void stop() {
direction = Direction.NONE;
}
@ -73,7 +74,7 @@ public class Elevator {
/* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
listener.floorChange(Direction.UP);
listener.floorChange(Direction.DOWN);
}
// /* Go halfway below. */
// if (!betweenFloors) currentFloor--;
@ -89,8 +90,8 @@ public class Elevator {
}
/* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
listener.floorChange(Direction.DOWN);
if (Math.floor(height / floorHeight) > prevFloor && listener != null) {
listener.floorChange(Direction.UP);
}
// /* Go halfway above. */
// if (betweenFloors) currentFloor++;

View File

@ -6,12 +6,13 @@ 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;
private BasicInstructionQueue queue = new BasicInstructionQueue();
private Elevator elevator;
public Simulation(Elevator elevator) {
@ -19,15 +20,20 @@ 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 d) {
switch (d) {
public void floorChange(Direction direction) {
System.out.println("Floor reached: " + direction);
switch (direction) {
case UP:
currentFloor++;
break;
@ -35,30 +41,32 @@ public class Simulation implements ElevatorListener {
currentFloor--;
break;
}
// iter();
queue.computeEvent(new ReachedFloorEvent(currentFloor, direction));
update();
}
// public void iter() {
// int nextDestination = queue.getNextInstruction();
// int currentFloor = 0;
// //System.out.println("next dest = " + nextDestination);
// if (!running) {
// if (nextDestination > 0) { /* We have a destination to go to. */
// running = true;
// elevator.goDown();
// }
// }
// else {
// if (elevator.currentFloor == nextDestination) {
// queue.removeInstruction(elevator.currentFloor, elevator.direction);
// running = false;
// }
// else {
// elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
// elevator.currentFloor += elevator.direction == Direction.UP ? 1 : -1;
// }
// }
// System.out.println("elevator floor " + elevator.currentFloor + " direction " +
// (elevator.direction == Direction.UP? "up" : "down") + " running: " + running);
// }
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 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();
}
}
}