Write the classes for the traffic signals in the course project. As the first part of the course project, let us create the traffic lights and Walk/Don't Walk signs.
To design a traffic simulation with the given requirements, we'll define classes for the traffic light and the pedestrian signal. We'll also ensure synchronization between the two to maintain safety.
// Enum to represent Traffic Light states
enum TrafficLightState {
RED, GREEN, YELLOW
}
// Enum to represent Pedestrian Signal states
enum PedestrianSignalState {
WALK, DONT_WALK, FLASHING_DONT_WALK
}
class TrafficLight {
private TrafficLightState state;
private final int MIN_TIME = 5; // Minimum time for each state in seconds
public TrafficLight() {
this.state = TrafficLightState.GREEN;
}
public TrafficLightState getState() {
return state;
}
public void changeState() {
switch (state) {
case RED:
state = TrafficLightState.GREEN;
break;
case GREEN:
state = TrafficLightState.YELLOW;
break;
case YELLOW:
state = TrafficLightState.RED;
break;
}
}
public void waitForMinimumTime() throws InterruptedException {
Thread.sleep(MIN_TIME * 1000);
}
}
class PedestrianSignal {
private PedestrianSignalState state;
private boolean buttonPressed;
private final int MIN_TIME = 5; // Minimum time for each state in seconds
public PedestrianSignal() {
this.state = PedestrianSignalState.DONT_WALK;
this.buttonPressed = false;
}
public PedestrianSignalState getState() {
return state;
}
public void pressButton() {
buttonPressed = true;
}
public void changeState(TrafficLight trafficLight) {
if (buttonPressed) {
switch (state) {
case WALK:
state = PedestrianSignalState.FLASHING_DONT_WALK;
break;
case FLASHING_DONT_WALK:
state = PedestrianSignalState.DONT_WALK;
trafficLight.changeState(); // Change traffic light to green after pedestrian signal completes its cycle
break;
case DONT_WALK:
if (trafficLight.getState() == TrafficLightState.RED) {
state = PedestrianSignalState.WALK;
}
break;
}
buttonPressed = false;
}
}
public void waitForMinimumTime() throws InterruptedException {
Thread.sleep(MIN_TIME * 1000);
}
}
public class TrafficSimulation {
public static void main(String[] args) throws InterruptedException {
TrafficLight trafficLight = new TrafficLight();
PedestrianSignal pedestrianSignal = new PedestrianSignal();
while (true) {
if (trafficLight.getState() == TrafficLightState.GREEN && pedestrianSignal.getState() == PedestrianSignalState.DONT_WALK) {
trafficLight.waitForMinimumTime();
trafficLight.changeState();
} else if (trafficLight.getState() == TrafficLightState.RED && pedestrianSignal.getState() == PedestrianSignalState.WALK) {
pedestrianSignal.waitForMinimumTime();
pedestrianSignal.changeState(trafficLight);
}
}
}
}
Explanation
- We've defined enums for the states of both the traffic light and the pedestrian signal.
- The `TrafficLight` class manages the states of the traffic light and ensures a minimum time for each state.
- The `PedestrianSignal` class manages the states of the pedestrian signal, and the button press logic is encapsulated within it.
- The `TrafficSimulation` class in the `main` method orchestrates the simulation, ensuring synchronization between the traffic light and the pedestrian signal based on the given requirements.