Compare commits
No commits in common. "7649656e646e9433cfdb497b4e23bab36e2d7b33" and "e0280d7e181e2fdcbd4438105b02aa527df6476c" have entirely different histories.
7649656e64
...
e0280d7e18
@ -1 +1 @@
|
||||
Main-Class: gui.ElevatorApplication
|
||||
Main-Class: ElevatorApplication
|
||||
|
6
Makefile
6
Makefile
@ -3,10 +3,10 @@ all: build assemble
|
||||
.PHONY: build
|
||||
build:
|
||||
mkdir -p build
|
||||
javac -Xlint $(shell find src -type f -name '*.java') -d build
|
||||
javac -Xlint src/*.java -d build
|
||||
|
||||
assemble:
|
||||
cd build/; jar cvmf ../MANIFEST.MF ../Elevator.jar $(shell cd build/; find -type f -name '*.class')
|
||||
cd build/; jar cvmf ../MANIFEST.MF ../Elevator.jar *.class
|
||||
|
||||
clean:
|
||||
-rm -r build/*
|
||||
-rm build/*.class
|
||||
|
@ -1,7 +1,5 @@
|
||||
package commandSystem;
|
||||
|
||||
public enum Direction {
|
||||
NONE,
|
||||
UP,
|
||||
DOWN,
|
||||
UP, DOWN, NONE
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package commandSystem;
|
||||
|
||||
public interface Elevator {
|
||||
|
||||
|
||||
|
||||
public void sendFloorReachedNotification();
|
||||
public int getCurrentFloor();
|
||||
public Direction getCurrentDirection();
|
||||
|
@ -1,8 +0,0 @@
|
||||
package commandSystem;
|
||||
|
||||
|
||||
public interface ElevatorListener {
|
||||
public void elevatorCall(int floor);
|
||||
public void floorCall(int floor, Direction direction);
|
||||
public void floorChange(Direction direction);
|
||||
}
|
@ -50,7 +50,7 @@ public class EventQueue {
|
||||
return;
|
||||
switch (request.getDirection()) {
|
||||
case UP:
|
||||
if (!upQueue.contains(request.getIncomingCallFloor()))
|
||||
if (!upQueue.contains(request.getIncomingCallFloor()));
|
||||
appSort(upQueue, request.getIncomingCallFloor(), false);
|
||||
break;
|
||||
case DOWN:
|
||||
@ -157,4 +157,4 @@ public class EventQueue {
|
||||
downQueue.removeAll(downQueue);
|
||||
upQueue.removeAll(upQueue);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,60 +4,60 @@ import Requests.CallFromElevatorRequest;
|
||||
import Requests.CallFromFloorRequest;
|
||||
|
||||
public class Test {
|
||||
|
||||
private static boolean running = false;
|
||||
private static EventQueue queue;
|
||||
|
||||
private static TestElevator elevator = new TestElevator();
|
||||
|
||||
private static boolean running = false;
|
||||
private static EventQueue queue;
|
||||
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);
|
||||
}
|
||||
|
||||
private static TestElevator elevator = new TestElevator();
|
||||
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");
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
iter();
|
||||
|
||||
}
|
||||
}
|
@ -4,13 +4,13 @@ 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
|
||||
|
7
src/gui/Direction.java
Normal file
7
src/gui/Direction.java
Normal file
@ -0,0 +1,7 @@
|
||||
package gui;
|
||||
|
||||
public enum Direction {
|
||||
NONE,
|
||||
UP,
|
||||
DOWN,
|
||||
}
|
71
src/gui/Elevator.java
Normal file
71
src/gui/Elevator.java
Normal file
@ -0,0 +1,71 @@
|
||||
package gui;
|
||||
public class Elevator {
|
||||
private int currentFloor = 0;
|
||||
private boolean betweenFloors = false; /* Between currentFloor and the one above it. */
|
||||
private Direction direction = Direction.NONE;
|
||||
private boolean stoppingNextFloor = false;
|
||||
private boolean emergency = false;
|
||||
|
||||
public Elevator(int currentFloor) {
|
||||
this.currentFloor = currentFloor;
|
||||
}
|
||||
|
||||
public void goUp() {
|
||||
if (emergency) return;
|
||||
stoppingNextFloor = false;
|
||||
direction = Direction.UP;
|
||||
}
|
||||
|
||||
public void goDown() {
|
||||
if (emergency) return;
|
||||
stoppingNextFloor = false;
|
||||
direction = Direction.DOWN;
|
||||
}
|
||||
|
||||
public void stopElevator() {
|
||||
direction = Direction.NONE;
|
||||
}
|
||||
|
||||
public void stopNextFloor() {
|
||||
stoppingNextFloor = true;
|
||||
}
|
||||
|
||||
public void emergencyStop() {
|
||||
emergency = true;
|
||||
direction = Direction.NONE;
|
||||
}
|
||||
|
||||
public void cancelEmergency() {
|
||||
emergency = false;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
if (emergency) return;
|
||||
switch (direction) {
|
||||
case DOWN:
|
||||
/* Go halfway below. */
|
||||
if (!betweenFloors) currentFloor--;
|
||||
betweenFloors = !betweenFloors;
|
||||
break;
|
||||
case UP:
|
||||
/* Go halfway above. */
|
||||
if (betweenFloors) currentFloor++;
|
||||
betweenFloors = !betweenFloors;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public int getCurrentFloor() {
|
||||
return currentFloor;
|
||||
}
|
||||
|
||||
public boolean getBetweenFloors() {
|
||||
return betweenFloors;
|
||||
}
|
||||
|
||||
public Direction getDirection() {
|
||||
return direction;
|
||||
}
|
||||
}
|
@ -1,25 +1,11 @@
|
||||
package gui;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
import Requests.CallFromElevatorRequest;
|
||||
import Requests.CallFromFloorRequest;
|
||||
import simulation.Elevator;
|
||||
import simulation.Simulation;
|
||||
|
||||
|
||||
public class ElevatorApplication implements ActionListener {
|
||||
private ElevatorCanvas canvas;
|
||||
private Simulation simulation;
|
||||
private Elevator elevator;
|
||||
|
||||
public ElevatorApplication() {
|
||||
elevator = new Elevator(0, 5);
|
||||
simulation = new Simulation(elevator);
|
||||
elevator.setActionListener(simulation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@ -35,11 +21,11 @@ public class ElevatorApplication implements ActionListener {
|
||||
|
||||
pane.add(Box.createHorizontalGlue());
|
||||
|
||||
canvas = new ElevatorCanvas(elevator);
|
||||
canvas = new ElevatorCanvas();
|
||||
canvas.setAlignmentY(Component.TOP_ALIGNMENT);
|
||||
pane.add(canvas);
|
||||
|
||||
FloorPanels fp = new FloorPanels(5, simulation);
|
||||
FloorPanels fp = new FloorPanels(5);
|
||||
fp.setAlignmentY(Component.TOP_ALIGNMENT);
|
||||
pane.add(fp);
|
||||
|
||||
@ -57,7 +43,6 @@ public class ElevatorApplication implements ActionListener {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ElevatorApplication app = new ElevatorApplication();
|
||||
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
app.createAndShowGUI();
|
||||
|
@ -1,22 +1,15 @@
|
||||
package gui;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import simulation.Elevator;
|
||||
import commandSystem.Direction;
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class ElevatorCanvas extends JPanel {
|
||||
private final static int WIDTH = 40;
|
||||
private final static int HEIGHT = 50;
|
||||
private final static Color COLOR = Color.PINK;
|
||||
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200);
|
||||
|
||||
private int y = 0;
|
||||
private Elevator elevator;
|
||||
|
||||
public ElevatorCanvas(Elevator elevator) {
|
||||
this.elevator = elevator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getMinimumSize() {
|
||||
@ -34,18 +27,7 @@ class ElevatorCanvas extends JPanel {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (elevator.getStoppingNextFloor()) {
|
||||
g.setColor(Color.BLUE);
|
||||
}
|
||||
else if (elevator.getDirection() == Direction.NONE) {
|
||||
g.setColor(Color.GRAY);
|
||||
}
|
||||
else if (elevator.getEmergency()) {
|
||||
g.setColor(Color.RED);
|
||||
}
|
||||
else {
|
||||
g.setColor(Color.BLACK);
|
||||
}
|
||||
g.setColor(COLOR);
|
||||
g.fillRect(0, y, WIDTH, HEIGHT);
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@ import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class ElevatorPanel extends JPanel {
|
||||
private JButton emergencyStop = new JButton("Emergency stop.");
|
||||
private JButton[] buttons;
|
||||
|
@ -1,18 +1,14 @@
|
||||
package gui;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
import commandSystem.ElevatorListener;
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class FloorPanels extends JPanel {
|
||||
private int nbFloors;
|
||||
private JPanel[] floors;
|
||||
|
||||
public FloorPanels(int nbFloors, ElevatorListener l) {
|
||||
public FloorPanels(int nbFloors) {
|
||||
this.nbFloors = nbFloors;
|
||||
floors = new JPanel[nbFloors];
|
||||
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
|
||||
@ -21,14 +17,8 @@ class FloorPanels extends JPanel {
|
||||
floors[i] = new JPanel();
|
||||
floors[i].setLayout(new BoxLayout(floors[i], BoxLayout.LINE_AXIS));
|
||||
floors[i].add(new JLabel("" + (i + 1)));
|
||||
if (i < nbFloors - 1) {
|
||||
JButton upButton = new JButton("↑");
|
||||
floors[i].add(upButton);
|
||||
}
|
||||
if (i > 1) {
|
||||
JButton downButton = new JButton("↓");
|
||||
floors[i].add(downButton);
|
||||
}
|
||||
floors[i].add(new JButton("↑"));
|
||||
floors[i].add(new JButton("↓"));
|
||||
this.add(floors[i]);
|
||||
}
|
||||
}
|
||||
|
3
src/module-info.java
Normal file
3
src/module-info.java
Normal file
@ -0,0 +1,3 @@
|
||||
module elevator {
|
||||
requires java.desktop;
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
package simulation;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
import commandSystem.Direction;
|
||||
import commandSystem.ElevatorListener;
|
||||
|
||||
|
||||
public class Elevator {
|
||||
static final int PRECISION = 10;
|
||||
private double floorHeight;
|
||||
private double step;
|
||||
private double height;
|
||||
// private int currentFloor = 0;
|
||||
// private boolean betweenFloors = false; /* Between currentFloor and the one above it. */
|
||||
private Direction direction = Direction.NONE;
|
||||
private boolean stoppingNextFloor = false;
|
||||
private boolean emergency = false;
|
||||
private ElevatorListener listener;
|
||||
|
||||
public Elevator(int currentFloor, int nbFloors) {
|
||||
height = (double) currentFloor / nbFloors;
|
||||
floorHeight = 1. / nbFloors;
|
||||
step = floorHeight / PRECISION;
|
||||
}
|
||||
|
||||
public void setActionListener(ElevatorListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void goUp() {
|
||||
if (emergency) return;
|
||||
stoppingNextFloor = false;
|
||||
direction = Direction.UP;
|
||||
}
|
||||
|
||||
public void goDown() {
|
||||
if (emergency) return;
|
||||
stoppingNextFloor = false;
|
||||
direction = Direction.DOWN;
|
||||
}
|
||||
|
||||
public void stopElevator() {
|
||||
direction = Direction.NONE;
|
||||
}
|
||||
|
||||
public void stopNextFloor() {
|
||||
stoppingNextFloor = true;
|
||||
}
|
||||
|
||||
public void emergencyStop() {
|
||||
emergency = true;
|
||||
direction = Direction.NONE;
|
||||
}
|
||||
|
||||
public void cancelEmergency() {
|
||||
emergency = false;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if (emergency) return;
|
||||
int prevFloor = (int) Math.floor(height / floorHeight);
|
||||
switch (direction) {
|
||||
case DOWN:
|
||||
height -= step;
|
||||
|
||||
/* Bottom reached. */
|
||||
if (height < step) {
|
||||
height = 0;
|
||||
direction = Direction.NONE;
|
||||
}
|
||||
|
||||
/* Passed a floor, invoke the listener. */
|
||||
if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
|
||||
listener.floorChange(Direction.UP);
|
||||
}
|
||||
// /* Go halfway below. */
|
||||
// if (!betweenFloors) currentFloor--;
|
||||
// betweenFloors = !betweenFloors;
|
||||
break;
|
||||
case UP:
|
||||
height += step;
|
||||
|
||||
/* Top reached. */
|
||||
if (height > 1-step) {
|
||||
height = 1;
|
||||
direction = Direction.NONE;
|
||||
}
|
||||
|
||||
/* Passed a floor, invoke the listener. */
|
||||
if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
|
||||
listener.floorChange(Direction.DOWN);
|
||||
}
|
||||
// /* Go halfway above. */
|
||||
// if (betweenFloors) currentFloor++;
|
||||
// betweenFloors = !betweenFloors;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// public int getCurrentFloor() {
|
||||
// return currentFloor;
|
||||
// }
|
||||
|
||||
// public boolean getBetweenFloors() {
|
||||
// return betweenFloors;
|
||||
// }
|
||||
|
||||
public Direction getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public boolean getStoppingNextFloor() {
|
||||
return stoppingNextFloor;
|
||||
}
|
||||
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public boolean getEmergency() {
|
||||
return emergency;
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package simulation;
|
||||
|
||||
import Requests.CallFromElevatorRequest;
|
||||
import Requests.CallFromFloorRequest;
|
||||
|
||||
import commandSystem.EventQueue;
|
||||
import commandSystem.Direction;
|
||||
import commandSystem.ElevatorListener;
|
||||
import simulation.Elevator;
|
||||
|
||||
|
||||
public class Simulation implements ElevatorListener {
|
||||
private boolean running = false;
|
||||
private int currentFloor = 0;
|
||||
private EventQueue queue;
|
||||
private Elevator elevator;
|
||||
|
||||
public Simulation(Elevator elevator) {
|
||||
this.elevator = elevator;
|
||||
}
|
||||
|
||||
public void elevatorCall(int floor) {
|
||||
}
|
||||
|
||||
public void floorCall(int floor, Direction direction) {
|
||||
}
|
||||
|
||||
public void floorChange(Direction d) {
|
||||
switch (d) {
|
||||
case UP:
|
||||
currentFloor++;
|
||||
break;
|
||||
case DOWN:
|
||||
currentFloor--;
|
||||
break;
|
||||
}
|
||||
// iter();
|
||||
}
|
||||
|
||||
// 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);
|
||||
// }
|
||||
}
|
Reference in New Issue
Block a user