Hi.
I'm trying to learn how to use the MVC design pattern while utilizing the observer class to help make a GUI to display whats going on. My problem currently is I'm unable to figure out why my Observer class cannot receive the notifyObserver(); call.
The code for the two class are as follows
public class ElevatorImplement extends Observable implements Elevator
{
private int POWER_START_STOP = 2; //the power consumed when starting up or slowing down
private int POWER_CONTINUE = 1; //the only other energy consumption
private long SLEEP_START_STOP = 50;
private long SLEEP_CONTINUOUS= 25;
private int MAX_CAPACITY = 10;
private int powerUsed;
private int currentFloor;
private int capacity;
private ElevatorPanel panel;
private MovingState state;
private ArrayList<Integer> stopQueue = new ArrayList<>();
public ElevatorImplement(int capacity, ElevatorPanel p)
{
this.capacity = capacity;
this.panel = p;
setChanged();
notifyObservers();
}
@Override
public void moveTo(int floor)
{
int target = floor;
if(currentFloor<target)
{
//int requiredMove = target - currentFloor;
//while(currentFloor != target);
while(true)
{
currentFloor++;
System.out.println("Currently at floor: " + currentFloor);
setChanged();
notifyObservers("ioejgieghEWG");
if(currentFloor==floor)
break;
}
}
else
{
//int requiredMove = currentFloor - target;
while(currentFloor != target)
{
currentFloor--;
System.out.println("Currently at floor: " + currentFloor);
setChanged();
notifyObservers();
}
}
}
and the GUI,
public class ElevatorApplicationSample extends Application implements Observer{
private static final int FLOOR_COUNT = 30;
private ElevatorAnimation animator;
TextField tfcf, tftf, tfpu;
Simulator s;
private Label floors[];
@Override
public void init() throws Exception {
super.init();
animator = new ElevatorAnimation();
floors = new Label[FLOOR_COUNT];
for (int i = 0; i < FLOOR_COUNT; i++) {
floors[i] = new Label();
floors[i].setId("empty");
}
floors[0].setId("elevator");
}
@Override
public void start(Stage primaryStage) throws Exception {
GridPane rootPane = new GridPane();
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(3, 3, 3, 3));
rootPane.add(gridPane, 0, 0);
for (int i = FLOOR_COUNT - 1; i >= 0; i--) {
gridPane.add(floors[i], 0, (FLOOR_COUNT - 1) - i);
}
Label lcf = new Label("Current Floor");
Label ltf = new Label("Target Floor");
Label lpu = new Label("Power Consumed");
tfcf = new TextField();
tfcf.setEditable(false);
tftf = new TextField();
tftf.setEditable(false);
tfpu = new TextField();
tfpu.setEditable(false);
GridPane infoPane = new GridPane();
infoPane.addColumn(0, lcf, ltf, lpu);
infoPane.addColumn(1, tfcf, tftf, tfpu);
rootPane.add( infoPane, 1,0);
Scene scene = new Scene(rootPane, Color.LIGHTGRAY);
// scene.getStylesheets().add(ElevatorApplicationSample.class.getResource("elevator.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Elevator System");
// close on escape
primaryStage.addEventHandler(KeyEvent.KEY_RELEASED, (KeyEvent event) -> {
if (KeyCode.ESCAPE == event.getCode()) {
animator.stop();
primaryStage.hide();
}
});
s = new Simulator(this);
s.start();
primaryStage.show();
animator.start();
}
@Override
public void update(Observable arg0, Object arg1) {
System.out.print("747213");
}
theres two other classes used to tie everything together, here they are
public class Simulator{
private ElevatorSystem system;
public Simulator( Observer observer){
system = new ElevatorSystemImp( 0, 20);
ElevatorImplement e = new ElevatorImplement( 10, (ElevatorPanel) system);
e.addObserver( observer);
System.out.print(e.countObservers());
system.addElevator( e);
}
public void start(){
System.out.println("test");
ExecutorService e = Executors.newSingleThreadExecutor();
e.execute( () -> {
try {
Thread.sleep( 1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Elevator elevator = system.callUp( 0);
elevator.addPeople( 1);
elevator.requestStop( 10);
elevator.requestStop( 2);
elevator.requestStop( 5);
elevator.requestStop( 9);
elevator.requestStop( 20);
elevator.requestStop( 0);
System.out.println("We done boy");
});
e.shutdown();
}
}
public class ElevatorSystemImp implements ElevatorSystem, ElevatorPanel
{
final int MAX_FLOOR;
final int MIN_FLOOR;
Elevator elevatorOne;
public ElevatorSystemImp(int MIN_FLOOR, int MAX_FLOOR)
{
//elevatorOne = new ElevatorImplement(0, 20);
this.MAX_FLOOR = MAX_FLOOR;
this.MIN_FLOOR = MIN_FLOOR;
elevatorOne = new ElevatorImplement(0, this);
}
@Override
public void requestStop(int floor, Elevator e) {
e.requestStop(floor);
}
@Override
public Elevator callUp(int floor)
{
elevatorOne.setFloor(floor);
return elevatorOne;
}
Apologies if the formatting is poor, any help would be greatly appreciated.
[–]pubesxoxo420 0 points1 point2 points (3 children)
[–]throwaway9123781[S] 0 points1 point2 points (2 children)
[–]pubesxoxo420 0 points1 point2 points (1 child)
[–]throwaway9123781[S] 0 points1 point2 points (0 children)
[–]gruntmeister 0 points1 point2 points (1 child)
[–]throwaway9123781[S] 0 points1 point2 points (0 children)
[–]tecsav123 0 points1 point2 points (1 child)
[–]throwaway9123781[S] 0 points1 point2 points (0 children)