From 0b3dc7381cb8a86a4b370964657d7527ff79cd4f Mon Sep 17 00:00:00 2001 From: MathieuPietri Date: Tue, 15 Oct 2019 12:15:59 +0200 Subject: [PATCH] =?UTF-8?q?maj=20requests=20et=20eventqueue=20(pas=20termi?= =?UTF-8?q?n=C3=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Requests/CallFromElevatorRequest.java | 37 ++++++++++++- src/Requests/CallFromFloorRequest.java | 40 ++++++++++++-- src/Requests/CancelEmergencyStopRequest.java | 35 +++++++++++- src/Requests/EmergencyStopRequest.java | 35 +++++++++++- src/Requests/ReachedFloorSignal.java | 44 +++++++++++++-- src/Requests/Request.java | 56 ++++++++------------ src/commandSystem/Elevator.java | 9 ---- src/commandSystem/EventQueue.java | 35 +++++++----- src/commandSystem/Test.java | 21 ++++---- src/commandSystem/TestElevator.java | 30 ----------- 10 files changed, 232 insertions(+), 110 deletions(-) delete mode 100644 src/commandSystem/Elevator.java delete mode 100644 src/commandSystem/TestElevator.java diff --git a/src/Requests/CallFromElevatorRequest.java b/src/Requests/CallFromElevatorRequest.java index 677c543..828fcdb 100644 --- a/src/Requests/CallFromElevatorRequest.java +++ b/src/Requests/CallFromElevatorRequest.java @@ -1,9 +1,42 @@ package Requests; -public class CallFromElevatorRequest extends Request { +import commandSystem.Direction; + +public class CallFromElevatorRequest implements Request { + + private final int wantedFloor; public CallFromElevatorRequest(int wantedFloor) { - super(RequestType.CALLFROMELEVATOR); this.wantedFloor = wantedFloor; } + + @Override + public Direction getRequestedDirection() { + return Direction.NONE; + } + + @Override + public int getRequestedFloor() { + return wantedFloor; + } + + @Override + public int getIncomingCallFloor() { + return -1; + } + + @Override + public RequestType getType() { + return RequestType.CALLFROMELEVATOR; + } + + @Override + public int getCurrentFloor() { + return -1; + } + + @Override + public Direction getCurrentDirection() { + return Direction.NONE; + } } diff --git a/src/Requests/CallFromFloorRequest.java b/src/Requests/CallFromFloorRequest.java index a6c3a93..92e3cd8 100644 --- a/src/Requests/CallFromFloorRequest.java +++ b/src/Requests/CallFromFloorRequest.java @@ -2,13 +2,45 @@ package Requests; import commandSystem.Direction; -public class CallFromFloorRequest extends Request { +public class CallFromFloorRequest implements Request { - public CallFromFloorRequest(int sourceFloor, Direction direction) { - super(RequestType.CALLFROMFLOOR); - this.sourceFloor = sourceFloor; + private final int incomingCallFloor; + private final Direction direction; + + public CallFromFloorRequest(int incomingCallFloor, Direction direction) { + this.incomingCallFloor = incomingCallFloor; this.direction = direction; } + @Override + public Direction getRequestedDirection() { + return direction; + } + + @Override + public int getRequestedFloor() { + return -1; + } + + @Override + public int getIncomingCallFloor() { + return incomingCallFloor; + } + + @Override + public RequestType getType() { + return RequestType.CALLFROMFLOOR; + } + + @Override + public int getCurrentFloor() { + return -1; + } + + @Override + public Direction getCurrentDirection() { + return Direction.NONE; + } + } diff --git a/src/Requests/CancelEmergencyStopRequest.java b/src/Requests/CancelEmergencyStopRequest.java index 02020a1..553e115 100644 --- a/src/Requests/CancelEmergencyStopRequest.java +++ b/src/Requests/CancelEmergencyStopRequest.java @@ -1,9 +1,40 @@ package Requests; -public class CancelEmergencyStopRequest extends Request{ +import commandSystem.Direction; +public class CancelEmergencyStopRequest implements Request{ + public CancelEmergencyStopRequest() { - super(RequestType.CANCELEMERGENCYSTOP); + } + + @Override + public Direction getRequestedDirection() { + return Direction.NONE; + } + + @Override + public int getRequestedFloor() { + return -1; + } + + @Override + public int getIncomingCallFloor() { + return -1; + } + + @Override + public RequestType getType() { + return RequestType.CANCELEMERGENCYSTOP; + } + + @Override + public int getCurrentFloor() { + return -1; + } + + @Override + public Direction getCurrentDirection() { + return Direction.NONE; } } diff --git a/src/Requests/EmergencyStopRequest.java b/src/Requests/EmergencyStopRequest.java index 40d4fe8..d57f6ec 100644 --- a/src/Requests/EmergencyStopRequest.java +++ b/src/Requests/EmergencyStopRequest.java @@ -1,8 +1,39 @@ package Requests; -public class EmergencyStopRequest extends Request { +import commandSystem.Direction; + +public class EmergencyStopRequest implements Request { public EmergencyStopRequest() { - super(RequestType.EMERGENCYSTOP); + } + + @Override + public Direction getRequestedDirection() { + return Direction.NONE; + } + + @Override + public int getRequestedFloor() { + return -1; + } + + @Override + public int getIncomingCallFloor() { + return -1; + } + + @Override + public RequestType getType() { + return RequestType.EMERGENCYSTOP; + } + + @Override + public int getCurrentFloor() { + return -1; + } + + @Override + public Direction getCurrentDirection() { + return Direction.NONE; } } diff --git a/src/Requests/ReachedFloorSignal.java b/src/Requests/ReachedFloorSignal.java index e6446d1..96db50b 100644 --- a/src/Requests/ReachedFloorSignal.java +++ b/src/Requests/ReachedFloorSignal.java @@ -1,9 +1,45 @@ package Requests; -public class ReachedFloorSignal extends Request{ - public ReachedFloorSignal(int reachedFLoor) { - super(RequestType.REACHEDFLOORSIGNAL); - this.reachedFloor = reachedFLoor; +import commandSystem.Direction; + +public class ReachedFloorSignal implements Request{ + + private final int currentFloor; + private final Direction currentDirection; + + public ReachedFloorSignal(int currentFloor, Direction currentDirection) { + this.currentFloor = currentFloor; + this.currentDirection = currentDirection; + } + + @Override + public Direction getRequestedDirection() { + return Direction.NONE; + } + + @Override + public int getRequestedFloor() { + return -1; + } + + @Override + public int getIncomingCallFloor() { + return -1; + } + + @Override + public RequestType getType() { + return RequestType.REACHEDFLOORSIGNAL; + } + + @Override + public int getCurrentFloor() { + return currentFloor; + } + + @Override + public Direction getCurrentDirection() { + return currentDirection; } diff --git a/src/Requests/Request.java b/src/Requests/Request.java index ca7499c..5f33ab9 100644 --- a/src/Requests/Request.java +++ b/src/Requests/Request.java @@ -2,59 +2,45 @@ package Requests; import commandSystem.Direction; -public abstract class Request { - - protected RequestType type; - protected int wantedFloor; - protected int sourceFloor; - protected Direction direction; - protected int reachedFloor; - - public Request(RequestType type) { - this.type = type; - this.reachedFloor = -1; - this.wantedFloor = -1; - this.sourceFloor = -1; - this.direction = Direction.NONE; - } - +public interface Request { + /** * - * @return the direction of the request, unconcerned extending classes return NONE. + * @return the direction of the request, unconcerned extending classes return + * NONE. */ - public Direction getDirection() { - return direction; - } + public Direction getRequestedDirection(); /** * * @return the wished floor, unconcerned extending classes return -1. */ - public int getWantedFloor() { - return wantedFloor; - } + public int getRequestedFloor(); /** * - * @return the floor it was called from, unconcerned extending classes return -1. + * @return the floor it was called from, unconcerned extending classes return + * -1. */ - public int getIncomingCallFloor() { - return sourceFloor; - } + public int getIncomingCallFloor(); /** * * @return the RequestType of the request. */ - public RequestType getType() { - return type; - } - + public RequestType getType(); + /** * - * @return the floor the elevator just reached, unconcerned extending classes return -1. + * @return the floor the elevator just reached, unconcerned extending classes + * return -1. */ - public int getReachedFloor() { - return reachedFloor; - } + public int getCurrentFloor(); + + /** + * + * @return the direction of the elevator, when a reachedFloorSignal is sent. + * Unconcerned extending classes return NONE + */ + public Direction getCurrentDirection(); } diff --git a/src/commandSystem/Elevator.java b/src/commandSystem/Elevator.java deleted file mode 100644 index 63969f5..0000000 --- a/src/commandSystem/Elevator.java +++ /dev/null @@ -1,9 +0,0 @@ -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 index c26f946..8242982 100644 --- a/src/commandSystem/EventQueue.java +++ b/src/commandSystem/EventQueue.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import Requests.Request; +import simulation.Elevator; // emergencyStop -> clear the queue @@ -11,14 +12,17 @@ public class EventQueue { private ArrayList upQueue = new ArrayList<>(); private ArrayList downQueue = new ArrayList<>(); private boolean emergencyState; - private Elevator elevator; + + private int currentFloor; + private Direction direction; + + public EventQueue() { - public EventQueue(Elevator elevator) { - - this.elevator = elevator; upQueue = new ArrayList(); downQueue = new ArrayList(); emergencyState = false; + currentFloor = 0; + direction = Direction.NONE; } /** @@ -48,7 +52,7 @@ public class EventQueue { case CALLFROMFLOOR: if (emergencyState) return; - switch (request.getDirection()) { + switch (request.getRequestedDirection()) { case UP: if (!upQueue.contains(request.getIncomingCallFloor())) appSort(upQueue, request.getIncomingCallFloor(), false); @@ -64,11 +68,11 @@ public class EventQueue { 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); + if (request.getRequestedFloor() > currentFloor && !upQueue.contains(request.getRequestedFloor())) + appSort(upQueue, request.getRequestedFloor(), false); + else if (request.getRequestedFloor() < currentFloor + && !upQueue.contains(request.getRequestedFloor())) + appSort(downQueue, request.getRequestedFloor(), true); break; case EMERGENCYSTOP: if (emergencyState) @@ -79,6 +83,11 @@ public class EventQueue { case CANCELEMERGENCYSTOP: emergencyState = false; break; + case REACHEDFLOORSIGNAL: + if (emergencyState) + return; + currentFloor = request.getCurrentFloor(); + direction = request.getCurrentDirection(); default: break; } @@ -118,11 +127,11 @@ public class EventQueue { public int getNextInstruction() { //System.out.println("" + upQueue + '\n' + downQueue); int nextFloor = -1; - switch (elevator.getCurrentDirection()) { + switch (direction) { // 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()) { + if (upQueue.get(i) >= currentFloor) { nextFloor = upQueue.get(i); } } @@ -136,7 +145,7 @@ public class EventQueue { // 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()) { + if (downQueue.get(i) <= currentFloor) { nextFloor = downQueue.get(i); } } diff --git a/src/commandSystem/Test.java b/src/commandSystem/Test.java index 52d0d1b..c5e10af 100644 --- a/src/commandSystem/Test.java +++ b/src/commandSystem/Test.java @@ -2,13 +2,15 @@ package commandSystem; import Requests.CallFromElevatorRequest; import Requests.CallFromFloorRequest; +import simulation.Elevator; public class Test { private static boolean running = false; private static EventQueue queue; - private static TestElevator elevator = new TestElevator(); + private static Direction direction; + private static int currentFloor; public static void iter() { int nextDestination = queue.getNextInstruction(); @@ -16,23 +18,24 @@ public class Test { if (!running) { if (nextDestination > 0) { // return -1 if no next destination running = true; - elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN; + direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN; } } else { - if (elevator.currentFloor == nextDestination) { - queue.removeInstruction(elevator.currentFloor, elevator.direction); + if (currentFloor == nextDestination) { + queue.removeInstruction(currentFloor, direction); running = false; } else { - elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN; - elevator.currentFloor += elevator.direction == Direction.UP ? 1 : -1; + direction = currentFloor < nextDestination ? Direction.UP : Direction.DOWN; + currentFloor += direction == Direction.UP ? 1 : -1; } } - System.out.println("elevator floor " + elevator.currentFloor + " direction " + - (elevator.direction == Direction.UP? "up" : "down") + " running: " + running); + System.out.println("elevator floor " + currentFloor + " direction " + + (direction == Direction.UP? "up" : "down") + " running: " + running); } public static void main(String[] args) { - queue = new EventQueue(elevator); + //TODO envoyer les notifs de changements d'étages + queue = new EventQueue(); iter(); iter(); queue.computeRequest(new CallFromFloorRequest(2, Direction.DOWN)); diff --git a/src/commandSystem/TestElevator.java b/src/commandSystem/TestElevator.java deleted file mode 100644 index 2b5052d..0000000 --- a/src/commandSystem/TestElevator.java +++ /dev/null @@ -1,30 +0,0 @@ -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; - } - - -}