mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 20:43:32 +00:00
Move vehicle route advancement to intersection arrival
This commit is contained in:
@@ -280,8 +280,7 @@ public class IntersectionProcess {
|
|||||||
System.out.println("[" + intersectionId + "] Sent vehicle " + vehicle.getId() +
|
System.out.println("[" + intersectionId + "] Sent vehicle " + vehicle.getId() +
|
||||||
" to " + nextDestination);
|
" to " + nextDestination);
|
||||||
|
|
||||||
// Update vehicle's path - advance to next destination in route
|
// Note: vehicle route is advanced when it arrives at the next intersection
|
||||||
vehicle.advanceRoute();
|
|
||||||
|
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
System.err.println("[" + intersectionId + "] Failed to send vehicle " +
|
System.err.println("[" + intersectionId + "] Failed to send vehicle " +
|
||||||
|
|||||||
@@ -104,16 +104,28 @@ public class Intersection {
|
|||||||
* Accepts an incoming vehicle and places it in the correct queue.
|
* Accepts an incoming vehicle and places it in the correct queue.
|
||||||
* * This method:
|
* * This method:
|
||||||
* 1. Increments the {@link #totalVehiclesReceived} counter.
|
* 1. Increments the {@link #totalVehiclesReceived} counter.
|
||||||
* 2. Gets the vehicle's *next* destination (from {@link Vehicle#getCurrentDestination()}).
|
* 2. Advances the vehicle's route (since it just arrived here)
|
||||||
* 3. Uses the {@link #routing} map to find the correct *direction* for that destination.
|
* 3. Gets the vehicle's *next* destination (from {@link Vehicle#getCurrentDestination()}).
|
||||||
* 4. Adds the vehicle to the queue of the {@link TrafficLight} for that direction.
|
* 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.
|
* @param vehicle The {@link Vehicle} arriving at the intersection.
|
||||||
*/
|
*/
|
||||||
public void receiveVehicle(Vehicle vehicle) {
|
public void receiveVehicle(Vehicle vehicle) {
|
||||||
totalVehiclesReceived++;
|
totalVehiclesReceived++;
|
||||||
|
|
||||||
|
// Advance route since vehicle just arrived at this intersection
|
||||||
|
vehicle.advanceRoute();
|
||||||
|
|
||||||
String nextDestination = vehicle.getCurrentDestination();
|
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);
|
String direction = routing.get(nextDestination);
|
||||||
|
|
||||||
if (direction != null && trafficLights.containsKey(direction)) {
|
if (direction != null && trafficLights.containsKey(direction)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user