diff --git a/main/src/main/java/sd/IntersectionProcess.java b/main/src/main/java/sd/IntersectionProcess.java index 66d55c8..11db78d 100644 --- a/main/src/main/java/sd/IntersectionProcess.java +++ b/main/src/main/java/sd/IntersectionProcess.java @@ -280,8 +280,7 @@ public class IntersectionProcess { System.out.println("[" + intersectionId + "] Sent vehicle " + vehicle.getId() + " to " + nextDestination); - // Update vehicle's path - advance to next destination in route - vehicle.advanceRoute(); + // Note: vehicle route is advanced when it arrives at the next intersection } catch (IOException | InterruptedException e) { System.err.println("[" + intersectionId + "] Failed to send vehicle " + diff --git a/main/src/main/java/sd/model/Intersection.java b/main/src/main/java/sd/model/Intersection.java index 718c98c..4475fc3 100644 --- a/main/src/main/java/sd/model/Intersection.java +++ b/main/src/main/java/sd/model/Intersection.java @@ -104,16 +104,28 @@ public class Intersection { * Accepts an incoming vehicle and places it in the correct queue. * * This method: * 1. Increments the {@link #totalVehiclesReceived} counter. - * 2. Gets the vehicle's *next* destination (from {@link Vehicle#getCurrentDestination()}). - * 3. Uses the {@link #routing} map to find the correct *direction* for that destination. - * 4. Adds the vehicle to the queue of the {@link TrafficLight} for that direction. + * 2. Advances the vehicle's route (since it just arrived here) + * 3. Gets the vehicle's *next* destination (from {@link Vehicle#getCurrentDestination()}). + * 4. Uses the {@link #routing} map to find the correct *direction* for that destination. + * 5. Adds the vehicle to the queue of the {@link TrafficLight} for that direction. * * @param vehicle The {@link Vehicle} arriving at the intersection. */ public void receiveVehicle(Vehicle vehicle) { totalVehiclesReceived++; + // Advance route since vehicle just arrived at this intersection + vehicle.advanceRoute(); + String nextDestination = vehicle.getCurrentDestination(); + + // Check if vehicle reached final destination + if (nextDestination == null) { + System.out.printf("[%s] Vehicle %s reached final destination%n", + this.id, vehicle.getId()); + return; + } + String direction = routing.get(nextDestination); if (direction != null && trafficLights.containsKey(direction)) {