// Smart Home Automation CLI - Dart // Assignment 1 - Problem 1 // File: NaifAlkanhal_251000447_Smart Home Automation Problem1.txt // // How to run (after saving as .dart if needed): // dart run smart_home.dart // // Note: This submission is saved as .txt per instructions, but the content is valid Dart code. import 'dart:io'; const int minTempC = 15; const int maxTempC = 30; enum SecurityState { armed, disarmed } class SmartHomeSystem { bool lightsOn; int thermostatC; SecurityState securityState; SmartHomeSystem({ required this.lightsOn, required this.thermostatC, required this.securityState, }); void toggleLights() { lightsOn = !lightsOn; stdout.writeln('Lights are now ${lightsOn ? "ON" : "OFF"}.'); } void setThermostat(int newTempC) { thermostatC = newTempC; stdout.writeln('Thermostat set to ${thermostatC}°C.'); } void toggleSecurity() { securityState = (securityState == SecurityState.armed) ? SecurityState.disarmed : SecurityState.armed; final statusText = securityState == SecurityState.armed ? 'ARMED' : 'DISARMED'; stdout.writeln('Security system is now $statusText.'); } String _tempLabelFizzBuzz(int tempC) { // FizzBuzz-like requirement: // multiple of 3 => "Cool Day" // multiple of 5 => "Warm Day" // multiple of both => "Perfect Day" final bool mult3 = (tempC % 3 == 0); final bool mult5 = (tempC % 5 == 0); if (mult3 && mult5) return 'Perfect Day'; if (mult3) return 'Cool Day'; if (mult5) return 'Warm Day'; return ''; } void showStatus() { final lightsText = lightsOn ? 'ON' : 'OFF'; final securityText = (securityState == SecurityState.armed) ? 'armed' : 'disarmed'; // String operation example: convert to upper-case for display final securityDisplay = securityText.toUpperCase(); final label = _tempLabelFizzBuzz(thermostatC); final tempLine = label.isEmpty ? '${thermostatC}°C' : '${thermostatC}°C - $label'; stdout.writeln('\nSystem Status:'); stdout.writeln('- Lights: $lightsText'); stdout.writeln('- Thermostat: $tempLine'); stdout.writeln('- Security System: $securityDisplay\n'); } } String _readLineTrimmed() { return (stdin.readLineSync() ?? '').trim(); } int _readMenuChoice({required int min, required int max}) { while (true) { stdout.write('Enter your choice: '); final input = _readLineTrimmed(); final value = int.tryParse(input); if (value != null && value >= min && value <= max) { return value; } stdout.writeln('Invalid choice. Please enter a number between $min and $max.'); } } int _readTemperatureC() { while (true) { stdout.write('Set thermostat temperature ($minTempC°C - $maxTempC°C): '); final input = _readLineTrimmed(); final value = int.tryParse(input); // Logical & relational operators for validation as required if (value != null && value >= minTempC && value <= maxTempC) { return value; } stdout.writeln('Invalid temperature. Please enter a value between $minTempC and $maxTempC.'); } } void main() { final system = SmartHomeSystem( // Default values per requirements: lightsOn: false, // lightsOff thermostatC: 20, // 20°C securityState: SecurityState.disarmed, ); stdout.writeln('Welcome to the Smart Home System!'); bool running = true; while (running) { stdout.writeln('\nPlease choose an action:'); stdout.writeln('1. Toggle lights'); stdout.writeln('2. Set thermostat temperature'); stdout.writeln('3. Arm/disarm security system'); stdout.writeln('4. View system status'); stdout.writeln('5. Exit'); final choice = _readMenuChoice(min: 1, max: 5); // Control flow requirement: if/else for user choices if (choice == 1) { // Switch requirement: handle device commands (lights, thermostat, security) const device = 'lights'; switch (device) { case 'lights': system.toggleLights(); break; case 'thermostat': // Not used here break; case 'security': // Not used here break; default: stdout.writeln('Unknown device.'); } } else if (choice == 2) { const device = 'thermostat'; switch (device) { case 'lights': // Not used here break; case 'thermostat': final temp = _readTemperatureC(); system.setThermostat(temp); break; case 'security': // Not used here break; default: stdout.writeln('Unknown device.'); } } else if (choice == 3) { const device = 'security'; switch (device) { case 'lights': // Not used here break; case 'thermostat': // Not used here break; case 'security': system.toggleSecurity(); break; default: stdout.writeln('Unknown device.'); } } else if (choice == 4) { system.showStatus(); } else { stdout.writeln('Exiting the system. Goodbye!'); running = false; } } }