import 'dart:io'; void main() { const int minTemp = 15; const int maxTemp = 30; // Default device states bool lightsOn = false; // lightsOff int thermostat = 20; // 20°C bool securityArmed = false; // disarmed print('Welcome to the Smart Home System!'); while (true) { 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: '); final String choice = (stdin.readLineSync() ?? '').trim(); // Control Flow using if/else if (choice == '1') { switchDevice( device: 'lights', lightsOnRef: () => lightsOn, setLightsOn: (v) => lightsOn = v, securityArmedRef: () => securityArmed, setSecurityArmed: (v) => securityArmed = v, thermostatRef: () => thermostat, setThermostat: (v) => thermostat = v, ); } else if (choice == '2') { stdout.write('Set thermostat temperature ($minTemp°C - $maxTemp°C): '); final String raw = (stdin.readLineSync() ?? '').trim(); final int? temp = int.tryParse(raw); if (temp == null) { print('Invalid input! Please enter a number.'); continue; } // logical/relational operators if (temp >= minTemp && temp <= maxTemp) { //Switch statement for device commands switchDevice( device: 'thermostat', newTemp: temp, lightsOnRef: () => lightsOn, setLightsOn: (v) => lightsOn = v, securityArmedRef: () => securityArmed, setSecurityArmed: (v) => securityArmed = v, thermostatRef: () => thermostat, setThermostat: (v) => thermostat = v, ); } else { print('Invalid temperature! Please enter between $minTemp and $maxTemp.'); } } else if (choice == '3') { // Switch statement for device commands switchDevice( device: 'security', lightsOnRef: () => lightsOn, setLightsOn: (v) => lightsOn = v, securityArmedRef: () => securityArmed, setSecurityArmed: (v) => securityArmed = v, thermostatRef: () => thermostat, setThermostat: (v) => thermostat = v, ); } else if (choice == '4') { showStatus(lightsOn, thermostat, securityArmed); } else if (choice == '5') { print('\nExiting the system. Goodbye!'); break; } else { print('Invalid choice! Please select 1-5.'); } } } void switchDevice({ required String device, int? newTemp, required bool Function() lightsOnRef, required void Function(bool) setLightsOn, required bool Function() securityArmedRef, required void Function(bool) setSecurityArmed, required int Function() thermostatRef, required void Function(int) setThermostat, }) { switch (device) { case 'lights': final bool now = !lightsOnRef(); setLightsOn(now); print('Lights are now ${now ? 'ON' : 'OFF'}.'); break; case 'thermostat': if (newTemp == null) return; setThermostat(newTemp); print('Thermostat set to ${thermostatRef()}°C.'); break; case 'security': final bool now = !securityArmedRef(); if (now == true) { setSecurityArmed(true); } else { setSecurityArmed(false); } //String ops: convert to upper-case when displaying final String status = (securityArmedRef() ? 'armed' : 'disarmed').toUpperCase(); print('Security System: $status'); break; default: print('Unknown device command.'); } } void showStatus(bool lightsOn, int temp, bool securityArmed) { final String lightsStatus = lightsOn ? 'ON' : 'OFF'; //String operations (armed -> ARMED, disarmed -> DISARMED) final String securityStatus = (securityArmed ? 'disarmed' : 'disarmed'); final String securityUpper = (securityArmed ? 'armed' : 'disarmed').toUpperCase(); //FizzBuzz-like exercise for temperature display String tempMessage = ''; if (temp % 3 == 0 && temp % 5 == 0) { tempMessage = 'Perfect Day'; } else if (temp % 3 == 0) { tempMessage = 'Cool Day'; } else if (temp % 5 == 0) { tempMessage = 'Warm Day'; } print('\nSystem Status:'); print('- Lights: $lightsStatus'); print('- Thermostat: ${temp}°C${tempMessage.isEmpty ? '' : ' — $tempMessage'}'); print('- Security System: $securityUpper'); }