I wanted to make an elevator system where you send information from the following: elevator (data) -> scheduler (buffer) -> floor (receive) The Floor subsystem and the Elevators are the clients in the system; the Scheduler is the server. when I pressed run some of the issues were:
class elevator is shown below:
package elevator;
import java.util.HashSet;
import java.util.Set;
public class elevator {
public enum State {
MOVING_UP, MOVING_DOWN, STOPPED
}
private int floor;
private State state;
@SuppressWarnings({ })
private Set<Integer> pressedButtons = (Set<Integer>) new HashSet<Integer>();
public elevator() {
state = State.STOPPED;
}
public int getFloor() {
return floor;
}
public void setFloor(int floor) {
this.floor = floor;
pressedButtons.remove(floor);
}
public State getState() {
return state;
}
public void setState(State s) {
state = s;
}
public boolean isMoving() {
return state == State.MOVING_UP || state == State.MOVING_DOWN;
}
public void buttonPressed(int i) {
pressedButtons.add(i);
}
public Set<Integer> getButtons() {
return pressedButtons;
}
public String toString() {
return "Floor: " + floor + "\n" + "\t State: " + state + "\n";
}
}
Exception in thread "Thread-0" java.lang.ClassCastException: class elevator.HashSet cannot be cast to class java.util.Set (elevator.HashSet is in unnamed module of loader 'app'; java.util.Set is in module java.base of loader 'bootstrap') at elevator.elevator.(elevator.java:21) at elevator.elevatorExchange.retrieveData(elevatorExchange.java:30) at elevator.elevatorExchange.run(elevatorExchange.java:19) at java.base/java.lang.Thread.run(Thread.java:835)
Thread 1: elevator package elevator;
import java.util.concurrent.BlockingQueue;
public class elevatorExchange implements Runnable{
private BlockingQueue<elevator> messages;
public elevatorExchange(BlockingQueue<elevator> messages) {
this.messages = messages;
}
@Override
public void run() {
try {
elevator elevatorData = retrieveData();
messages.put(elevatorData);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private elevator retrieveData() throws InterruptedException {
Thread.sleep(5000);
elevator elevatorData = new elevator();
return elevatorData;
}
}
Thread 2: Scheduler. Scheduler is only being used as a communication channel from the Floor thread to the Elevator thread
package scheduler;
import java.util.concurrent.BlockingQueue;
import elevator.elevator;
public class Scheduler implements Runnable {
private BlockingQueue<elevator> messages;
public Scheduler(BlockingQueue<elevator> messages) {
this.messages = messages;
}
@Override
public void run() {
elevator elevatorData = messages.take();
}
Thread 3: The floor (this will receive it from the scheduler). This is the part I'm struggling with the most, I am trying to make sure the data passed down to the floor is from the scheduler and not the elevator, but my IDE keeps making changes to the data type that's running a lot of exceptions.
package floor;
import java.util.concurrent.BlockingQueue;
import elevator.elevator;
public class FloorReceiver implements Runnable{
private BlockingQueue<elevator> messages;
public FloorReceiver(BlockingQueue<elevator> messages) {
this.messages = messages;
}
@Override
public void run() {
try {
System.out.println("waiting for data from elevator");
elevator elevatorData = messages.take();
System.out.println("data from elevator" + elevatorData);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Testing:
package floor;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import elevator.elevator;
import elevator.elevatorExchange;
public class elevatorToFloorTest {
public static void main(String[] args) {
BlockingQueue<elevator> messages = new ArrayBlockingQueue<elevator>(1);
elevatorExchange retriever = new elevatorExchange(messages);
FloorReceiver receiver = new FloorReceiver(messages);
new Thread(retriever).start();
new Thread(receiver).start();
}
}
elevator.HashSet. Is there some reason you're using this instead of Java's HashSet? Also, you don't show the classelevator, which seems to be where the trouble lies.