-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficMonitoringSystem.java
More file actions
114 lines (98 loc) · 4.07 KB
/
Copy pathTrafficMonitoringSystem.java
File metadata and controls
114 lines (98 loc) · 4.07 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
import java.util.Random;
class TrafficMonitoringSystem {
// Static variables for city-wide statistics
private static int totalVehiclesMonitored = 0;
private static int highCongestionCount = 0;
// Intersections & road segments
private static final String[] INTERSECTIONS = {"A", "B", "C", "D", "E"};
private static final String[] DIRECTIONS = {"North", "South", "East", "West"};
// Traffic data storage
private int[][] hourlySpeeds; // [24][4] - Speeds for each hour and road segment
private int[] vehicleCounts; // [4] - Total vehicles per road segment
// Constructor to initialize data
public TrafficMonitoringSystem(int[][] speeds, int[] counts) {
this.hourlySpeeds = speeds;
this.vehicleCounts = counts;
for (int count : counts) {
totalVehiclesMonitored += count;
}
}
// Calculate the average speed at the intersection
public double calculateAverageSpeed() {
int sum = 0, totalEntries = 0;
for (int[] hour : hourlySpeeds) {
for (int speed : hour) {
sum += speed;
totalEntries++;
}
}
return (double) sum / totalEntries;
}
// Calculate total vehicles for the intersection
public int getTotalVehicles() {
int sum = 0;
for (int count : vehicleCounts) {
sum += count;
}
return sum;
}
// Determine congestion level based on speed and vehicle density
public String determineCongestionLevel(double avgSpeed, int totalVehicles) {
String congestion;
if (totalVehicles > 700 || avgSpeed < 35) {
congestion = "High";
highCongestionCount++;
} else if (totalVehicles > 600 || avgSpeed < 45) {
congestion = "Moderate";
} else {
congestion = "Low";
}
return congestion;
}
// Generate report for a single intersection
public void generateReport(String intersectionName) {
double avgSpeed = calculateAverageSpeed();
int totalVehicles = getTotalVehicles();
String congestionLevel = determineCongestionLevel(avgSpeed, totalVehicles);
System.out.printf("Intersection %s: - Total Vehicles: %d - Average Speed: %.1f km/h - Congestion Level: %s%n",
intersectionName, totalVehicles, avgSpeed, congestionLevel);
}
// Static method to display citywide summary
public static void displayCitySummary() {
System.out.println("\nCitywide Totals:");
System.out.println("Total Vehicles Monitored: " + totalVehiclesMonitored);
System.out.println("High Congestion Intersections: " + highCongestionCount);
}
// Sample data generator
public static int[][] generateHourlySpeeds() {
Random rand = new Random();
int[][] speeds = new int[24][4];
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
speeds[i][j] = rand.nextInt(31) + 30; // Speeds between 30 - 60 km/h
}
}
return speeds;
}
public static int[] generateVehicleCounts() {
Random rand = new Random();
int[] counts = new int[4];
for (int i = 0; i < 4; i++) {
counts[i] = rand.nextInt(201) + 100; // Counts between 100 - 300
}
return counts;
}
public static void main(String[] args) {
TrafficMonitoringSystem[] intersections = new TrafficMonitoringSystem[INTERSECTIONS.length];
// Generate and store traffic data for each intersection
for (int i = 0; i < INTERSECTIONS.length; i++) {
intersections[i] = new TrafficMonitoringSystem(generateHourlySpeeds(), generateVehicleCounts());
}
System.out.println("Smart City Traffic Monitoring Report:");
for (int i = 0; i < INTERSECTIONS.length; i++) {
intersections[i].generateReport(INTERSECTIONS[i]);
}
// Display citywide summary
displayCitySummary();
}
}