Repository navigation
Expand file tree
/
Copy pathElevator.java
More file actions
83 lines (66 loc) · 2.1 KB
/
Copy pathElevator.java
File metadata and controls
83 lines (66 loc) · 2.1 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication4;
import java.util.TreeSet;
/**
*
* @author User
*/
public class Elevator {
private String id;
private int currentFloor = 0;
private Direction currentDirection = Direction.UP;
private State currentState = State.IDLE;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getCurrentFloor() {
return currentFloor;
}
public Direction getCurrentDirection() {
return currentDirection;
}
public State getCurrentState() {
return currentState;
}
void processRequest(RequestElevator request) {
currentState = State.MOVING;
int startFloor = currentFloor;
if (startFloor < request.getDestinationFloor()) {
for (int i = startFloor; i <= request.getDestinationFloor(); i++) {
try {
Thread.sleep(1000); //it's cost 1 sec to go from floor n to floor n+1
} catch (InterruptedException e) {
e.printStackTrace();
}
currentFloor = i;
System.out.println(id + " have reached floor -- " + currentFloor);
}
} else if (startFloor > request.getDestinationFloor()) {
for (int i = startFloor; i >= request.getDestinationFloor(); i--) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentFloor = i;
System.out.println(id+" have reached floor -- " + currentFloor);
}
}
System.out.println(id+" Reached Destination Floor--opening door");
currentState = State.IDLE;
startFloor = currentFloor;
}
}
enum State {
MOVING, STOPPED, IDLE
}
enum Direction {
UP, DOWN
}