-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElevatorInfo.java
181 lines (142 loc) · 4.65 KB
/
ElevatorInfo.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.sql.Time;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Arrays;
import javafx.util.Callback;
public class ElevatorInfo extends Elevator {
// data members for program logic:
public int currentWeight; // how much we're holding
public Building building;
// used for tracking what time it is at any point
public Time sqlStartingTime;
public long systemStartingTime;
// tracks what buttons are pressed inside the elevator car
public LinkedList<Integer> buttonsPressed;
// tracks released passengers
public ArrayList<PassengerReleased> released;
// tracks current passengers:
public LinkedList<PassengerRequest> occupants = new LinkedList<PassengerRequest>();
public static ElevatorInfo ElevatorInfoInstance;
private boolean freezeMainThread = true;
public int releaseCap;
public int timesMoved = 0;
/*
* //inherited members:
*
* protected int capacity; //total capacity, in pounds protected int timeMoveOneFloor; //time to
* move up or down one floor protected int floors; //floors in building protected int doorDelta;
* //time to open/close doors protected int currentFloor = 1; //the floor where the elevator is
* right now
*
* @SuppressWarnings("deprecation") protected Time currentTime= new Time(8, 0, 0); // Elevator
* starts "working" protected int startingFloor = 1;
*
* protected boolean verbose = false; //what is this.
*
* protected Queue<PassengerRequest> servingQueue; // all requests that will be made
*/
private Callback<ArrayList<PassengerReleased>, Void> callback;
public ElevatorInfo(int capacity, int timeMoveOneFloor, int floors, int doorDelta, boolean verbose) {
super(capacity, timeMoveOneFloor, floors, doorDelta, verbose);
sqlStartingTime = currentTime;
currentFloor = 1; //now we're base - one!
currentWeight = 0;
building = new Building(this.floors);
buttonsPressed = new LinkedList<Integer>();
released = new ArrayList<PassengerReleased>();
systemStartingTime = System.currentTimeMillis();
}
// inherited methods:
ArrayList<PassengerReleased> move() {
return new ArrayList<PassengerReleased>();
}
boolean continueOperate() {
if (this.releaseCap <= released.size()) { // check if all have been served - CJL
Print.print(released); // print results if they have - CJL
return false;
}
return true; // if not all have been served continue to operate - CJL
}
public void printStatus () {
if (verbose) {
System.out.println("\nCurrently Released: " + this.released.size() + " out of " + this.releaseCap);
System.out.println("Currently holding: " + this.occupants.size());
System.out.println("Buttons Pressed " + Arrays.toString(buttonsPressed.toArray()));
System.out.println("Currently on Floor " + this.currentFloor);
System.out.println("Current Time: " + currentTime);
System.out.println();
}
}
@Override
public ArrayList<PassengerReleased> operate() {
systemStartingTime = System.currentTimeMillis();
ElevatorInfoInstance = this;
new Thread() {
@Override
public void run() {
ApplicationStartGUI.launch(ApplicationStartGUI.class);
}
}.start();
while(freezeMainThread){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return released;
}
// other methods:
public boolean atGroundFloor() {
return currentFloor == 1;
}
public boolean atMaxFloor() {
return currentFloor == floors;
}
public int remainingSpace() {
return capacity - currentWeight;
}
// call this before anything that requires the elevator's currentTime member to be accurate
public void updateTime() {
this.currentTime = TimeManip.systemTimeToSqlTime(this.systemStartingTime, this.sqlStartingTime,
System.currentTimeMillis());
}
// internal class:
public class ElevatorFloor {
public ElevatorRequest request;
public ElevatorFloor() {
request = ElevatorRequest.nothing;
}
public boolean hasRequest() {
if (request != ElevatorRequest.nothing) {
return true;
}
return false;
}
public boolean hasUpRequest() {
if (request == ElevatorRequest.up || request == ElevatorRequest.both) {
return true;
}
return false;
}
public boolean hasDownRequest() {
if (request == ElevatorRequest.down || request == ElevatorRequest.both) {
return true;
}
return false;
}
}
public void initialize(Queue<PassengerRequest> theQueue) {
servingQueue = theQueue;
releaseCap = theQueue.size();
}
public static ElevatorInfo getInstance() {
return ElevatorInfoInstance;
}
public void finished() {
freezeMainThread = false;
}
public enum ElevatorRequest {
nothing, up, down, both
}
}