Advertisement
UsSe3wa

Untitled

Nov 16th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.63 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class SmartHomeSystem {
  6.  
  7.   interface Controllable {
  8.     boolean turnOn();
  9.  
  10.     boolean turnOff();
  11.  
  12.     boolean isOn();
  13.   }
  14.  
  15.   interface Chargeable {
  16.     boolean isCharging();
  17.  
  18.     boolean startCharging();
  19.  
  20.     boolean stopCharging();
  21.   }
  22.  
  23.   public enum Status {
  24.     OFF,
  25.     ON
  26.   }
  27.  
  28.   // Enum for Light's Brightness Levels
  29.  public enum BrightnessLevel {
  30.    LOW,
  31.    MEDIUM,
  32.    HIGH
  33.  }
  34.  
  35.  public enum LightColor {
  36.    WHITE,
  37.    YELLOW
  38.  }
  39.  
  40.  public abstract static class SmartDevice implements Controllable {
  41.    private Status status;
  42.    private int deviceID;
  43.  
  44.    public SmartDevice(Status status) {
  45.      this.status = status;
  46.    }
  47.  
  48.    public String displayStatus() {
  49.      return this.getClass().getSimpleName() + " " + deviceID + " is " + status;
  50.    }
  51.  
  52.    public int getDeviceId() {
  53.      return deviceID;
  54.    }
  55.  
  56.    public void setDeviceId(int deviceID) {
  57.      this.deviceID = deviceID;
  58.    }
  59.  
  60.    public Status getStatus() {
  61.      return status;
  62.    }
  63.  
  64.    public void setStatus(Status status) {
  65.      this.status = status;
  66.    }
  67.  
  68.    @Override
  69.    public boolean turnOn() {
  70.      if (status == Status.ON) {
  71.        return false;
  72.      }
  73.      setStatus(Status.ON);
  74.      return true;
  75.    }
  76.  
  77.    @Override
  78.    public boolean turnOff() {
  79.      if (status == Status.OFF) {
  80.        return false;
  81.      }
  82.      setStatus(Status.OFF);
  83.      return true;
  84.    }
  85.  
  86.    @Override
  87.    public boolean isOn() {
  88.      return status == Status.ON;
  89.    }
  90.  }
  91.  
  92.  public static class Heater extends SmartDevice {
  93.    private int temperature;
  94.    static final int MAX_HEATER_TEMP = 30;
  95.    static final int MIN_HEATER_TEMP = 15;
  96.  
  97.    public Heater(Status status, int temperature) {
  98.      super(status);
  99.      this.temperature = temperature;
  100.    }
  101.  
  102.    public int getTemperature() {
  103.      return temperature;
  104.    }
  105.  
  106.    public void setTemperature(int temperature) {
  107.      if (isOn() && temperature >= MIN_HEATER_TEMP && temperature <= MAX_HEATER_TEMP) {
  108.        this.temperature = temperature;
  109.      }
  110.    }
  111.  
  112.    @Override
  113.    public String displayStatus() {
  114.      return super.displayStatus() + " and the temperature is " + getTemperature();
  115.    }
  116.  }
  117.  
  118.  public static class Camera extends SmartDevice implements Chargeable {
  119.    static final int MAX_CAMERA_ANGLE = 60;
  120.    static final int MIN_CAMERA_ANGLE = -60;
  121.  
  122.    private boolean charging;
  123.    private boolean recording;
  124.    private int angle;
  125.  
  126.    public Camera(Status status, boolean charging, boolean recording, int angle) {
  127.      super(status);
  128.      this.charging = charging;
  129.      this.recording = recording;
  130.      this.angle = angle;
  131.    }
  132.  
  133.    public int getAngle() {
  134.      return angle;
  135.    }
  136.  
  137.    public void setCameraAngle(int angle) {
  138.      if (isOn() && angle >= MIN_CAMERA_ANGLE && angle <= MAX_CAMERA_ANGLE) {
  139.        this.angle = angle;
  140.      }
  141.    }
  142.  
  143.    public boolean startRecording() {
  144.      if (isOn() && !recording) {
  145.        recording = true;
  146.        return true;
  147.      }
  148.      return false;
  149.    }
  150.  
  151.    public boolean stopRecording() {
  152.      if (isOn() && recording) {
  153.        recording = false;
  154.        return true;
  155.      }
  156.      return false;
  157.    }
  158.  
  159.    public boolean isRecording() {
  160.      return recording;
  161.    }
  162.  
  163.    @Override
  164.    public boolean isCharging() {
  165.      return charging;
  166.    }
  167.  
  168.    @Override
  169.    public boolean startCharging() {
  170.      if (charging) {
  171.        return false;
  172.      }
  173.      charging = true;
  174.      return true;
  175.    }
  176.  
  177.    @Override
  178.    public boolean stopCharging() {
  179.      if (!charging) {
  180.        return false;
  181.      }
  182.      charging = false;
  183.      return true;
  184.    }
  185.  
  186.    @Override
  187.    public String displayStatus() {
  188.      return super.displayStatus() + ", the angle is " + getAngle() + ", the charging status is "
  189.          + (charging ? "true" : "false") + ", and the recording status is " + (recording ? "true" : "false");
  190.    }
  191.  }
  192.  
  193.  public static class Light extends SmartDevice implements Chargeable {
  194.    private boolean charging;
  195.    private BrightnessLevel brightnessLevel;
  196.    LightColor lightColor;
  197.  
  198.    public Light(Status status, boolean charging, BrightnessLevel brightnessLevel, LightColor lightColor) {
  199.      super(status);
  200.      this.charging = charging;
  201.      this.brightnessLevel = brightnessLevel;
  202.      this.lightColor = lightColor;
  203.    }
  204.  
  205.    public LightColor getLightColor() {
  206.      return lightColor;
  207.    }
  208.  
  209.    public void setLightColor(LightColor lightColor) {
  210.      if (isOn())
  211.        this.lightColor = lightColor;
  212.    }
  213.  
  214.    public BrightnessLevel getBrightnessLevel() {
  215.      return brightnessLevel;
  216.    }
  217.  
  218.    public boolean setBrightnessLevel(BrightnessLevel brightnessLevel) {
  219.      if (isOn()) {
  220.        this.brightnessLevel = brightnessLevel;
  221.        return true;
  222.      }
  223.      return false;
  224.    }
  225.  
  226.    @Override
  227.    public boolean isCharging() {
  228.      return charging;
  229.    }
  230.  
  231.    @Override
  232.    public boolean startCharging() {
  233.      if (charging) {
  234.        return false;
  235.      }
  236.      charging = true;
  237.      return true;
  238.    }
  239.  
  240.    @Override
  241.    public boolean stopCharging() {
  242.      if (!charging) {
  243.        return false;
  244.      }
  245.      charging = false;
  246.      return true;
  247.    }
  248.  
  249.    @Override
  250.    public String displayStatus() {
  251.      return super.displayStatus() + ", the color is " + lightColor + ", the charging status is "
  252.          + (charging ? "true" : "false")
  253.          + ", and the brightness level is " + brightnessLevel;
  254.    }
  255.  }
  256.  
  257.  public static boolean isValid(String name, int id) {
  258.    if (name.equals("Light") && (id < 0 || id > 3)) {
  259.      return false;
  260.    }
  261.    if (name.equals("Camera") && (id < 4 || id > 5)) {
  262.      return false;
  263.    }
  264.    if (name.equals("Heater") && (id < 6 || id > 9)) {
  265.      return false;
  266.    }
  267.    return true;
  268.  }
  269.  
  270.  public static void main(String[] args) {
  271.    Scanner scan = new Scanner(System.in);
  272.    List<String> outputMessages = new ArrayList<>();
  273.  
  274.    SmartDevice[] devices = new SmartDevice[10];
  275.    devices[0] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
  276.    devices[0].deviceID = 0;
  277.    devices[1] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
  278.    devices[1].deviceID = 1;
  279.    devices[2] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
  280.    devices[2].deviceID = 2;
  281.    devices[3] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
  282.    devices[3].deviceID = 3;
  283.    devices[4] = new Camera(Status.ON, false, false, 45);
  284.    devices[4].deviceID = 4;
  285.    devices[5] = new Camera(Status.ON, false, false, 45);
  286.    devices[5].deviceID = 5;
  287.    devices[6] = new Heater(Status.ON, 20);
  288.    devices[6].deviceID = 6;
  289.    devices[7] = new Heater(Status.ON, 20);
  290.    devices[7].deviceID = 7;
  291.    devices[8] = new Heater(Status.ON, 20);
  292.    devices[8].deviceID = 8;
  293.    devices[9] = new Heater(Status.ON, 20);
  294.    devices[9].deviceID = 9;
  295.  
  296.    while (true) {
  297.      String commandLine = scan.nextLine();
  298.      if (commandLine.equals("end")) {
  299.        break;
  300.      }
  301.  
  302.      String[] commandParts = commandLine.split(" ");
  303.      if (commandParts.length == 1) {
  304.        if (commandParts[0].equals("DisplayAllStatus")) {
  305.          for (SmartDevice d : devices) {
  306.            outputMessages.add(d.displayStatus());
  307.          }
  308.          continue;
  309.        } else {
  310.          outputMessages.add("Invalid command");
  311.          continue;
  312.        }
  313.      }
  314.  
  315.      if (commandParts.length < 3) {
  316.        outputMessages.add("Invalid command");
  317.        continue;
  318.      }
  319.  
  320.      String command = commandParts[0];
  321.      String deviceName = commandParts[1];
  322.      int deviceId = -1;
  323.  
  324.      try {
  325.        deviceId = Integer.parseInt(commandParts[2]);
  326.      } catch (NumberFormatException e) {
  327.        outputMessages.add("Invalid command");
  328.        continue;
  329.      }
  330.  
  331.      if (!isValid(deviceName, deviceId)) {
  332.        outputMessages.add("The smart device was not found");
  333.        continue;
  334.      }
  335.  
  336.      SmartDevice device = devices[deviceId];
  337.  
  338.      switch (command) {
  339.        case "TurnOn":
  340.          if (device.turnOn()) {
  341.            outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is turned on");
  342.          } else if (device.isOn()) {
  343.            outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is already on");
  344.          } else {
  345.            outputMessages.add("Invalid command");
  346.          }
  347.          break;
  348.  
  349.        case "TurnOff":
  350.          if (device.turnOff()) {
  351.            outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is turned off");
  352.          } else if (device.isOn()) {
  353.            outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is already off");
  354.          } else {
  355.            outputMessages.add("Invalid command");
  356.          }
  357.          break;
  358.  
  359.        case "StartCharging":
  360.          if (device instanceof Chargeable) {
  361.            Chargeable chargeableDevice = (Chargeable) device;
  362.            if (!device.isOn()) {
  363.              outputMessages
  364.                  .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  365.            } else {
  366.              if (chargeableDevice.startCharging()) {
  367.                outputMessages.add(deviceName + " " + deviceId + " is charging");
  368.              } else {
  369.                outputMessages.add(deviceName + " " + deviceId + " is already charging");
  370.              }
  371.            }
  372.          } else {
  373.            outputMessages.add(deviceName + " " + deviceId + " is not chargeable");
  374.          }
  375.          break;
  376.  
  377.        case "StopCharging":
  378.          if (device instanceof Chargeable) {
  379.            Chargeable chargeableDevice = (Chargeable) device;
  380.            if (!device.isOn()) {
  381.              outputMessages
  382.                  .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  383.            } else {
  384.              if (chargeableDevice.stopCharging()) {
  385.                outputMessages.add(deviceName + " " + deviceId + " stopped charging");
  386.              } else {
  387.                outputMessages.add(deviceName + " " + deviceId + " is not charging");
  388.              }
  389.            }
  390.          } else {
  391.            outputMessages.add(deviceName + " " + deviceId + " is not chargeable");
  392.          }
  393.          break;
  394.        case "SetColor":
  395.          if (device instanceof Light) {
  396.            Light light = (Light) device;
  397.            if (commandParts.length < 4) {
  398.              outputMessages.add("Invalid command");
  399.            } else if (!device.isOn()) {
  400.              outputMessages
  401.                  .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  402.            } else {
  403.              try {
  404.                LightColor color = LightColor.valueOf(commandParts[3]);
  405.                light.setLightColor(color);
  406.                outputMessages.add("Light " + deviceId + " color is set to " + color);
  407.              } catch (IllegalArgumentException e) {
  408.                outputMessages.add("The light color can only be \"YELLOW\" or \"WHITE\"");
  409.               }
  410.             }
  411.           } else {
  412.             outputMessages.add(deviceName + " " + deviceId + " is not a light");
  413.           }
  414.           break;
  415.  
  416.         case "SetBrightness":
  417.           if (device instanceof Light) {
  418.             Light light = (Light) device;
  419.             if (commandParts.length < 4) {
  420.               outputMessages.add("Invalid command");
  421.             } else if (!device.isOn()) {
  422.               outputMessages
  423.                   .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  424.             } else {
  425.               try {
  426.                 BrightnessLevel brightness = BrightnessLevel.valueOf(commandParts[3]);
  427.                 light.setBrightnessLevel(brightness);
  428.                 outputMessages.add("Light " + deviceId + " brightness level is set to " + brightness);
  429.               } catch (IllegalArgumentException e) {
  430.                 outputMessages.add("The brightness can only be one of \"LOW\", \"MEDIUM\", or \"HIGH\"");
  431.               }
  432.             }
  433.           } else {
  434.             outputMessages.add(deviceName + " " + deviceId + " is not a light");
  435.           }
  436.           break;
  437.  
  438.         case "SetCameraAngle":
  439.           if (device instanceof Camera) {
  440.             if (!device.isOn()) {
  441.               outputMessages
  442.                   .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  443.             } else {
  444.               int angle = Integer.parseInt(commandParts[3]);
  445.               if (angle >= -60 && angle <= 60) {
  446.                 ((Camera) device).setCameraAngle(angle);
  447.                 outputMessages.add(deviceName + " " + deviceId + " angle is set to " + angle);
  448.               } else {
  449.                 outputMessages.add("Camera " + deviceId + " angle should be in the range [-60, 60]");
  450.               }
  451.             }
  452.           } else {
  453.             outputMessages.add(deviceName + " " + deviceId + " is not a camera");
  454.           }
  455.           break;
  456.  
  457.         case "StartRecording":
  458.           if (device instanceof Camera) {
  459.             if (!device.isOn()) {
  460.               outputMessages
  461.                   .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  462.             } else {
  463.               if (((Camera) device).startRecording()) {
  464.                 outputMessages.add(deviceName + " " + deviceId + " started recording");
  465.               } else {
  466.                 outputMessages.add(deviceName + " " + deviceId + " is already recording");
  467.               }
  468.             }
  469.           } else {
  470.             outputMessages.add(deviceName + " " + deviceId + " is not a camera");
  471.           }
  472.           break;
  473.  
  474.         case "StopRecording":
  475.           if (device instanceof Camera) {
  476.             if (!device.isOn()) {
  477.               outputMessages
  478.                   .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  479.             } else {
  480.               if (((Camera) device).stopRecording()) {
  481.                 outputMessages.add(deviceName + " " + deviceId + " stopped recording");
  482.               } else {
  483.                 outputMessages.add(deviceName + " " + deviceId + " is not recording");
  484.               }
  485.             }
  486.           } else {
  487.             outputMessages.add(deviceName + " " + deviceId + " is not a camera");
  488.           }
  489.           break;
  490.  
  491.         case "SetTemperature":
  492.           if (device instanceof Heater) {
  493.             if (!device.isOn()) {
  494.               outputMessages
  495.                   .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
  496.             } else {
  497.               int temp = Integer.parseInt(commandParts[3]);
  498.               if (temp >= 15 && temp <= 30) {
  499.                 ((Heater) device).setTemperature(temp);
  500.                 outputMessages.add(deviceName + " " + deviceId + " temperature is set to " + temp);
  501.               } else {
  502.                 outputMessages.add("Heater " + deviceId + " temperature should be in the range [15, 30]");
  503.               }
  504.             }
  505.           } else {
  506.             outputMessages.add(deviceName + " " + deviceId + " is not a heater");
  507.           }
  508.           break;
  509.  
  510.         default:
  511.           outputMessages.add("Invalid command");
  512.           break;
  513.       }
  514.     }
  515.  
  516.     for (String message : outputMessages) {
  517.       System.out.println(message);
  518.     }
  519.     scan.close();
  520.   }
  521. }
  522.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement