Menu
Coddy logo textTech

Recap - Traffic Light

Part of the Logic & Flow section of Coddy's Dart journey — lesson 62 of 65.

challenge icon

Challenge

Easy

Create a comprehensive traffic light management system that demonstrates your mastery of enums and switch statements. Your task is to build a system that can handle multiple traffic lights and provide appropriate actions for drivers.

  1. Define an enum called TrafficLight with three values: red, yellow, and green
  2. Create a function called getDriverAction that takes a TrafficLight parameter and returns the appropriate action string using a switch statement:
    • For TrafficLight.red: return "Stop"
    • For TrafficLight.yellow: return "Caution"
    • For TrafficLight.green: return "Go"
  3. Create a function called getWaitTime that takes a TrafficLight parameter and returns the wait time in seconds using a switch statement:
    • For TrafficLight.red: return 30
    • For TrafficLight.yellow: return 3
    • For TrafficLight.green: return 25
  4. Create three variables to represent different intersections:
    • intersection1 of type TrafficLight set to TrafficLight.red
    • intersection2 of type TrafficLight set to TrafficLight.yellow
    • intersection3 of type TrafficLight set to TrafficLight.green
  5. For each intersection, print the status in this exact format:
    • Intersection 1: Red light - Stop - Wait time: 30 seconds
    • Intersection 2: Yellow light - Caution - Wait time: 3 seconds
    • Intersection 3: Green light - Go - Wait time: 25 seconds
  6. Simulate a traffic light sequence change by updating intersection1 to TrafficLight.green
  7. Print the updated status: Updated Intersection 1: Green light - Go - Wait time: 25 seconds
  8. Create a summary by printing: Traffic system managing 3 intersections successfully

Your program should demonstrate the power of combining enums with switch statements to create a type-safe, readable traffic management system. The switch statements must handle all enum values and return the exact strings and numbers specified above.

Try it yourself

import 'dart:io';

// TODO: Define your TrafficLight enum here

// TODO: Create your getDriverAction function here

// TODO: Create your getWaitTime function here

void main() {
  // TODO: Write your code below
  
  // Create intersection variables
  
  // Print status for each intersection
  
  // Update intersection1 and print updated status
  
  // Print summary
}

All lessons in Logic & Flow