-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParkingChecker.java
More file actions
75 lines (54 loc) · 1.91 KB
/
ParkingChecker.java
File metadata and controls
75 lines (54 loc) · 1.91 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
import java.util.logging.Level;
import java.util.logging.Logger;
public class ParkingChecker {
private final int totalPlaces = Parking.numPlaces;
private final int placesArray = totalPlaces;
/*Creating an Array to be able to access the names of CarThread*/
private final String[] pArray = new String[placesArray];
private final int cPlaces = totalPlaces;
/*to store where a car was stores*/
private int p = 0;
public ParkingChecker() {
}
public synchronized void arrayChecker() {
for (int i = 0; i < pArray.length; i++) {
System.out.print("(*" + pArray[i] + "*)");
}
System.out.println("\n");
}
/* We need to initialize the array first */
public void iniArray() {
for (int i = 0; i < pArray.length; i++) {
pArray[i] = "0";
}
}
public synchronized Integer Entry(String entryName) {
//Checking if there's free slots.
while (Parking.numPlaces == 0) {
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(ParkingChecker.class.getName()).log(Level.SEVERE, null, ex);
}
}
for (int i = 0; i < pArray.length; i++) {
if (pArray[i] == "0") {
pArray[i] = entryName;
arrayChecker();
p = i;
break;
}
//And stores the freeSlot the the Int value.
}
return p;
}
/*We need to handle the now free slot of a leaving car*/
public synchronized void Exit(String name, int pNum) {
System.out.println("Leaving :" + name);
pArray[pNum] = "0";
System.out.println("Free slots now : " + Parking.numPlaces);
arrayChecker();
/*Notify of a new free slot*/
notifyAll();
}
}