import 'dart:io'; /// Encapsulates the state and logic for the smart home devices. class SmartHomeSystem { // 2. Device Status: Initialize each device with default values. bool lightsOn = false; // Lights (can be on or off) int thermostatTemp = 20; // Thermostat (default 20°C) bool securityArmed = false; // Security System (can be armed or disarmed) /// The minimum and maximum allowable temperature for the thermostat. static const int minTemp = 15; static const int maxTemp = 30; // --- Core Device Control Methods --- /// Toggles the light status. String toggleLights() { lightsOn = !lightsOn; return lightsOn ? "Lights **turned ON**." : "Lights **turned OFF**."; } /// Arms the security system. String armSecurity() { if (!securityArmed) { securityArmed = true; return "Security System **ARMED**."; } return "Security System is already armed."; } /// Disarms the security system. String disarmSecurity() { if (securityArmed) { securityArmed = false; return "Security System **DISARMED**."; } return "Security System is already disarmed."; } /// Sets the thermostat temperature with validation. String setTemperature(String input) { try { final int newTemp = int.parse(input.trim()); // 4. Input Validation: Temperature range check. if (newTemp < minTemp || newTemp > maxTemp) { return "⚠️ Error: Temperature must be between $minTemp°C and $maxTemp°C. Please try again."; } thermostatTemp = newTemp; return "Thermostat set to **$thermostatTemp°C** successfully."; } on FormatException { return "⚠️ Error: Invalid input. Please enter a valid number for the temperature."; } } // --- Status Display Method --- /// Displays the current status of all devices and applies FizzBuzz-like logic. String getSystemStatus() { // 6. String Interpolation and Operations: final lightStatus = lightsOn ? "ON" : "OFF"; final securityStatus = securityArmed ? "ARMED" : "DISARMED"; // 5. Control Flow: FizzBuzz-like exercise String tempMessage; final isMultipleOf3 = thermostatTemp % 3 == 0; final isMultipleOf5 = thermostatTemp % 5 == 0; if (isMultipleOf3 && isMultipleOf5) { tempMessage = "Perfect Day! (Multiple of 3 and 5)"; } else if (isMultipleOf3) { tempMessage = "Cool Day! (Multiple of 3)"; } else if (isMultipleOf5) { tempMessage = "Warm Day! (Multiple of 5)"; } else { tempMessage = "Standard Temperature."; } return """ --- Current System Status --- 💡 Lights: **$lightStatus** 🌡️ Thermostat: **$thermostatTemp°C** ($tempMessage) 🔒 Security System: **$securityStatus** --------------------------- """; } } // --- Main Application Logic --- // 1. Main Method: The program starts here. void main() { final SmartHomeSystem system = SmartHomeSystem(); bool running = true; print("Welcome to the **Dart Smart Home CLI**!"); print("Use 'status' to view devices or 'help' for options."); // 3. User Interaction: Use a loop for continuous interaction. while (running) { print("\n--------------------------"); stdout.write("Enter command (e.g., lights on, temp 25, exit): "); final String? input = stdin.readLineSync(); if (input == null || input.trim().isEmpty) { continue; } // Process the input command final List parts = input.trim().toLowerCase().split(' '); final String primaryCommand = parts[0]; final String? secondaryCommand = parts.length > 1 ? parts[1] : null; // 5. Control Flow: Use if/else statements to handle user choices (main menu) if (primaryCommand == 'exit') { running = false; print("👋 Shutting down Smart Home system. Goodbye!"); break; } else if (primaryCommand == 'status') { print(system.getSystemStatus()); } else if (primaryCommand == 'help') { _displayHelp(); } else { // 5. Control Flow: Use a switch statement to handle device commands switch (primaryCommand) { case 'lights': if (secondaryCommand == 'on' || secondaryCommand == 'off' || secondaryCommand == 'toggle') { print(system.toggleLights()); } else { print("⚠️ Invalid lights command. Try: lights on/off or lights toggle."); } break; case 'temp': case 'thermostat': if (secondaryCommand != null) { print(system.setTemperature(secondaryCommand)); } else { print("⚠️ Missing temperature value. Try: temp 25 (range ${SmartHomeSystem.minTemp}-${SmartHomeSystem.maxTemp}°C)."); } break; case 'security': if (secondaryCommand == 'arm') { print(system.armSecurity()); } else if (secondaryCommand == 'disarm') { print(system.disarmSecurity()); } else { print("⚠️ Invalid security command. Try: security arm or security disarm."); } break; default: print("Command **'$primaryCommand'** not recognized. Type 'help' for options."); break; } } } } /// Helper function to display command options. void _displayHelp() { print(""" --- Available Commands --- * **lights [on/off/toggle]**: Control the house lights. * **temp [15-30]**: Set the thermostat temperature. * **security [arm/disarm]**: Control the security system. * **status**: View the current state of all devices. * **exit**: Close the Smart Home application. -------------------------- """); } lgt