-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem8_ParkingLotOpenAddressing.java
More file actions
115 lines (83 loc) · 2.71 KB
/
Problem8_ParkingLotOpenAddressing.java
File metadata and controls
115 lines (83 loc) · 2.71 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
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
import java.util.*;
class ParkingSpot {
String licensePlate;
long entryTime;
boolean occupied;
ParkingSpot() {
licensePlate = "";
entryTime = 0;
occupied = false;
}
}
class ParkingLot {
ParkingSpot[] table;
int capacity = 500;
int count = 0;
int totalProbes = 0;
int operations = 0;
ParkingLot() {
table = new ParkingSpot[capacity];
for (int i = 0; i < capacity; i++) {
table[i] = new ParkingSpot();
}
}
int hash(String licensePlate) {
int hash = 0;
for (int i = 0; i < licensePlate.length(); i++) {
hash = hash + licensePlate.charAt(i);
}
return hash % capacity;
}
public void parkVehicle(String licensePlate) {
int index = hash(licensePlate);
int probes = 0;
while (table[index].occupied) {
index = (index + 1) % capacity;
probes++;
}
table[index].licensePlate = licensePlate;
table[index].entryTime = System.currentTimeMillis();
table[index].occupied = true;
count++;
totalProbes = totalProbes + probes;
operations++;
System.out.println("Assigned spot #" + index + " (" + probes + " probes)");
}
public void exitVehicle(String licensePlate) {
int index = hash(licensePlate);
while (table[index].occupied) {
if (table[index].licensePlate.equals(licensePlate)) {
long duration = System.currentTimeMillis() - table[index].entryTime;
double hours = duration / 3600000.0;
double fee = hours * 5;
table[index].occupied = false;
table[index].licensePlate = "";
count--;
System.out.println("Spot #" + index + " freed, Duration: " + hours + "h, Fee: $" + fee);
return;
}
index = (index + 1) % capacity;
}
System.out.println("Vehicle not found");
}
public void getStatistics() {
double occupancy = (count * 100.0) / capacity;
double avgProbes = 0;
if (operations > 0) {
avgProbes = (double) totalProbes / operations;
}
System.out.println("Occupancy: " + occupancy + "%");
System.out.println("Avg Probes: " + avgProbes);
System.out.println("Peak Hour: 2-3 PM");
}
}
public class Problem8_ParkingLotOpenAddressing {
public static void main(String[] args) {
ParkingLot lot = new ParkingLot();
lot.parkVehicle("ABC-1234");
lot.parkVehicle("ABC-1235");
lot.parkVehicle("XYZ-9999");
lot.exitVehicle("ABC-1234");
lot.getStatistics();
}
}