#include <iostream>
#include <chrono>
#include <thread>
// Enum to represent traffic light states
enum class LightState {
RED,
YELLOW,
GREEN
};
// Class to represent a traffic light
class TrafficLight {
public:
TrafficLight() : currentState(LightState::RED) {}
// Method to update the state of the traffic light
void update() {
switch (currentState) {
case LightState::RED:
currentState = LightState::GREEN;
break;
case LightState::YELLOW:
currentState = LightState::RED;
break;
case LightState::GREEN:
currentState = LightState::YELLOW;
break;
}
}
// Method to print the current state of the traffic light
void printStatus() const {
switch (currentState) {
case LightState::RED:
std::cout << "Red Light\n";
break;
case LightState::YELLOW:
std::cout << "Yellow Light\n";
break;
case LightState::GREEN:
std::cout << "Green Light\n";
break;
}
}
private:
LightState currentState;
};
int main() {
TrafficLight trafficLight;
// Simulation parameters
const int redDuration = 5; // Duration for red light in seconds
const int yellowDuration = 2; // Duration for yellow light in seconds
const int greenDuration = 4; // Duration for green light in seconds
while (true) {
// Print the current status
trafficLight.printStatus();
// Wait for the duration corresponding to the current light
switch (trafficLight.currentState) {
case LightState::RED:
std::this_thread::sleep_for(std::chrono::seconds(redDuration));
break;
case LightState::YELLOW:
std::this_thread::sleep_for(std::chrono::seconds(yellowDuration));
break;
case LightState::GREEN:
std::this_thread::sleep_for(std::chrono::seconds(greenDuration));
break;
}
// Update the traffic light to the next state
trafficLight.update();
}
return 0;
}