/
Elevator.java
315 lines (274 loc) · 12.2 KB
/
Elevator.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cscie160.hw1;
/**
* Harvard CSCIE 160
* Homework Assignment HW1
* Brian Ketelsen
* <p>
* Requirements:
* <ul>
* <li>Create an Elevator class in the package cscie160.hw1. The class should have a no argument constructor that sets up the elevator's state.</li>
* <li>Define several constant values in the Elevator class. Include the capacity and number of floors in the building.</li>
* <li>Augment the Elevator with data members indicating the current floor and current direction of travel. Also add data members for both its destination requests and the number of passengers destined for each floor. [It's tempting to think that an Elevator can determine if a given floor is among its destinations just by checking to see if it has any passengers destined for the floor. But this will not stand up under the later requirement that an Elevator could be destined for a particular floor just to fetch passengers, while having none on board who are going there.]</li>
* <li>Add a method move() to the elevator with the characteristic that every time it is called it increments or decrements the value of the current floor, according to whether the elevator is moving up or down, changing directions as appropriate.</li>
* <li>Override the toString() method of java.lang.Object (the default base class of every Java class) in your Elevator class which returns a String object summarizing all the pertinent values in the elevator, including the number of passengers on board and the current floor. [The call to System.out.println() will accept any object as an argument and will look for a toString method for a means to create a String it can write to standard out. Thus, if Elevator is equipped with toString, then System.out.println(myElevator) will get a String from myElevator.toString() and send it to standard out.]</li>
* <li>When the Elevator reaches a floor that is among its destinations it should stop, otherwise it should keep moving. Write the stop() method in such a way that it prints a message to standard out announcing the event of its stopping and have it send your Elevator to standard out after all processing for the stop is complete, i.e. after discharging passengers, to show its post-stop-processing state.</li>
* <li>Stopping should entail appropriate changes in state. For instance, if an Elevator had two passengers destined for a particular floor, stopping there should clear the number of passengers destined there and the number of passengers on board should decrease by two and eliminate the floor as a destination of the Elevator. [The class Floor comes in the next homework so you needn't worry about what happens to the passengers after the Elevator stops.]</li>
* <li>Endow your Elevator with a method, boardPassenger(int floor), for boarding a passenger destined for a particular floor. Calling this method should increase the number of passengers destined there by one, and should increase the number of passengers on board by one. [Note: this does not require defining a class Passenger.] For now, allow your elevator to fill beyond its capacity, accepting more passengers than it can hold. In homework 2, we will use an exception to handle this error case and limit the occupancy in the elevator.</li>
* <li>Add self-documenting javadoc comments to your class. Run the javadoc utility on your class and submit the resulting HTML file, Elevator.html, with your work. If you are submitting hard copy, submit a printout from your browser displaying the html documentation for the class. [This requirement will apply to every homework during the course.]</li>
* </ul>
*
*/
public class Elevator {
/**
* <CODE>MAXIMIMUM_CAPACITY</code> declaration
* Assumed to be 10 for this assignment
*/
public static final int MAXIMUM_CAPACITY = 10;
/**
* <CODE>NUMBER_OF_FLOORS</code> declaration
* Assumed to be 7 for this assignment
*/
public static final int NUMBER_OF_FLOORS = 7;
/**
* Embedded class/enumeration <code>DirectionOfTravel</code>
*/
enum DirectionOfTravel {
UP, DOWN, STOPPED
}
/**
* Embedded class/enumeration <code>Floor</code>
* Floor encapsulates all the logic and data surrounding floors.
* Floor contains constant names e.g. <code>FIRST</code> for each floor.
*/
public enum Floor {
FIRST("FIRST", 0, false),
SECOND("SECOND", 0, false),
THIRD("THIRD", 0, false),
FOURTH("FOURTH", 0, false),
FIFTH("FIFTH", 0, false),
SIXTH("SIXTH", 0, false),
SEVENTH("SEVENTH", 0, false);
private final int _number;
private final String _name;
private int _queuedPassengers;
private boolean _hasDestinationRequests;
private Floor _nextUp;
private Floor _nextDown;
/**
* Default enumeration constructor
* @param name The name of the floor being created
* @param queuedPax Number of passengers queued for that floor
* @param hasDestinationRequests Boolean indicator of whether the floor has destination requests pending
*/
Floor(String name, int queuedPax, boolean hasDestinationRequests) {
this._number = this.ordinal() + 1;
this._name = name;
this._queuedPassengers = queuedPax;
this._hasDestinationRequests = hasDestinationRequests;
}
/**
* Default accessor for floor number
* @return The number of the floor instance
*/
public int floorNumber() {
return _number;
}
/**
* Default accessor for floor name
* @return The name of the floor instance
*/
public String floorName() {
return _name;
}
/**
* Default accessor for number of queued passengers
* @return The number of queued passengers for the floor instance
*/
public int queuedPassengers() {
return _queuedPassengers;
}
/**
* Adds a passenger to the list of passengers queued for this floor instance
*/
public void addQueuedPassenger(){
this._queuedPassengers +=1;
}
/**
* Clears the queued passenger list for this floor - to be used when the elevator stops and passengers disembark
*/
public void clearQueuedPassengers(){
this._queuedPassengers = 0;
}
/**
* nextFloorUp
* Convenience method to return the next floor up on the chain
* Added to facilitate moving up and down without complicated if statements
* Artifact of design decision to use embedded enumeration of floors instead of simple floor array
*/
public Floor nextFloorUp() {
Floor next = Floor.FIRST; // default value
switch(_number){
case 1: next = Floor.SECOND;
break;
case 2: next = Floor.THIRD;
break;
case 3: next = Floor.FOURTH;
break;
case 4: next = Floor.FIFTH;
break;
case 5: next = Floor.SIXTH;
break;
case 6: next = Floor.SEVENTH;
break;
}
return next;
}
/**
* nextFloorDown
* Convenience method to return the next floor down on the chain
* Added to facilitate moving up and down without complicated if statements
* Artifact of design decision to use embedded enumeration of floors instead of simple floor array
*/
public Floor nextFloorDown() {
Floor next = Floor.FIRST; // default value
switch(_number){
case 2: next = Floor.FIRST;
break;
case 3: next = Floor.SECOND;
break;
case 4: next = Floor.THIRD;
break;
case 5: next = Floor.FOURTH;
break;
case 6: next = Floor.FIFTH;
break;
case 7: next = Floor.SIXTH;
break;
}
return next;
}
/**
* makeDestinationRequest
* Modifies the <code>hasDestinationRequests</code> flag to be <code>true</code>
*/
public void makeDestinationRequest() {
this._hasDestinationRequests = true;
}
/**
* clearDestinationRequest
* Modifies the <code>hasDestinationRequests</code> flag to be <code>false</code>
*/
public void clearDestinationRequest() {
this._hasDestinationRequests = false;
}
/**
* hasDestinationRequests
* @return <code>true</code> if the floor has pending destination requests
*/
public boolean hasDestinationRequests(){
return this._hasDestinationRequests;
}
}
private DirectionOfTravel _directionOfTravel;
private Floor _currentFloor;
private int _passengersOnboard;
private java.util.HashMap _floorQueue;
/**
* Constructor for Elevator class - no arguments
*/
Elevator() {
_currentFloor = Floor.FIRST;
_passengersOnboard = 0;
_directionOfTravel = DirectionOfTravel.UP;
}
/**
* The move method progresses the elevator up and down. If the elevator is on the first floor
* it sets the direction of travel to UP. If the elevator is on the top floor, it sets the
* direction of travel to DOWN. If there are pending destination requests, the elevator will
* stop.
*/
public void move() {
if (_currentFloor == Floor.FIRST) {
_directionOfTravel = DirectionOfTravel.UP;
}
if (_currentFloor == Floor.SEVENTH) {
_directionOfTravel = DirectionOfTravel.DOWN;
}
if (_directionOfTravel == DirectionOfTravel.UP) {
_currentFloor = _currentFloor.nextFloorUp();
} else if (_directionOfTravel == DirectionOfTravel.DOWN) {
_currentFloor = _currentFloor.nextFloorDown();
}
if(_currentFloor.hasDestinationRequests()){
stop();
}
}
/**
* Stop the elevator, disembarking passengers on board. Clears the flag that marks a floor
* as having queued passengers.
*/
private void stop() {
System.out.println("Stopping on "+ _currentFloor.floorName() +" floor " );
_currentFloor.clearDestinationRequest();
_passengersOnboard = _passengersOnboard - _currentFloor.queuedPassengers();
_currentFloor.clearQueuedPassengers();
System.out.println(this);
}
/**
* boardPassenger
* Adds the passenger to the list of passengers on the elevator.
*
* @param floor The integer representing the floor the passenger is requesting
*/
public void boardPassenger(int floor){
_passengersOnboard +=1;
switch(floor){
case 1: Floor.FIRST.makeDestinationRequest();
Floor.FIRST.addQueuedPassenger();
break;
case 2: Floor.SECOND.makeDestinationRequest();
Floor.SECOND.addQueuedPassenger();
break;
case 3: Floor.THIRD.makeDestinationRequest();
Floor.THIRD.addQueuedPassenger();
break;
case 4: Floor.FOURTH.makeDestinationRequest();
Floor.FOURTH.addQueuedPassenger();
break;
case 5: Floor.FIFTH.makeDestinationRequest();
Floor.FIFTH.addQueuedPassenger();
break;
case 6: Floor.SIXTH.makeDestinationRequest();
Floor.SIXTH.addQueuedPassenger();
break;
case 7: Floor.SEVENTH.makeDestinationRequest();
Floor.SEVENTH.addQueuedPassenger();
break;
}
}
/**
* Override the Object.toString() method for useful and attractive output
*
*/
@Override
public String toString() {
StringBuilder output = new StringBuilder();
output.append("Currently " + _passengersOnboard + " Passengers Onboard\r\n");
output.append("On Floor : " + _currentFloor + "\r\n");
return output.toString();
}
/**
* main method to be executed when called from the command line
*
*/
public static void main(String[] args) {
Elevator theElevator = new Elevator();
theElevator.boardPassenger(2);
theElevator.boardPassenger(2);
theElevator.boardPassenger(3);
System.out.println(theElevator);
for(int i=1;i<21;i++){
theElevator.move();
}
}
}