-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParkingSystemSimulation.java
More file actions
72 lines (63 loc) · 3.25 KB
/
Copy pathParkingSystemSimulation.java
File metadata and controls
72 lines (63 loc) · 3.25 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
import java.io.*;
import java.util.*;
public class ParkingSystemSimulation {
public static void main(String[] args) {
String fileName = "car_data.txt";
createFileWithData(fileName); // Ensure file exists and has data
// Create a parking lot with 4 spots
ParkingLot parkingLot = new ParkingLot(4);
// Create gate threads
List<Gate> gates = new ArrayList<>();
gates.add(new Gate(parkingLot, "Gate 1", fileName));
gates.add(new Gate(parkingLot, "Gate 2", fileName));
gates.add(new Gate(parkingLot, "Gate 3", fileName));
// Start all gate threads
for (Gate gate : gates) { //Starts all gate threads, initiating their run() methods, which handle cars arriving at their respective gates.
gate.start();
}
// ensures the main thread waits for each gate thread to finish before proceeding.
for (Gate gate : gates) {
try {
gate.join(); //Ensures all gates have finished serving their cars before displaying the final results.
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// Print final simulation results
int numOfCars = 0; // total number of cars served by summing the cars handled by all gates.
for(Gate gate : gates) {
numOfCars += gate.getNumOfCars();
}
System.out.println("Simulation complete. Total cars served: " + (numOfCars));
System.out.println("Simulation complete. Currently Parked cars: " + parkingLot.getCurrentlyParked());
System.out.println("Details:");
for (Gate gate : gates) {
System.out.println("- " + gate.getGateName() + " served " + gate.getCarsServedCount() + " cars.");
}
}
// Method to create the file and write data into it if it doesn't exist
private static void createFileWithData(String fileName) {
File file = new File(fileName);
if (!file.exists()) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write("Gate 1, Car 0, Arrive 0, Parks 3\n");
writer.write("Gate 1, Car 1, Arrive 1, Parks 4\n");
writer.write("Gate 1, Car 2, Arrive 2, Parks 2\n");
writer.write("Gate 1, Car 3, Arrive 3, Parks 5\n");
writer.write("Gate 1, Car 4, Arrive 4, Parks 3\n");
writer.write("Gate 2, Car 5, Arrive 3, Parks 4\n");
writer.write("Gate 2, Car 6, Arrive 6, Parks 3\n");
writer.write("Gate 2, Car 7, Arrive 7, Parks 2\n");
writer.write("Gate 2, Car 8, Arrive 8, Parks 5\n");
writer.write("Gate 2, Car 9, Arrive 9, Parks 3\n");
writer.write("Gate 3, Car 10, Arrive 2, Parks 4\n");
writer.write("Gate 3, Car 11, Arrive 5, Parks 3\n");
writer.write("Gate 3, Car 12, Arrive 7, Parks 2\n");
writer.write("Gate 3, Car 13, Arrive 10, Parks 5\n");
writer.write("Gate 3, Car 14, Arrive 11, Parks 3\n");
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
}
}