Recap - Traffic Light
Part of the Logic & Flow section of Coddy's Dart journey — lesson 62 of 65.
Challenge
EasyCreate 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.
- Define an enum called
TrafficLightwith three values:red,yellow, andgreen - Create a function called
getDriverActionthat takes aTrafficLightparameter 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"
- For
- Create a function called
getWaitTimethat takes aTrafficLightparameter and returns the wait time in seconds using a switch statement:- For
TrafficLight.red: return30 - For
TrafficLight.yellow: return3 - For
TrafficLight.green: return25
- For
- Create three variables to represent different intersections:
intersection1of typeTrafficLightset toTrafficLight.redintersection2of typeTrafficLightset toTrafficLight.yellowintersection3of typeTrafficLightset toTrafficLight.green
- For each intersection, print the status in this exact format:
Intersection 1: Red light - Stop - Wait time: 30 secondsIntersection 2: Yellow light - Caution - Wait time: 3 secondsIntersection 3: Green light - Go - Wait time: 25 seconds
- Simulate a traffic light sequence change by updating
intersection1toTrafficLight.green - Print the updated status:
Updated Intersection 1: Green light - Go - Wait time: 25 seconds - 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
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List6Basic Error Handling
What are Exceptions?The 'try-catch' BlockCatching Exceptions with 'on'The 'finally' BlockThrowing an ExceptionRecap - Safe Division9Enumerations (Enums)
What are Enums?Defining a Simple EnumUsing Enums in VariablesEnums in 'switch' StatementsRecap - Traffic Light