-
Notifications
You must be signed in to change notification settings - Fork 1
/
Garage.java
365 lines (238 loc) · 8.63 KB
/
Garage.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.LinkedList;
/**
* The class represents a garage with a capacity of seven cars and a waiting line with a capacity of five cars.
* Incoming cars are parked in the garage; the last one to arrive is kept near the entrance and the first car
* is kept at the last; Waiting cars enter the garage in a first-come, first-serve basis. The class also reads
* from a file with arrival and departure information followed by the liscence number.
*/
public class Garage {
// Stack representing a garage
private StackInt<String> s;
// Queue representing a waiting line
private Queue<String> q;
/**
* Constructor to initialize a garage and a waiting line
*/
public Garage() {
s = new LinkedStack<>();
q = new LinkedList<> ();
}
/**
* A constructor to initialize a garage and process information from a file; it adds
* the liscence number from the file to a stack representing a garage and a
* queue of waiting cars, removes them after departure, and does execption handling.
* @param fileName The file containing arrival and departure information followed by
* the license Number.
*/
public Garage(String fileName) {
s= new LinkedStack<>();
q = new LinkedList<> ();
String status="";
String liscence="";
// Exceptional handling if file is not found
try {
// Read from the file
Scanner input = new Scanner(new File(fileName));
// Find index of space separating thr first letter and license No.
while(input.hasNextLine()) {
String text = input.nextLine();
int space = text.indexOf(" ") + 1;
// Get the first letter
for (int i = 0; i < space - 1; i++) {
status += text.charAt(i);
}
// Get the license number
while (space < text.length()) {
liscence += text.charAt(space);
space++;
}
// Call arrival method if first letter is 'a'
if (status.equals("a")) {
arrival(liscence);
}
// Call departure method if first letter is 'd'
else {
departure(liscence);
}
// Clear the variables
status="";
liscence="";
}
}
// Catch exception if file is not found
catch(FileNotFoundException ex) {
s = new LinkedStack<>();
}
}
/**
* Adds up to 7 cars in a garage; other incoming cars enter a waiting room with a
* capacity of 5 cars in a first-come-first-serve basis and only allows cars with
* unique license number to enter.
* @param license The license number of the incoming cars.
* @return true if the car was added successfully;
* false if car was not added
*/
public boolean arrival(String license) {
boolean arrived = false;
// Proceed only if license number is unique
if (!checkDub(license)) {
// Add liscence no. to the garage if there is space
if(numberParked() < 7) {
s.push(license);
arrived = true;
}
// Add other cars to a waiting line if it has less than 5 cars
else if (numberWaiting() < 5) {
q.add(license);
arrived = true;
}
}
return arrived;
}
/**
* Moves cars blocking the path out, removes the specified car, move other cars back
* in, and allows cars from the waiting line to enter if space is available.
* @param license The license of the car to be removed.
* @return The number of cars that had to be moved;
* -1 if liscense number was not found in garage.
*/
public int departure(String license) {
StackInt<String> temp = new LinkedStack<>();
boolean found = false;
int count=0;
// Look through the garage to find the specified liscence number
while(!s.empty() && !found) {
String plateNo = s.peek();
if (plateNo.equals(license)) {
s.pop();
found = true;
}
else {
temp.push(s.pop());
count++;
}
}
// Retrieve the stack back to its original order
while(!temp.empty()) {
s.push(temp.pop());
}
// Move cars from waiting line to garage if space is available
while(numberParked() < 7 && !q.isEmpty()) {
s.push(q.remove());
}
// Return the number of cars that had to be moved
if (found) {
return count;
}
// Return -1 if no car was found
else {
return -1;
}
}
/**
* Checks for dublication of license number in garage and waiting line.
* @param license The license plate number of the vehicle to be checked.
* @return true if license number is identical;
* false if license number is not identical.
*/
private boolean checkDub(String license) {
boolean identical = false;
// Temporary stack
StackInt<String> temp = new LinkedStack<>();
// Temporary Queue
Queue<String> tempQ = new LinkedList<>();
while(!s.empty()) {
String plateNo = s.peek();
temp.push(s.pop());
if (plateNo.equals(license)) {
identical = true;
}
}
// Retrieve stack back to its original order
while(!temp.empty()) {
s.push(temp.pop());
}
while(!q.isEmpty()) {
String plateNo = q.peek();
tempQ.add(q.remove());
if (plateNo.equals(license)) {
identical = true;
}
}
// Retrieve queue back to its original order
while(!tempQ.isEmpty()) {
q.add(tempQ.remove());
}
return identical;
}
/**
* Gets the number of cars parked in the garage.
* @return the number of cars parked.
*/
public int numberParked() {
// Temorary stack
StackInt<String> temp = new LinkedStack<>();
int parked = 0;
while (!s.empty()) {
temp.push(s.pop());
parked++;
}
while(!temp.empty()) {
s.push(temp.pop());
}
return parked;
}
/**
* Gets the number of cars waiting in line.
* @return the number of cars waiting.
*/
public int numberWaiting() {
// Temporary stack
Queue<String> temp = new LinkedList<>();
int count=0;
while(!q.isEmpty()) {
temp.add(q.remove());
count++;
}
while(!temp.isEmpty()) {
q.add(temp.remove());
}
return count;
}
/**
* Returns a formatted string containing all cars in the garage and the waiting line.
* @return A String Value of cars in the garage and waiting line.
*/
@Override
public String toString() {
// Temporary stack
StackInt<String> temp = new LinkedStack<>();
// Temporary queue
Queue<String> temp2 = new LinkedList<>();
String garage="";
String waiting="";
// Get all cars in the garage
while (!s.empty()) {
String t = temp.push(s.pop());
garage += t + "\t";
}
// Retrieve the stack to its original form
while (!temp.empty()) {
s.push(temp.pop());
}
// Get all cars waiting in the line
while(!q.isEmpty()) {
temp2.add(q.peek());
waiting += q.remove() + "\t";
}
// Retrieve the queue back to its original form
while(!temp2.isEmpty()) {
q.add(temp2.remove());
}
return String.format("\nCars Parked In The Garage: %-1s %-28s %-1s", garage,
"\n\nCars Waiting In The Line: ", waiting);
}
}