merge avec le github + j'ai mis les trucs de l'interface dans un package gui
This commit is contained in:
parent
aa0b3a6bb4
commit
e0280d7e18
10
src/Requests/CallFromElevatorRequest.java
Normal file
10
src/Requests/CallFromElevatorRequest.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package Requests;
|
||||||
|
|
||||||
|
public class CallFromElevatorRequest extends Request {
|
||||||
|
|
||||||
|
public CallFromElevatorRequest(int wantedFloor) {
|
||||||
|
super();
|
||||||
|
this.wantedFloor = wantedFloor;
|
||||||
|
this.type = RequestType.CALLFROMELEVATOR;
|
||||||
|
}
|
||||||
|
}
|
15
src/Requests/CallFromFloorRequest.java
Normal file
15
src/Requests/CallFromFloorRequest.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
src/Requests/EmergencyStopRequest.java
Normal file
9
src/Requests/EmergencyStopRequest.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package Requests;
|
||||||
|
|
||||||
|
public class EmergencyStopRequest extends Request {
|
||||||
|
|
||||||
|
public EmergencyStopRequest() {
|
||||||
|
super();
|
||||||
|
this.type = RequestType.EMERGENCYSTOP;
|
||||||
|
}
|
||||||
|
}
|
46
src/Requests/Request.java
Normal file
46
src/Requests/Request.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
5
src/Requests/RequestType.java
Normal file
5
src/Requests/RequestType.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package Requests;
|
||||||
|
|
||||||
|
public enum RequestType {
|
||||||
|
CALLFROMFLOOR, CALLFROMELEVATOR, EMERGENCYSTOP, CANCELEMERGENCYSTOP
|
||||||
|
}
|
5
src/commandSystem/Direction.java
Normal file
5
src/commandSystem/Direction.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package commandSystem;
|
||||||
|
|
||||||
|
public enum Direction {
|
||||||
|
UP, DOWN, NONE
|
||||||
|
}
|
9
src/commandSystem/Elevator.java
Normal file
9
src/commandSystem/Elevator.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package commandSystem;
|
||||||
|
|
||||||
|
public interface Elevator {
|
||||||
|
|
||||||
|
|
||||||
|
public void sendFloorReachedNotification();
|
||||||
|
public int getCurrentFloor();
|
||||||
|
public Direction getCurrentDirection();
|
||||||
|
}
|
160
src/commandSystem/EventQueue.java
Normal file
160
src/commandSystem/EventQueue.java
Normal file
@ -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<Integer> upQueue = new ArrayList<>();
|
||||||
|
private ArrayList<Integer> downQueue = new ArrayList<>();
|
||||||
|
private boolean emergencyState;
|
||||||
|
private Elevator elevator;
|
||||||
|
|
||||||
|
public EventQueue(Elevator elevator) {
|
||||||
|
|
||||||
|
this.elevator = elevator;
|
||||||
|
upQueue = new ArrayList<Integer>();
|
||||||
|
downQueue = new ArrayList<Integer>();
|
||||||
|
emergencyState = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add new floor to <b>queue</b> queue and sort it to keep floors order
|
||||||
|
*
|
||||||
|
* @param queue ArrayList<Int>
|
||||||
|
* @param floor int - the floor we add to queue
|
||||||
|
* @param reversed boolean - reverse queue order
|
||||||
|
*/
|
||||||
|
public void appSort(ArrayList<Integer> 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);
|
||||||
|
}
|
||||||
|
}
|
63
src/commandSystem/Test.java
Normal file
63
src/commandSystem/Test.java
Normal file
@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
30
src/commandSystem/TestElevator.java
Normal file
30
src/commandSystem/TestElevator.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
NONE,
|
NONE,
|
||||||
UP,
|
UP,
|
@ -1,3 +1,4 @@
|
|||||||
|
package gui;
|
||||||
public class Elevator {
|
public class Elevator {
|
||||||
private int currentFloor = 0;
|
private int currentFloor = 0;
|
||||||
private boolean betweenFloors = false; /* Between currentFloor and the one above it. */
|
private boolean betweenFloors = false; /* Between currentFloor and the one above it. */
|
@ -1,3 +1,4 @@
|
|||||||
|
package gui;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
@ -1,3 +1,4 @@
|
|||||||
|
package gui;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
@ -1,3 +1,4 @@
|
|||||||
|
package gui;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
@ -1,3 +1,4 @@
|
|||||||
|
package gui;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
3
src/module-info.java
Normal file
3
src/module-info.java
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module elevator {
|
||||||
|
requires java.desktop;
|
||||||
|
}
|
Reference in New Issue
Block a user