import 'dart:io'; // 7. Final/Const Variables: Use const for any constant values const int MIN_TEMP = 15; const int MAX_TEMP = 30; // Device Status Variables (Initialized with default values) bool lightsOn = false; // lights: off int thermostatTemp = 20; // thermostat: 20°C String securityStatus = 'disarmed'; // security system: disarmed void main() { // 1. Main Method print("Welcome to the Smart Home System!"); // 3. User Interaction: Use a loop to allow continuous interaction bool running = true; while (running) { displayMenu(); // Read user choice String? choiceInput = stdin.readLineSync(); int? choice = int.tryParse(choiceInput ?? ''); if (choice != null) { // 5. Control Flow: Use if/else for choice validation if (choice < 1 || choice > 5) { print("\nInvalid choice. Please enter a number between 1 and 5."); continue; } // 5. Control Flow: Use a switch statement to handle user choices switch (choice) { case 1: toggleLights(); break; case 2: setThermostatTemperature(); break; case 3: armDisarmSecuritySystem(); break; case 4: viewSystemStatus(); break; case 5: running = false; print("\nExiting the Smart Home System. Goodbye!"); break; default: // This should ideally not be reached due to the preceding if/else check print("\nInvalid choice. Please try again."); } } else { print("\nInvalid input. Please enter a number."); } if (running) { print("\n" + ("-" * 30)); // Separator for better readability } } } void displayMenu() { print("\nPlease choose an action:"); print("1. Toggle lights"); print("2. Set thermostat temperature"); print("3. Arm/disarm security system"); print("4. View system status"); print("5. Exit"); stdout.write("\nEnter your choice: "); } void toggleLights() { lightsOn = !lightsOn; // 6. String Interpolation: Using string interpolation in output print("Lights are now ${lightsOn ? 'ON' : 'OFF'}."); } void setThermostatTemperature() { stdout.write("Set thermostat temperature (${MIN_TEMP}°C - ${MAX_TEMP}°C): "); String? tempInput = stdin.readLineSync(); int? newTemp = int.tryParse(tempInput ?? ''); // 4. Input Validation & 8. Logical & Relational Operators: Validate user input if (newTemp != null && newTemp >= MIN_TEMP && newTemp <= MAX_TEMP) { thermostatTemp = newTemp; print("Thermostat set to ${thermostatTemp}°C."); } else { print("Invalid temperature. Please set a value between $MIN_TEMP°C and $MAX_TEMP°C."); } } void armDisarmSecuritySystem() { if (securityStatus == 'disarmed') { securityStatus = 'armed'; print("Security system is now ARMED."); } else { securityStatus = 'disarmed'; print("Security system is now DISARMED."); } } void viewSystemStatus() { String lightsState = lightsOn ? "ON" : "OFF"; // 6. String Operations: Convert status to user-friendly message (e.g., "armed" to "ARMED") String securityState = securityStatus.toUpperCase(); // 5. Control Flow: Implement a FizzBuzz-like exercise for weather status String weatherMessage; if (thermostatTemp % 3 == 0 && thermostatTemp % 5 == 0) { weatherMessage = "Perfect Day"; } else if (thermostatTemp % 3 == 0) { weatherMessage = "Cool Day"; } else if (thermostatTemp % 5 == 0) { weatherMessage = "Warm Day"; } else { weatherMessage = "Normal"; // Default case } print("\nSystem Status:"); // 6. String Interpolation: Displaying status with device values print("- Lights: $lightsState"); print("- Thermostat: ${thermostatTemp}°C - $weatherMessage"); print("- Security System: $securityState"); }