import 'dart:io'; // --- 1. Base Class for Smart Devices --- abstract class SmartDevice { final String name; bool _isOn; SmartDevice(this.name, {bool isOn = false}) : _isOn = isOn; bool get isOn => _isOn; void turnOn() { _isOn = true; print('$name تم تشغيله.'); } void turnOff() { _isOn = false; print('$name تم إيقاف تشغيله.'); } String get status; } // --- 2. Concrete Device Classes --- // Lighting Device class SmartLight extends SmartDevice { SmartLight() : super('الإضاءة الذكية', isOn: false); @override String get status => _isOn ? 'مُشغلة (ON)' : 'مُطفأة (OFF)'; } // Security Device class SmartSecurity extends SmartDevice { SmartSecurity() : super('نظام الأمان', isOn: false); @override String get status => _isOn ? 'مُفعل (ARMED)' : 'مُعطل (DISARMED)'; } // Thermostat Device with custom logic class SmartThermostat extends SmartDevice { static const int minTemp = 15; static const int maxTemp = 30; int _temperature; SmartThermostat() : _temperature = 20, super('منظم الحرارة', isOn: true); int get temperature => _temperature; set temperature(int newTemp) { if (newTemp >= minTemp && newTemp <= maxTemp) { _temperature = newTemp; print('تم ضبط درجة الحرارة على $newTemp°C.'); } else { print('خطأ: درجة الحرارة يجب أن تكون بين $minTemp و $maxTemp.'); } } // FizzBuzz logic implementation String get fizzBuzzStatus { bool isMultipleOf3 = _temperature % 3 == 0; bool isMultipleOf5 = _temperature % 5 == 0; if (isMultipleOf3 && isMultipleOf5) { return 'Perfect Day'; } else if (isMultipleOf3) { return 'Cool Day'; } else if (isMultipleOf5) { return 'Warm Day'; } return 'Normal Day'; } @override String get status => 'الحرارة: $_temperature°C (${fizzBuzzStatus})'; } // --- 3. Controller Class --- class SmartHomeController { final SmartLight light = SmartLight(); final SmartThermostat thermostat = SmartThermostat(); final SmartSecurity security = SmartSecurity(); void displayMenu() { print('\nاختر خيارك:'); print('1. تشغيل/إيقاف الإضاءة (${light.status})'); print('2. ضبط درجة الحرارة (${thermostat.temperature}°C)'); print('3. تفعيل/تعطيل الأمان (${security.status})'); print('4. عرض حالة الأجهزة'); print('5. خروج'); stdout.write('أدخل رقم الخيار: '); } void toggleLight() { light.isOn ? light.turnOff() : light.turnOn(); } void toggleSecurity() { security.isOn ? security.turnOff() : security.turnOn(); } void setTemperature() { stdout.write('أدخل درجة الحرارة الجديدة (${SmartThermostat.minTemp} - ${SmartThermostat.maxTemp}): '); String? input = stdin.readLineSync(); try { int newTemp = int.parse(input ?? ''); thermostat.temperature = newTemp; } catch (e) { print('خطأ: إدخال غير صالح. يرجى إدخال رقم صحيح.'); } } void displayStatus() { print('\n--- حالة الأجهزة الحالية ---'); print('الإضاءة: ${light.status}'); print('منظم الحرارة: ${thermostat.status}'); print('نظام الأمان: ${security.status}'); } } // --- 4. Main Function --- void main() { final controller = SmartHomeController(); print('مرحباً بك في نظام التحكم بالمنزل الذكي (Smart Home Automation System)'); while (true) { controller.displayMenu(); String? choice = stdin.readLineSync(); if (choice == null) { print('إدخال غير صالح. يرجى المحاولة مرة أخرى.'); continue; } switch (choice.trim()) { case '1': controller.toggleLight(); break; case '2': controller.setTemperature(); break; case '3': controller.toggleSecurity(); break; case '4': controller.displayStatus(); break; case '5': print('شكراً لاستخدامك النظام. إلى اللقاء!'); return; default: print('خيار غير صحيح. يرجى اختيار رقم من 1 إلى 5.'); } print('----------------------------------------'); } }