-
Notifications
You must be signed in to change notification settings - Fork 0
/
Institute.java
405 lines (379 loc) · 12.6 KB
/
Institute.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import java.util.*;
/**
* An institute represents a site which consists of number of rooms and
* and doors
* There are regular doors and emergency doors which allows evacuation in the case
* of emergency
* @author Moustafa Mahmoud
* @version 8/1/2012
**/
public class Institute implements CALM
{
// declare your ArrayLists here
public ArrayList<Pass> PASS = new ArrayList<Pass>();
public ArrayList<Room> ROOM = new ArrayList<Room>();
public ArrayList<Door> DOOR = new ArrayList<Door>();
public ArrayList<Door> EDOOR = new ArrayList<Door>();
/**Initializes new instance/object of class Institute
* the constructor when deployed creates and loads a set of rooms and door and emergency door to the site
* and addes them to lists for easy access to rooms and doors and emer
* the constructor takes no parameters
*/
public Institute()
{
loadRooms();
setUpDoors();
loadPasses();
setUpEmergencyDoors();
// Now,add all passes to the Outside - Room 100
for (Pass pass : PASS)
{
ROOM.get(0).enter(pass);
}
}
/**
* Returns all of the details of all rooms including the passes
* currently in each room, or "No passes"
* @return all of the details of all rooms including location
* of all passes currently in each room, or "No passes"
*/
public String toString()
{
String s = "";
for (Room room: ROOM)
{
if (room.is_the_room_free() == false)
{
s = s + room.toString() + "\n\n" + room.who_is_in_room() + "\n*************\n" ;
}
else
{
s = s + room.toString() + "\n\n" + "No passes" + "\n*************\n" ;
}
}
return s;
}
/**
* Returns all of the details of all rooms including the passes
* currently in each room, or "No passes"
* @return all of the details of all rooms including location
* of all passes currently in each room, or "No passes"
*/
public String roomsInInstitue()
{
String s = "";
for (Room room: ROOM)
{
s = s + room.toString() + "\n*************\n";
}
return s;
}
/**Returns a String representation of all the passes in all rooms
* @return a String representation of all passes in all rooms, room code
* and name should be included at the start of the list
**/
public String getAllPassesInAllRooms()
{
for (Room room: ROOM)
{
return room.who_is_in_room();
}
return "Can't do it";
}
/**Returns the name of the room which contains the specified pass or null
* @param ps - the specified pass
* @return the number of the Room which contains the pass, or -1
**/
public int findPass(int ps)
{
for (Room room : ROOM)
{
if (room.is_this_pass_in_room(getPass(ps)))
{
return room.get_room_number();
}
}
return -1;
}
/** Given the name of a room, returns the room code
* or null if room does not exist
* @param name of room
* @return number of room, or -1 if the room does not exist
*/
public int getRoomNo(String rm)
{
for (Room room : ROOM)
{
if (room.room_name == rm)
{
return room.room_number;
}
}
return -1;
}
/**Returns a String representation of all the passes in specified room
* @roomCode the room code
* @return a String representation of all passes in specified room, or
* "No such room"
**/
public String getAllPassesInRoom(int rmNo)
{
for (Room room : ROOM)
{
if (room.room_number == rmNo)
{
return room.who_is_in_room();
}
}
return "Can't do it";
}
/** Returns a String with all of the details of a Pass
* @param passNo is the pass number
* @return all details of the Pass, or "No such Pass"
*/
public String showPass(int pNo)
{
for (Pass pass : PASS)
{
if (pass.id == pNo)
{
return pass.toString();
}
}
return "No such Pass";
}
/** Returns a String with all of the details of a Door
* @param doorNo is the door number
* @return all details of the door, or "No such Door"
*/
public String showDoor(int dNo)
{
for (Door door : DOOR)
{
if (door.door_number == dNo)
{
return door.toString();
}
}
return "No such Door";
}
/**Returns true if a Pass is allowed to move using the door, false otherwise
* A move can be made if:
* the protection rating of the pass >= the danger rating of the destination room
* AND the destination room is not full
* AND the pass has hours left >= limit of the destination room
* AND the pass is currently in the source room
* AND the pass id is for a pass in the system
* AND the door number is the number for a door in the system
* @param psId is the id of the pass requesting the move
* @param doorNo is the number of the door through which the pass wants to move
* @return true if the pass is allowed in the move, false otherwise
**/
public boolean canMove(int psId, int doorNo)
{ //other checks optional
Door door = getDoor(doorNo);
Pass pass = getPass(psId);
if (door.is_pass_allowed(pass)==true)
{
return true;
}
else
{
return false;
}
}
/**Returns the result of a pass requesting to move by Door.
* A move will be successful if:
* the protection rating of the pass >= the danger rating of the destination room
* AND the destination room is not full
* AND the pass has sufficient hours left >= limit of the destination room
* AND the pass is currently in the source room
* AND the pass id is for a pass in the system
* AND the door number is the number for a door in the system
* If the move can be made, the pass information is removed from the source
* room, added to the destination room and a suitable message returned.
* If move cannot be made, the state of the system remains unchanged
* and a message specifying the reason is returned.
* @param psId is the id of the pass requesting the move
* @param doorNo is the number of the door through which the pass wants to move
* @return a String giving the result of the request
**/
public String move(int psId, int doorNo)
{ //other checks optional
Door door = getDoor(doorNo);
Pass pass = getPass(psId);
if (door.is_pass_allowed(pass)==true)
{
door.move(pass);
return "Access Accepted";
}
return "Can't do this";
}
// These methods are for Task 6 only and not required for the Demonstration
// If you choose to implement them, uncomment the following number
/** Resets all passes
*/
public void resetPasses()
{
for (Pass pass : PASS)
{
pass.reset_pass();
}
}
/** Evacuates all passes from all rooms of the site into the outside in the case of emergency
* if the room has no one in it or the room is the gathering point in the case of emergency, the
* room is ignored, however, is all other cases, every pass within any other room will be transfered through
* the proper emergency door into the outside, which is the gathering point in the case
* of emergency
*/
public void evacuation()
{
for (Room room : ROOM)
{
if (room.passes_in_room != 0 | room.room_number != 100)
{
ArrayList<Pass> copy = new ArrayList<Pass>();
copy.addAll(room.list_passes_in_room);
for (Pass pass : copy)
{
for (Door edoor : EDOOR)
{
if (edoor.door_number == room.room_number)
{
edoor.move(pass);
}
}
}
}
else
{
continue;
}
}
}
//***************private methods**************
private void loadRooms()
{
//declare & create all Appendix A rooms
Room Outside = new Room(100,"Outside" , 0, 0, 1000);
Room Staff_room = new Room(101,"Staff Room" , 1, 1, 100);
Room Lab1 = new Room(102,"Lab1" , 3, 3, 10);
Room Virus_lab = new Room(103,"Virus Lab" , 5, 4, 2);
Room Decontamination = new Room(104,"Decontamination " , 0, 0, 1);
//add to collection of rooms
ROOM.add(Outside);
ROOM.add(Staff_room);
ROOM.add(Lab1);
ROOM.add(Virus_lab);
ROOM.add(Decontamination);
}
private void setUpDoors()
{
//declare & create all Appendix A doors
Door Door0 = new Door(0, ROOM.get(0), ROOM.get(1));
Door Door1 = new Door(1, ROOM.get(1), ROOM.get(0));
Door Door2 = new Door(2, ROOM.get(1), ROOM.get(2));
Door Door3 = new Door(3, ROOM.get(2), ROOM.get(1));
Door Door4 = new Door(4, ROOM.get(3), ROOM.get(1));
Door Door5 = new Door(5, ROOM.get(1), ROOM.get(4));
Door Door6 = new Door(6, ROOM.get(4), ROOM.get(1));
Door Door7 = new Door(7, ROOM.get(2), ROOM.get(3));
Door Door8 = new Door(8, ROOM.get(1), ROOM.get(3));
//add to collection of rooms
DOOR.add(Door0);
DOOR.add(Door1);
DOOR.add(Door2);
DOOR.add(Door3);
DOOR.add(Door4);
DOOR.add(Door5);
DOOR.add(Door6);
DOOR.add(Door7);
DOOR.add(Door8);
}
private void loadPasses()
{
//declare & create all Appendix A passes
Pass Ali = new Pass(1000, 5, 10);
Pass Bert = new Pass(1001, 3, 20);
Pass Ceri = new Pass(1002, 10, 20);
Pass Dana = new Pass(1003, 2, 12);
Pass Eli = new Pass(1004, 3, 3);
Pass Fred = new Pass(1005, 1, 5);
Pass Gani = new Pass(1006, 10, 6);
Pass Hui = new Pass(1007, 7, 20);
Pass Imran = new Pass(1008, 6, 24);
Assistant Sam = new Assistant(1009,7,20,"Dr.Hana");
Researcher Jas = new Researcher(1010,7,15,333,"Queens");
TeamLeader Sara = new TeamLeader(1011,5,10,35,300);
//add to collection of passes
PASS.add(Ali);
PASS.add(Bert);
PASS.add(Ceri);
PASS.add(Dana);
PASS.add(Eli);
PASS.add(Fred);
PASS.add(Gani);
PASS.add(Hui);
PASS.add(Imran);
PASS.add(Sam);
PASS.add(Jas);
PASS.add(Sara);
}
private void setUpEmergencyDoors()
{
//declare & create all Appendix A passes
Door emergency_door1 = new Door(101, ROOM.get(1), ROOM.get(0));
Door emergency_door2 = new Door(102, ROOM.get(2), ROOM.get(0));
Door emergency_door3 = new Door(103, ROOM.get(3), ROOM.get(0));
Door emergency_door4 = new Door(104, ROOM.get(4), ROOM.get(0));
//add to collection of passes
EDOOR.add(emergency_door1);
EDOOR.add(emergency_door2);
EDOOR.add(emergency_door3);
EDOOR.add(emergency_door4);
}
// some useful private methods for you to write the code
/** Returns the pass with the pass id specified by the parameter
* @return the pass with the specified id number
**/
public Pass getPass(int id)
{
for (Pass pass : PASS)
{
if (pass.id == id)
{
return pass;
}
}
return null;
}
/** Returns the room with the name specified by the parameter
* @param roomNo is the number of the required room
* @return the room with the specified name
**/
private Room getRoom(int roomNo)
{
for (Room room : ROOM)
{
if (room.room_number == roomNo)
{
return room;
}
}
return null;
}
/** Returns the room with the name specified by the parameter
* @return the room with the specified name
**/
private Door getDoor(int dr)
{
for (Door door : DOOR)
{
if (door.door_number == dr)
{
return door;
}
}
return null;
}
}