Repository navigation
Expand file tree
/
Copy pathSimulation.java
More file actions
122 lines (103 loc) · 6.41 KB
/
Copy pathSimulation.java
File metadata and controls
122 lines (103 loc) · 6.41 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
116
117
118
119
120
121
122
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package ferrysimulation;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* Simulation wires every component together and collects final statistics.
*
* Components created here:
* • 4 TollBooths (2 per side)
* • 2 WaitingAreas
* • 30 Vehicles (12 Cars, 10 Minibuses, 8 Trucks)
* • 1 FerryThread
*/
public class Simulation {
// ── Vehicle counts from the specification ─────────────────────────────────
private static final int CAR_COUNT = 12;
private static final int MINIBUS_COUNT = 10;
private static final int TRUCK_COUNT = 8;
private static final int TOTAL = CAR_COUNT + MINIBUS_COUNT + TRUCK_COUNT;
public void run() throws InterruptedException {
// ── Start clock ───────────────────────────────────────────────────────
long startTime = System.currentTimeMillis();
SimLogger.setStartTime(startTime);
// ── Infrastructure ────────────────────────────────────────────────────
TollBooth[] tolls = {
new TollBooth(0), // Side A
new TollBooth(1) // Side B
};
WaitingArea[] areas = {
new WaitingArea(0), // Side A
new WaitingArea(1) // Side B
};
// ── Ferry ─────────────────────────────────────────────────────────────
int ferrySide = (Math.random() < 0.5) ? 0 : 1;
CountDownLatch allDone = new CountDownLatch(1);
FerryThread ferry = new FerryThread(areas, ferrySide, TOTAL, allDone);
// ── Create vehicles ───────────────────────────────────────────────────
List<Vehicle> vehicles = new ArrayList<>();
for (int i = 1; i <= CAR_COUNT; i++) {
int side = (Math.random() < 0.5) ? 0 : 1;
vehicles.add(new Vehicle("Car-" + i, VehicleType.CAR, side,
tolls, areas, ferry));
}
for (int i = 1; i <= MINIBUS_COUNT; i++) {
int side = (Math.random() < 0.5) ? 0 : 1;
vehicles.add(new Vehicle("Minibus-" + i, VehicleType.MINIBUS, side,
tolls, areas, ferry));
}
for (int i = 1; i <= TRUCK_COUNT; i++) {
int side = (Math.random() < 0.5) ? 0 : 1;
vehicles.add(new Vehicle("Truck-" + i, VehicleType.TRUCK, side,
tolls, areas, ferry));
}
// ── Start ferry first, then all vehicles ──────────────────────────────
ferry.start();
for (Vehicle v : vehicles) v.start();
// ── Wait for all vehicles to finish ───────────────────────────────────
allDone.await();
ferry.interrupt(); // signal ferry to stop
ferry.join(2000);
// Join all vehicle threads
for (Vehicle v : vehicles) v.join(1000);
long simTime = System.currentTimeMillis() - startTime;
// ── Statistics ────────────────────────────────────────────────────────
printStatistics(vehicles, ferry, simTime);
}
// ─────────────────────────────────────────────────────────────────────────
private void printStatistics(List<Vehicle> vehicles,
FerryThread ferry,
long simTime) {
System.out.println();
System.out.println("══════════════════════════════════════════════");
System.out.println(" SIMULATION STATISTICS ");
System.out.println("══════════════════════════════════════════════");
System.out.printf(" Total simulation time : %d ms%n", simTime);
long totalWait = 0;
long maxWait = 0;
for (Vehicle v : vehicles) {
totalWait += v.getTotalWaitMs();
if (v.getMaxWaitMs() > maxWait) maxWait = v.getMaxWaitMs();
}
long avgWait = vehicles.isEmpty() ? 0 : totalWait / vehicles.size();
System.out.printf(" Average waiting time : %d ms%n", avgWait);
System.out.printf(" Maximum waiting time : %d ms%n", maxWait);
System.out.printf(" Number of ferry trips : %d%n",
ferry.getTripCount());
// Utilization = total units carried / (trips × MAX_CAPACITY)
int maxPossibleUnits =
ferry.getTripCount() * FerryThread.MAX_CAPACITY;
double utilization = maxPossibleUnits == 0 ? 0.0
: (double) ferry.getTotalUnitsCarried() / maxPossibleUnits * 100.0;
System.out.printf(" Ferry utilization ratio : %.1f%%%n", utilization);
System.out.printf(" Total capacity units moved: %d%n",
ferry.getTotalUnitsCarried());
System.out.println("══════════════════════════════════════════════");
System.out.println(" Simulation completed successfully. ");
System.out.println("══════════════════════════════════════════════");
}
}