import 'dart:io'; const int MIN_TEMP = 15; const int MAX_TEMP = 30; void main() { // القيم الافتراضية bool lightsOn = false; // false => OFF, true => ON int thermostat = 20; // الافتراضي 20°C bool securityArmed = false; // false => DISARMED print('Welcome to the Smart Home System!'); bool running = true; while (running) { showMenu(); stdout.write('\nEnter your choice: '); String? input = stdin.readLineSync(); int? choice = input != null ? int.tryParse(input.trim()) : null; if (choice == null) { print('Invalid input. Please enter a number from 1 to 5.\n'); continue; } switch (choice) { case 1: // Toggle lights lightsOn = !lightsOn; print('Lights are now ${lightsOn ? "ON" : "OFF"}.\n'); break; case 2: // Set thermostat setThermostat((int newTemp) { thermostat = newTemp; print('Thermostat set to ${thermostat}°C.\n'); }, thermostat); break; case 3: // Arm/disarm security securityArmed = !securityArmed; print('Security System is now ${securityArmed ? "ARMED" : "DISARMED"}.\n'); break; case 4: // View system status print('\nSystem Status:'); print('- Lights: ${lightsOn ? "ON" : "OFF"}'); print('- Thermostat: ${thermostat}°C ${fizzBuzzTemp(thermostat)}'); print('- Security System: ${securityArmed ? "ARMED" : "DISARMED"}\n'); break; case 5: print('Exiting the system. Goodbye!'); running = false; break; default: print('Choice out of range. Please choose between 1 and 5.\n'); } } } void showMenu() { print(''' Please choose an action: 1. Toggle lights 2. Set thermostat temperature 3. Arm/disarm security system 4. View system status 5. Exit '''); } /// setThermostat: تطلب من المستخدم إدخال درجة حرارة صحيحة ضمن النطاق. /// callback: دالة تُطبّق لتحديث المتغير في المكان الذي استُدعي منه. void setThermostat(void Function(int) callback, int currentTemp) { while (true) { stdout.write('Set thermostat temperature (${MIN_TEMP}°C - ${MAX_TEMP}°C). Current: ${currentTemp}°C: '); String? line = stdin.readLineSync(); if (line == null) { print('No input detected. Try again.'); continue; } int? temp = int.tryParse(line.trim()); if (temp == null) { print('Invalid number. Please enter an integer value.'); continue; } if (temp < MIN_TEMP || temp > MAX_TEMP) { print('Temperature out of range. Please enter between ${MIN_TEMP} and ${MAX_TEMP}.'); continue; } // قيمة صحيحة داخل النطاق callback(temp); break; } } /// fizzBuzzTemp: طبق متطلب FizzBuzz-like على درجة الحرارة /// - multiple of 3 => " - Cool Day" /// - multiple of 5 => " - Warm Day" /// - multiple of both => " - Perfect Day" /// وإلا => (سلسلة فارغة) String fizzBuzzTemp(int temp) { bool by3 = temp % 3 == 0; bool by5 = temp % 5 == 0; if (by3 && by5) return '- Perfect Day'; if (by3) return '- Cool Day'; if (by5) return '- Warm Day'; return ''; }