Design Patterns  «Prev  Next»
Lesson 8Course project, part 2
ObjectiveWrite classes for traffic signals

Course Project - Traffic Signals

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.
Here are some of the specifications for the project:
  1. Traffic lights have an intermediate yellow state, and always pass through their three possible states in the cyclic order red/green/yellow/red.
  2. Walk/Don't Walk signs have an intermediate flashing don't walk state, and always pass through their three possible states in the cyclic order walk/flashing don't walk/don't walk/walk.
  3. There should be a minimum time associated with each light state (for instance, you don't want pedestrians to have to race across the street in under 5 seconds). However, the traffic light probably should not have a maximum time on green.
    Other states may need maximum times to allow the traffic light to revert to green Remember, pedestrians have to push a button to get a walk sign. If no one pushes a button, the pedestrian signal remains locked in "Don't Walk"
  4. By default, the traffic light is green until a pedestrian pushes a button on the light indicating they wish to cross the street.
  5. Most importantly, the lights must be connected. It is important to insure that whenever the traffic light is green, the crosswalk sign reads,
    Don't Walk
Write the classes for the traffic signals in a Traffic Simulation using the Java programming language.
  1. Traffic lights have an intermediate yellow state, and always pass through their three possible states in the cyclic order red/green/yellow/red.
  2. Walk/Don't Walk signs have an intermediate flashing "Don't walk" state, and always pass through their three possible states in the cyclic order walk/flashing "Don't Walk/Don't walk/walk".
  3. There should be a minimum time associated with each light state (for instance, you don't want pedestrians to have to race across the street in under 5 seconds).
  4. However, the traffic light probably should not have a maximum time on green
  5. Other states may need maximum times to allow the traffic light to revert to green.
  6. Remember, pedestrians have to push a button to get a walk sign. If no one pushes a button, the pedestrian signal remains locked in "Don't Walk".
  7. By default, the traffic light is green until a pedestrian pushes a button on the light indicating they wish to cross the street.
  8. Most importantly, the lights must be connected. It is important to insure that whenever the traffic light is green, the crosswalk sign reads, Don't Walk


Traffic Simulation: Traffic Lights and Pedestrian Signals in Java

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

  1. We've defined enums for the states of both the traffic light and the pedestrian signal.
  2. The `TrafficLight` class manages the states of the traffic light and ensures a minimum time for each state.
  3. The `PedestrianSignal` class manages the states of the pedestrian signal, and the button press logic is encapsulated within it.
  4. 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.

Abstract Traffic Signal - Exercise

In this exercise, you will be writing the classes for the traffic lights.
Abstract Traffic Signal - Exercise

SEMrush Software