Compare commits

...

9 Commits

17 changed files with 316 additions and 153 deletions

View File

@ -1 +1 @@
Main-Class: ElevatorApplication
Main-Class: gui.ElevatorApplication

View File

@ -3,10 +3,10 @@ all: build assemble
.PHONY: build
build:
mkdir -p build
javac -Xlint src/*.java -d build
javac -Xlint $(shell find src -type f -name '*.java') -d build
assemble:
cd build/; jar cvmf ../MANIFEST.MF ../Elevator.jar *.class
cd build/; jar cvmf ../MANIFEST.MF ../Elevator.jar $(shell cd build/; find -type f -name '*.class')
clean:
-rm build/*.class
-rm -r build/*

View File

@ -1,5 +1,7 @@
package commandSystem;
public enum Direction {
UP, DOWN, NONE
NONE,
UP,
DOWN,
}

View File

@ -2,7 +2,7 @@ package commandSystem;
public interface Elevator {
public void sendFloorReachedNotification();
public int getCurrentFloor();
public Direction getCurrentDirection();

View File

@ -0,0 +1,8 @@
package commandSystem;
public interface ElevatorListener {
public void elevatorCall(int floor);
public void floorCall(int floor, Direction direction);
public void floorChange(Direction direction);
}

View File

@ -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);
}
}
}

View File

@ -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();
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 boolean running = false;
private static EventQueue queue;
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");
private static TestElevator elevator = new TestElevator();
iter();
iter();
iter();
iter();
iter();
iter();
iter();
iter();
iter();
iter();
iter();
iter();
}
}
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();
}
}

View File

@ -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

View File

@ -1,7 +0,0 @@
package gui;
public enum Direction {
NONE,
UP,
DOWN,
}

View File

@ -1,71 +0,0 @@
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;
}
}

View File

@ -1,11 +1,25 @@
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) {
@ -21,11 +35,11 @@ public class ElevatorApplication implements ActionListener {
pane.add(Box.createHorizontalGlue());
canvas = new ElevatorCanvas();
canvas = new ElevatorCanvas(elevator);
canvas.setAlignmentY(Component.TOP_ALIGNMENT);
pane.add(canvas);
FloorPanels fp = new FloorPanels(5);
FloorPanels fp = new FloorPanels(5, simulation);
fp.setAlignmentY(Component.TOP_ALIGNMENT);
pane.add(fp);
@ -43,6 +57,7 @@ public class ElevatorApplication implements ActionListener {
public static void main(String[] args) {
ElevatorApplication app = new ElevatorApplication();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
app.createAndShowGUI();

View File

@ -1,15 +1,22 @@
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() {
@ -27,7 +34,18 @@ class ElevatorCanvas extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(COLOR);
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.fillRect(0, y, WIDTH, HEIGHT);
}

View File

@ -4,6 +4,7 @@ import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
class ElevatorPanel extends JPanel {
private JButton emergencyStop = new JButton("Emergency stop.");
private JButton[] buttons;

View File

@ -1,14 +1,18 @@
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) {
public FloorPanels(int nbFloors, ElevatorListener l) {
this.nbFloors = nbFloors;
floors = new JPanel[nbFloors];
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
@ -17,8 +21,14 @@ 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)));
floors[i].add(new JButton(""));
floors[i].add(new JButton(""));
if (i < nbFloors - 1) {
JButton upButton = new JButton("");
floors[i].add(upButton);
}
if (i > 1) {
JButton downButton = new JButton("");
floors[i].add(downButton);
}
this.add(floors[i]);
}
}

View File

@ -1,3 +0,0 @@
module elevator {
requires java.desktop;
}

View File

@ -0,0 +1,127 @@
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;
}
}

View File

@ -0,0 +1,63 @@
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);
// }
}