commit c041d169df9ba65e44f744ae1ce58e8b8a754793 Author: JISAUAY Date: Wed Oct 2 17:53:57 2024 -0500 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1b2d693 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1eb3ab5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BridgeSimulator.iml b/BridgeSimulator.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/BridgeSimulator.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/BridgeSimulator.java b/src/BridgeSimulator.java new file mode 100644 index 0000000..451855e --- /dev/null +++ b/src/BridgeSimulator.java @@ -0,0 +1,84 @@ + +import java.util.concurrent.Semaphore; +import java.util.*; + +public class BridgeSimulator { + public static void main(String[] args) { + int simulationTime = 30; // In simulated seconds + int emergencies = 2; + + + TrafficController trafficController = new TrafficController(simulationTime, emergencies); + + + } + + Vehicle generateVehicle() { + Random rand = new Random(); + + int r = rand.nextInt(2); + if (r == 0) { + return new Truck(); + } else if (emegencyCounter > emergencies) { + return new EmergencyVehicle(); + } else { + return new Car(); + } + } + + +} + +class TrafficController { + int simulationTime; + int emergencies; + int currentTime = 0; + int trafficCounter = 0; + int emegencyCounter = 0; + + Semaphore northQueue = new Semaphore(10); + Semaphore southQueue = new Semaphore(10); + Semaphore bridge = new Semaphore(1); + Semaphore emergencyQueue = new Semaphore(10); + + TrafficController(int simulationTime, int emergencies) { + this.simulationTime = simulationTime; + this.emergencies = emergencies; + } + + // Simulate Vehicles arriving + void addVehicle() { + Vehicle vehicle = generateVehicle(); + + if (vehicle instanceof EmergencyVehicle) { + emergencyQueue.add(vehicle); + return; + } + + Random rand = new Random(); + int r = rand.nextInt(2); + if (r == 0) { + northQueue.add(vehicle); + } else { + southQueue.add(vehicle); + } + } + + + + public Semaphore getNorthQueue() { + return northQueue; + } + + public Semaphore getSouthQueue() { + return southQueue; + } + + public Semaphore getBridge() { + return bridge; + } + + public Semaphore getEmergencyQueue() { + return emergencyQueue; + } +} \ No newline at end of file diff --git a/src/Car.java b/src/Car.java new file mode 100644 index 0000000..8f4322c --- /dev/null +++ b/src/Car.java @@ -0,0 +1,15 @@ +public class Car extends Vehicle { + public static int crossingTime = 2; + public static int priority = 2; + + public Car() {} + + public void run() { + try { + Thread.sleep(crossingTime); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Car failed to cross."); + } + } +} diff --git a/src/EmergencyVehicle.java b/src/EmergencyVehicle.java new file mode 100644 index 0000000..bb445a7 --- /dev/null +++ b/src/EmergencyVehicle.java @@ -0,0 +1,15 @@ +public class EmergencyVehicle extends Vehicle { + public static int crossingTime = 1; + public static int priority = 1; + + public EmergencyVehicle() {} + + public void run() { + try { + Thread.sleep(crossingTime); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Car failed to cross."); + } + } +} diff --git a/src/Truck.java b/src/Truck.java new file mode 100644 index 0000000..ee1935b --- /dev/null +++ b/src/Truck.java @@ -0,0 +1,15 @@ +public class Truck extends Vehicle { + public static int crossingTime = 3; + public static int priority = 2; + + public Truck() {} + + public void run() { + try { + Thread.sleep(crossingTime); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Truck failed to cross."); + } + } +} diff --git a/src/Vehicle.java b/src/Vehicle.java new file mode 100644 index 0000000..c13308d --- /dev/null +++ b/src/Vehicle.java @@ -0,0 +1,4 @@ +public abstract class Vehicle implements Runnable { + public static int crossingTime; + public static int priority; +}