import 'dart:io'; // 2. Device Status: Initialize device states bool lightsOn = false; int thermostatTemp = 20; // Default 20°C bool securityArmed = false; // Function to display the system status (Requirement 5) void displayStatus() { print('\n--- System Status ---'); print('Lights: ${lightsOn ? 'ON' : 'OFF'}'); print('Thermostat: $thermostatTemp°C'); print('Security System: ${securityArmed ? 'ARMED' : 'DISARMED'}'); print('---------------------\n'); } // 1. Main Method: The program starts here (Requirement 1) void main() { print('Welcome to the Smart Home Automation System CLI!'); // 3. User Interaction: Loop for continuous interaction (Requirement 3, Control Flow) while (true) { print('Please select an option:'); print(' 1. Toggle Lights (Currently: ${lightsOn ? 'ON' : 'OFF'})'); print(' 2. Set Thermostat Temperature (Current: $thermostatTemp°C)'); print(' 3. Toggle Security System (Currently: ${securityArmed ? 'ARMED' : 'DISARMED'})'); print(' 4. View System Status'); print(' 5. Exit Program'); stdout.write('Your choice: '); // Read and parse the user's choice final choice = stdin.readLineSync(); // 5. Control Flow: Use if/else to handle menu options (Requirement 5) if (choice == '1') { lightsOn = !lightsOn; // Toggle the state (Requirement 3) print('Lights are now ${lightsOn ? 'ON' : 'OFF'}!'); } else if (choice == '2') { stdout.write('Enter new temperature (15°C - 30°C): '); final tempInput = stdin.readLineSync(); // Use try-catch for robust conversion try { final newTemp = int.parse(tempInput ?? ''); // 4. Input Validation: Check temperature range (Requirement 4) if (newTemp >= 15 && newTemp <= 30) { thermostatTemp = newTemp; print('Thermostat set to $thermostatTemp°C!'); } else { print('❌ Invalid temperature. Must be between 15°C and 30°C.'); } } catch (_) { print('❌ Invalid input. Please enter a number.'); } } else if (choice == '3') { securityArmed = !securityArmed; // Toggle the state (Requirement 3) print('Security System is now ${securityArmed ? 'ARMED' : 'DISARMED'}!'); } else if (choice == '4') { displayStatus(); } else if (choice == '5') { print('Exiting the Smart Home Automation Program. Goodbye!'); break; // Exit the loop (Requirement 3) } else { print('❌ Invalid choice. Please select an option from 1 to 5.'); } } }