From e0280d7e181e2fdcbd4438105b02aa527df6476c Mon Sep 17 00:00:00 2001 From: MathieuPietri Date: Fri, 11 Oct 2019 17:59:39 +0200 Subject: [PATCH] merge avec le github + j'ai mis les trucs de l'interface dans un package gui --- src/Requests/CallFromElevatorRequest.java | 10 ++ src/Requests/CallFromFloorRequest.java | 15 ++ src/Requests/EmergencyStopRequest.java | 9 ++ src/Requests/Request.java | 46 +++++++ src/Requests/RequestType.java | 5 + src/commandSystem/Direction.java | 5 + src/commandSystem/Elevator.java | 9 ++ src/commandSystem/EventQueue.java | 160 ++++++++++++++++++++++ src/commandSystem/Test.java | 63 +++++++++ src/commandSystem/TestElevator.java | 30 ++++ src/{ => gui}/Direction.java | 2 + src/{ => gui}/Elevator.java | 1 + src/{ => gui}/ElevatorApplication.java | 1 + src/{ => gui}/ElevatorCanvas.java | 1 + src/{ => gui}/ElevatorPanel.java | 1 + src/{ => gui}/FloorPanels.java | 1 + src/module-info.java | 3 + 17 files changed, 362 insertions(+) create mode 100644 src/Requests/CallFromElevatorRequest.java create mode 100644 src/Requests/CallFromFloorRequest.java create mode 100644 src/Requests/EmergencyStopRequest.java create mode 100644 src/Requests/Request.java create mode 100644 src/Requests/RequestType.java create mode 100644 src/commandSystem/Direction.java create mode 100644 src/commandSystem/Elevator.java create mode 100644 src/commandSystem/EventQueue.java create mode 100644 src/commandSystem/Test.java create mode 100644 src/commandSystem/TestElevator.java rename src/{ => gui}/Direction.java (79%) rename src/{ => gui}/Elevator.java (99%) rename src/{ => gui}/ElevatorApplication.java (99%) rename src/{ => gui}/ElevatorCanvas.java (98%) rename src/{ => gui}/ElevatorPanel.java (97%) rename src/{ => gui}/FloorPanels.java (97%) create mode 100644 src/module-info.java diff --git a/src/Requests/CallFromElevatorRequest.java b/src/Requests/CallFromElevatorRequest.java new file mode 100644 index 0000000..ab3acad --- /dev/null +++ b/src/Requests/CallFromElevatorRequest.java @@ -0,0 +1,10 @@ +package Requests; + +public class CallFromElevatorRequest extends Request { + + public CallFromElevatorRequest(int wantedFloor) { + super(); + this.wantedFloor = wantedFloor; + this.type = RequestType.CALLFROMELEVATOR; + } +} diff --git a/src/Requests/CallFromFloorRequest.java b/src/Requests/CallFromFloorRequest.java new file mode 100644 index 0000000..317662d --- /dev/null +++ b/src/Requests/CallFromFloorRequest.java @@ -0,0 +1,15 @@ +package Requests; + +import commandSystem.Direction; + +public class CallFromFloorRequest extends Request { + + + public CallFromFloorRequest(int sourceFloor, Direction direction) { + super(); + this.sourceFloor = sourceFloor; + this.direction = direction; + this.type = RequestType.CALLFROMFLOOR; + } + +} diff --git a/src/Requests/EmergencyStopRequest.java b/src/Requests/EmergencyStopRequest.java new file mode 100644 index 0000000..e0552dd --- /dev/null +++ b/src/Requests/EmergencyStopRequest.java @@ -0,0 +1,9 @@ +package Requests; + +public class EmergencyStopRequest extends Request { + + public EmergencyStopRequest() { + super(); + this.type = RequestType.EMERGENCYSTOP; + } +} diff --git a/src/Requests/Request.java b/src/Requests/Request.java new file mode 100644 index 0000000..4624e61 --- /dev/null +++ b/src/Requests/Request.java @@ -0,0 +1,46 @@ +package Requests; + +import commandSystem.Direction; + +public abstract class Request { + + protected RequestType type; + protected int wantedFloor; + protected int sourceFloor; + protected Direction direction; + + public Request() { + this.wantedFloor = -1; + this.sourceFloor = -1; + this.direction = Direction.NONE; + } + + /** + * + * @return the direction of the request, unconcerned extending classes return NONE + */ + public Direction getDirection() { + return direction; + } + + /** + * + * @return the wished floor, unconcerned extending classes return -1 + */ + public int getWantedFloor() { + return wantedFloor; + } + + /** + * + * @return the floor it was called from, unconcerned extending classes return -1 + */ + public int getIncomingCallFloor() { + return sourceFloor; + } + + + public RequestType getType() { + return type; + } +} diff --git a/src/Requests/RequestType.java b/src/Requests/RequestType.java new file mode 100644 index 0000000..6d4dc8d --- /dev/null +++ b/src/Requests/RequestType.java @@ -0,0 +1,5 @@ +package Requests; + +public enum RequestType { + CALLFROMFLOOR, CALLFROMELEVATOR, EMERGENCYSTOP, CANCELEMERGENCYSTOP +} diff --git a/src/commandSystem/Direction.java b/src/commandSystem/Direction.java new file mode 100644 index 0000000..45cb363 --- /dev/null +++ b/src/commandSystem/Direction.java @@ -0,0 +1,5 @@ +package commandSystem; + +public enum Direction { + UP, DOWN, NONE +} diff --git a/src/commandSystem/Elevator.java b/src/commandSystem/Elevator.java new file mode 100644 index 0000000..d97e8d3 --- /dev/null +++ b/src/commandSystem/Elevator.java @@ -0,0 +1,9 @@ +package commandSystem; + +public interface Elevator { + + + public void sendFloorReachedNotification(); + public int getCurrentFloor(); + public Direction getCurrentDirection(); +} diff --git a/src/commandSystem/EventQueue.java b/src/commandSystem/EventQueue.java new file mode 100644 index 0000000..5e24830 --- /dev/null +++ b/src/commandSystem/EventQueue.java @@ -0,0 +1,160 @@ +package commandSystem; + +import java.util.ArrayList; +import java.util.Collections; + +import Requests.Request; + +// emergencyStop -> clear the queue + +public class EventQueue { + private ArrayList upQueue = new ArrayList<>(); + private ArrayList downQueue = new ArrayList<>(); + private boolean emergencyState; + private Elevator elevator; + + public EventQueue(Elevator elevator) { + + this.elevator = elevator; + upQueue = new ArrayList(); + downQueue = new ArrayList(); + emergencyState = false; + } + + /** + * Add new floor to queue queue and sort it to keep floors order + * + * @param queue ArrayList + * @param floor int - the floor we add to queue + * @param reversed boolean - reverse queue order + */ + public void appSort(ArrayList queue, int floor, boolean reversed) { + queue.add(floor); + if (reversed) + Collections.sort(queue, Collections.reverseOrder()); + else + Collections.sort(queue); + } + + /** + * Compute a request into the {@link #EventQueue} depending on the elevator + * status + * + * @param request - the request to compute + */ + public void computeRequest(Request request) { + + switch (request.getType()) { + case CALLFROMFLOOR: + if (emergencyState) + return; + switch (request.getDirection()) { + case UP: + if (!upQueue.contains(request.getIncomingCallFloor())); + appSort(upQueue, request.getIncomingCallFloor(), false); + break; + case DOWN: + if (!downQueue.contains(request.getIncomingCallFloor())) + appSort(downQueue, request.getIncomingCallFloor(), true); + break; + default: + System.out.println("foiop"); + break; + } + break; + case CALLFROMELEVATOR: + if (emergencyState) + return; + if (request.getWantedFloor() > elevator.getCurrentFloor() && !upQueue.contains(request.getWantedFloor())) + appSort(upQueue, request.getWantedFloor(), false); + else if (request.getWantedFloor() < elevator.getCurrentFloor() + && !upQueue.contains(request.getWantedFloor())) + appSort(downQueue, request.getWantedFloor(), true); + break; + case EMERGENCYSTOP: + if (emergencyState) + return; + emergencyState = true; + clearQueue(); + break; + case CANCELEMERGENCYSTOP: + emergencyState = false; + break; + default: + break; + } + } + + /** + * removes one instruction from the queue - call when the elevator reaches + * targeted floor + * + * @param reachedFloor + * @param direction of elevators + */ + public void removeInstruction(int reachedFloor, Direction direction) { + switch (direction) { + case UP: + if (upQueue.contains(reachedFloor)) + upQueue.remove(upQueue.indexOf(reachedFloor)); + else // we go up to the top of descending queue + downQueue.remove(downQueue.indexOf(reachedFloor)); + break; + case DOWN: + if (downQueue.contains(reachedFloor)) + downQueue.remove(downQueue.indexOf(reachedFloor)); + else // we go down to the bottom of ascending queue + upQueue.remove(upQueue.indexOf(reachedFloor)); + break; + default: + break; + } + } + + /** + * Return the next floor the elevator has to go to + * + * @return next floor if exists else -1 + */ + public int getNextInstruction() { + //System.out.println("" + upQueue + '\n' + downQueue); + int nextFloor = -1; + switch (elevator.getCurrentDirection()) { + // get first element of upQueue that is ABOVE the elevator's current floor + case UP: + for (int i = 0; i < upQueue.size() && nextFloor < 0; i++) { + if (upQueue.get(i) >= elevator.getCurrentFloor()) { + nextFloor = upQueue.get(i); + } + } + // change direction if every floor in upQueue is UNDER the elevator's current + // floor + if (nextFloor < 0 && downQueue.size() > 0) { + nextFloor = downQueue.get(0); + } + break; + + // get first element of downQueue that is UNDER the elevator's current floor + case DOWN: + for (int i = 0; i < downQueue.size() && nextFloor < 0; i++) { + if (downQueue.get(i) <= elevator.getCurrentFloor()) { + nextFloor = downQueue.get(i); + } + } + // change direction if every floor in downQueue is ABOVE the elevator's current + // floor + if (nextFloor < 0 && upQueue.size() > 0) { + nextFloor = upQueue.get(0); + } + break; + default: + break; + } + return nextFloor; + } + + public void clearQueue() { + downQueue.removeAll(downQueue); + upQueue.removeAll(upQueue); + } +} \ No newline at end of file diff --git a/src/commandSystem/Test.java b/src/commandSystem/Test.java new file mode 100644 index 0000000..78d8f91 --- /dev/null +++ b/src/commandSystem/Test.java @@ -0,0 +1,63 @@ +package commandSystem; + +import Requests.CallFromElevatorRequest; +import Requests.CallFromFloorRequest; + +public class Test { + + private static boolean running = false; + private static EventQueue queue; + + private static TestElevator elevator = new TestElevator(); + + public static void iter() { + int nextDestination = queue.getNextInstruction(); + //System.out.println("next dest = " + nextDestination); + if (!running) { + if (nextDestination > 0) { // return -1 if no next destination + running = true; + elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN; + } + } 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 static void main(String[] args) { + queue = new EventQueue(elevator); + iter(); + iter(); + queue.computeRequest(new CallFromFloorRequest(2, Direction.DOWN)); + iter(); + System.out.println("someone calls from floor 2 to go down"); + iter(); + queue.computeRequest(new CallFromFloorRequest(1, Direction.UP)); + System.out.println("someone calls from floor 1 to go up"); + iter(); + + queue.computeRequest(new CallFromElevatorRequest(5)); + System.out.println("the guy who entered calls for floor 5"); + + iter(); + iter(); + iter(); + iter(); + iter(); + iter(); + iter(); + iter(); + iter(); + iter(); + iter(); + iter(); + + } +} \ No newline at end of file diff --git a/src/commandSystem/TestElevator.java b/src/commandSystem/TestElevator.java new file mode 100644 index 0000000..eab7e26 --- /dev/null +++ b/src/commandSystem/TestElevator.java @@ -0,0 +1,30 @@ +package commandSystem; + +public class TestElevator implements Elevator{ + + public int currentFloor; + public Direction direction; + + public TestElevator() { + this.direction = Direction.UP; + this.currentFloor = 0; + } + + + @Override + public void sendFloorReachedNotification() { + // TODO Auto-generated method stub + } + + @Override + public int getCurrentFloor() { + return currentFloor; + } + + @Override + public Direction getCurrentDirection() { + return direction; + } + + +} diff --git a/src/Direction.java b/src/gui/Direction.java similarity index 79% rename from src/Direction.java rename to src/gui/Direction.java index 7863c87..15adff9 100644 --- a/src/Direction.java +++ b/src/gui/Direction.java @@ -1,3 +1,5 @@ +package gui; + public enum Direction { NONE, UP, diff --git a/src/Elevator.java b/src/gui/Elevator.java similarity index 99% rename from src/Elevator.java rename to src/gui/Elevator.java index 605e39d..8499815 100644 --- a/src/Elevator.java +++ b/src/gui/Elevator.java @@ -1,3 +1,4 @@ +package gui; public class Elevator { private int currentFloor = 0; private boolean betweenFloors = false; /* Between currentFloor and the one above it. */ diff --git a/src/ElevatorApplication.java b/src/gui/ElevatorApplication.java similarity index 99% rename from src/ElevatorApplication.java rename to src/gui/ElevatorApplication.java index f76a72f..f920a02 100644 --- a/src/ElevatorApplication.java +++ b/src/gui/ElevatorApplication.java @@ -1,3 +1,4 @@ +package gui; import javax.swing.*; import java.awt.*; import java.awt.event.*; diff --git a/src/ElevatorCanvas.java b/src/gui/ElevatorCanvas.java similarity index 98% rename from src/ElevatorCanvas.java rename to src/gui/ElevatorCanvas.java index 8ca7d48..d4065cc 100644 --- a/src/ElevatorCanvas.java +++ b/src/gui/ElevatorCanvas.java @@ -1,3 +1,4 @@ +package gui; import javax.swing.*; import java.awt.*; diff --git a/src/ElevatorPanel.java b/src/gui/ElevatorPanel.java similarity index 97% rename from src/ElevatorPanel.java rename to src/gui/ElevatorPanel.java index 7ef5bc4..71cbdf8 100644 --- a/src/ElevatorPanel.java +++ b/src/gui/ElevatorPanel.java @@ -1,3 +1,4 @@ +package gui; import javax.swing.*; import java.awt.*; import java.awt.event.*; diff --git a/src/FloorPanels.java b/src/gui/FloorPanels.java similarity index 97% rename from src/FloorPanels.java rename to src/gui/FloorPanels.java index b225e54..25e58c6 100644 --- a/src/FloorPanels.java +++ b/src/gui/FloorPanels.java @@ -1,3 +1,4 @@ +package gui; import javax.swing.*; import java.awt.*; import java.awt.event.*; diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..f48bd61 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,3 @@ +module elevator { + requires java.desktop; +} \ No newline at end of file