-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathElevatorMovementThread.java
More file actions
59 lines (47 loc) · 1.31 KB
/
ElevatorMovementThread.java
File metadata and controls
59 lines (47 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class ElevatorMovementThread extends Thread {
// Reference to elevator.
private Elevator _elevator;
/**
* Constructor.
* @param elevator
*/
public ElevatorMovementThread(Elevator elevator){
// Initialize.
_elevator = elevator;
}
/**
* Run.
*/
public void run(){
// Endless loop.
while(true){
// Variables.
Floor floorAt = _elevator.getFloorAt();
Floor floorTo = _elevator.getFloorDestination();
// Door.
ElevatorDoor elevatorDoor = _elevator.getElevatorDoor();
int elevatorDoorStatus = elevatorDoor.getStatus();
// Make sure we have destination.
if(floorTo != null){
// If the door is open, close it before we begin moving.
if(elevatorDoorStatus == ElevatorDoor.OPEN){
elevatorDoor.close();
// Only move if closed.
} else if(elevatorDoorStatus == ElevatorDoor.CLOSED){
// Compare floors.
int floorCompare = floorAt.compareTo(floorTo);
int y = _elevator.getLocation().y + (_elevator.getElevatorDef().moveSpeed * floorCompare);
// Move the elevator.
_elevator.moveTo(y);
}
}
// Attempt to serve the next request.
_elevator.serveNextRequest();
try {
sleep(_elevator.getElevatorDef().moveAnimationMilliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}