Compare commits
9 Commits
e0280d7e18
...
7649656e64
Author | SHA1 | Date | |
---|---|---|---|
7649656e64 | |||
afeb28bc1e | |||
26d9057e85 | |||
4159a1fbf8 | |||
78e40e13bd | |||
30e28253b9 | |||
a186cf4149 | |||
7418e84b42 | |||
ed432f890c |
@ -1 +1 @@
|
|||||||
Main-Class: ElevatorApplication
|
Main-Class: gui.ElevatorApplication
|
||||||
|
6
Makefile
6
Makefile
@ -3,10 +3,10 @@ all: build assemble
|
|||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
javac -Xlint src/*.java -d build
|
javac -Xlint $(shell find src -type f -name '*.java') -d build
|
||||||
|
|
||||||
assemble:
|
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:
|
clean:
|
||||||
-rm build/*.class
|
-rm -r build/*
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package commandSystem;
|
package commandSystem;
|
||||||
|
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
UP, DOWN, NONE
|
NONE,
|
||||||
|
UP,
|
||||||
|
DOWN,
|
||||||
}
|
}
|
||||||
|
8
src/commandSystem/ElevatorListener.java
Normal file
8
src/commandSystem/ElevatorListener.java
Normal 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);
|
||||||
|
}
|
@ -50,7 +50,7 @@ public class EventQueue {
|
|||||||
return;
|
return;
|
||||||
switch (request.getDirection()) {
|
switch (request.getDirection()) {
|
||||||
case UP:
|
case UP:
|
||||||
if (!upQueue.contains(request.getIncomingCallFloor()));
|
if (!upQueue.contains(request.getIncomingCallFloor()))
|
||||||
appSort(upQueue, request.getIncomingCallFloor(), false);
|
appSort(upQueue, request.getIncomingCallFloor(), false);
|
||||||
break;
|
break;
|
||||||
case DOWN:
|
case DOWN:
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package gui;
|
|
||||||
|
|
||||||
public enum Direction {
|
|
||||||
NONE,
|
|
||||||
UP,
|
|
||||||
DOWN,
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +1,25 @@
|
|||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
import Requests.CallFromElevatorRequest;
|
||||||
|
import Requests.CallFromFloorRequest;
|
||||||
|
import simulation.Elevator;
|
||||||
|
import simulation.Simulation;
|
||||||
|
|
||||||
|
|
||||||
public class ElevatorApplication implements ActionListener {
|
public class ElevatorApplication implements ActionListener {
|
||||||
private ElevatorCanvas canvas;
|
private ElevatorCanvas canvas;
|
||||||
|
private Simulation simulation;
|
||||||
|
private Elevator elevator;
|
||||||
|
|
||||||
|
public ElevatorApplication() {
|
||||||
|
elevator = new Elevator(0, 5);
|
||||||
|
simulation = new Simulation(elevator);
|
||||||
|
elevator.setActionListener(simulation);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -21,11 +35,11 @@ public class ElevatorApplication implements ActionListener {
|
|||||||
|
|
||||||
pane.add(Box.createHorizontalGlue());
|
pane.add(Box.createHorizontalGlue());
|
||||||
|
|
||||||
canvas = new ElevatorCanvas();
|
canvas = new ElevatorCanvas(elevator);
|
||||||
canvas.setAlignmentY(Component.TOP_ALIGNMENT);
|
canvas.setAlignmentY(Component.TOP_ALIGNMENT);
|
||||||
pane.add(canvas);
|
pane.add(canvas);
|
||||||
|
|
||||||
FloorPanels fp = new FloorPanels(5);
|
FloorPanels fp = new FloorPanels(5, simulation);
|
||||||
fp.setAlignmentY(Component.TOP_ALIGNMENT);
|
fp.setAlignmentY(Component.TOP_ALIGNMENT);
|
||||||
pane.add(fp);
|
pane.add(fp);
|
||||||
|
|
||||||
@ -43,6 +57,7 @@ public class ElevatorApplication implements ActionListener {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ElevatorApplication app = new ElevatorApplication();
|
ElevatorApplication app = new ElevatorApplication();
|
||||||
|
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
app.createAndShowGUI();
|
app.createAndShowGUI();
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
package gui;
|
package gui;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import simulation.Elevator;
|
||||||
|
import commandSystem.Direction;
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("serial")
|
||||||
class ElevatorCanvas extends JPanel {
|
class ElevatorCanvas extends JPanel {
|
||||||
private final static int WIDTH = 40;
|
private final static int WIDTH = 40;
|
||||||
private final static int HEIGHT = 50;
|
private final static int HEIGHT = 50;
|
||||||
private final static Color COLOR = Color.PINK;
|
|
||||||
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200);
|
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200);
|
||||||
|
|
||||||
private int y = 0;
|
private int y = 0;
|
||||||
|
private Elevator elevator;
|
||||||
|
|
||||||
|
public ElevatorCanvas(Elevator elevator) {
|
||||||
|
this.elevator = elevator;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dimension getMinimumSize() {
|
public Dimension getMinimumSize() {
|
||||||
@ -27,7 +34,18 @@ class ElevatorCanvas extends JPanel {
|
|||||||
@Override
|
@Override
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
super.paintComponent(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);
|
g.fillRect(0, y, WIDTH, HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import java.awt.*;
|
|||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("serial")
|
||||||
class ElevatorPanel extends JPanel {
|
class ElevatorPanel extends JPanel {
|
||||||
private JButton emergencyStop = new JButton("Emergency stop.");
|
private JButton emergencyStop = new JButton("Emergency stop.");
|
||||||
private JButton[] buttons;
|
private JButton[] buttons;
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
import commandSystem.ElevatorListener;
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("serial")
|
||||||
class FloorPanels extends JPanel {
|
class FloorPanels extends JPanel {
|
||||||
private int nbFloors;
|
private int nbFloors;
|
||||||
private JPanel[] floors;
|
private JPanel[] floors;
|
||||||
|
|
||||||
public FloorPanels(int nbFloors) {
|
public FloorPanels(int nbFloors, ElevatorListener l) {
|
||||||
this.nbFloors = nbFloors;
|
this.nbFloors = nbFloors;
|
||||||
floors = new JPanel[nbFloors];
|
floors = new JPanel[nbFloors];
|
||||||
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
|
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
|
||||||
@ -17,8 +21,14 @@ class FloorPanels extends JPanel {
|
|||||||
floors[i] = new JPanel();
|
floors[i] = new JPanel();
|
||||||
floors[i].setLayout(new BoxLayout(floors[i], BoxLayout.LINE_AXIS));
|
floors[i].setLayout(new BoxLayout(floors[i], BoxLayout.LINE_AXIS));
|
||||||
floors[i].add(new JLabel("" + (i + 1)));
|
floors[i].add(new JLabel("" + (i + 1)));
|
||||||
floors[i].add(new JButton("↑"));
|
if (i < nbFloors - 1) {
|
||||||
floors[i].add(new JButton("↓"));
|
JButton upButton = new JButton("↑");
|
||||||
|
floors[i].add(upButton);
|
||||||
|
}
|
||||||
|
if (i > 1) {
|
||||||
|
JButton downButton = new JButton("↓");
|
||||||
|
floors[i].add(downButton);
|
||||||
|
}
|
||||||
this.add(floors[i]);
|
this.add(floors[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
module elevator {
|
|
||||||
requires java.desktop;
|
|
||||||
}
|
|
127
src/simulation/Elevator.java
Normal file
127
src/simulation/Elevator.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
63
src/simulation/Simulation.java
Normal file
63
src/simulation/Simulation.java
Normal 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);
|
||||||
|
// }
|
||||||
|
}
|
Reference in New Issue
Block a user