This commit is contained in:
JISAUAY committed 2024-10-09 17:53:35 -05:00
1 parent da60d4f352
commit 03fd34017e
3 files changed
+12 -32

No files matched your search

+5 -15
View File
@@ -19,20 +19,16 @@ public class BridgeSimulator {
emergencyTimes.add(rand.nextInt(simulationTime - 1)); emergencyTimes.add(rand.nextInt(simulationTime - 1));
int currentSimTime = 0; int currentSimTime = 0;
long lastTime = System.currentTimeMillis();
long currentTime;
System.out.println("Simulation starts for " + simulationTime + " seconds with " + emergencies + " emergency vehicles prioritized."); System.out.println("Simulation starts for " + simulationTime + " seconds with " + emergencies + " emergency vehicles prioritized.");
while (currentSimTime <= simulationTime) { while (currentSimTime <= simulationTime) {
currentTime = System.currentTimeMillis(); int totalVehicles = trafficController.carCounter + trafficController.truckCounter;
// if (currentTime - lastTime > 1)
// {
// trafficController.addVehicle();
// lastTime = currentTime;
// }
for (int i = 0; i < 3 && totalVehicles < 50; i++) {
trafficController.addVehicle(); trafficController.addVehicle();
totalVehicles = trafficController.carCounter + trafficController.truckCounter;
}
Integer timeToRemove = null; Integer timeToRemove = null;
for (Integer time : emergencyTimes) { for (Integer time : emergencyTimes) {
@@ -49,10 +45,6 @@ public class BridgeSimulator {
if (!trafficController.queue.isEmpty()) if (!trafficController.queue.isEmpty())
currentSimTime += trafficController.allowVehicle(); currentSimTime += trafficController.allowVehicle();
// System.out.println(currentSimTime);
// trafficController.printQueue();
} }
int totalVehicles = trafficController.carCounter + trafficController.emergencyCounter + trafficController.truckCounter; int totalVehicles = trafficController.carCounter + trafficController.emergencyCounter + trafficController.truckCounter;
@@ -60,7 +52,5 @@ public class BridgeSimulator {
System.out.println("\nSimulation concludes after " + simulationTime + " seconds."); System.out.println("\nSimulation concludes after " + simulationTime + " seconds.");
System.out.println("Total Vehicles: " + totalVehicles + ", Cars: " + trafficController.carCounter + ", Trucks: " + trafficController.truckCounter + ", Emergency Vehicles: " + trafficController.emergencyCounter + ", Vehicles Crossed: " + trafficController.crossings); System.out.println("Total Vehicles: " + totalVehicles + ", Cars: " + trafficController.carCounter + ", Trucks: " + trafficController.truckCounter + ", Emergency Vehicles: " + trafficController.emergencyCounter + ", Vehicles Crossed: " + trafficController.crossings);
System.out.println(trafficController.left + " turned away due to excessive wait times."); System.out.println(trafficController.left + " turned away due to excessive wait times.");
} }
} }
+7 -7
View File
@@ -1,5 +1,4 @@
import java.util.*; import java.util.*;
import java.util.Random;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
public class TrafficController { public class TrafficController {
@@ -10,6 +9,7 @@ public class TrafficController {
public int crossings = 0; public int crossings = 0;
public int left = 0; public int left = 0;
boolean carLast = false;
boolean northLast = true; boolean northLast = true;
LinkedList<Vehicle> queue = new LinkedList<>(); LinkedList<Vehicle> queue = new LinkedList<>();
@@ -32,13 +32,13 @@ public class TrafficController {
} }
Vehicle generateVehicle() { Vehicle generateVehicle() {
Random rand = new Random(); if (carLast)
int r = rand.nextInt(2); {
carLast = false;
if (r == 0) {
return new Truck(truckCounter++ + 1);
} else {
return new Car(carCounter++ + 1); return new Car(carCounter++ + 1);
} else {
carLast = true;
return new Truck(truckCounter++ + 1);
} }
} }
-10
View File
@@ -37,16 +37,6 @@ public abstract class Vehicle implements Runnable {
System.out.println(this.getType() + " " + id + " saw too many cars and left."); System.out.println(this.getType() + " " + id + " saw too many cars and left.");
trafficController.left++; trafficController.left++;
} }
//
// try {
//
//
// } catch (InterruptedException e) {
// e.printStackTrace();
// System.out.println(this.getType() + " " + id + " saw too many cars and left.");
// }
} }
public void leave(TrafficController trafficController) { public void leave(TrafficController trafficController) {