Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class SmartHomeSystem {
- interface Controllable {
- boolean turnOn();
- boolean turnOff();
- boolean isOn();
- }
- interface Chargeable {
- boolean isCharging();
- boolean startCharging();
- boolean stopCharging();
- }
- public enum Status {
- OFF,
- ON
- }
- // Enum for Light's Brightness Levels
- public enum BrightnessLevel {
- LOW,
- MEDIUM,
- HIGH
- }
- public enum LightColor {
- WHITE,
- YELLOW
- }
- public abstract static class SmartDevice implements Controllable {
- private Status status;
- private int deviceID;
- public SmartDevice(Status status) {
- this.status = status;
- }
- public String displayStatus() {
- return this.getClass().getSimpleName() + " " + deviceID + " is " + status;
- }
- public int getDeviceId() {
- return deviceID;
- }
- public void setDeviceId(int deviceID) {
- this.deviceID = deviceID;
- }
- public Status getStatus() {
- return status;
- }
- public void setStatus(Status status) {
- this.status = status;
- }
- @Override
- public boolean turnOn() {
- if (status == Status.ON) {
- return false;
- }
- setStatus(Status.ON);
- return true;
- }
- @Override
- public boolean turnOff() {
- if (status == Status.OFF) {
- return false;
- }
- setStatus(Status.OFF);
- return true;
- }
- @Override
- public boolean isOn() {
- return status == Status.ON;
- }
- }
- public static class Heater extends SmartDevice {
- private int temperature;
- static final int MAX_HEATER_TEMP = 30;
- static final int MIN_HEATER_TEMP = 15;
- public Heater(Status status, int temperature) {
- super(status);
- this.temperature = temperature;
- }
- public int getTemperature() {
- return temperature;
- }
- public void setTemperature(int temperature) {
- if (isOn() && temperature >= MIN_HEATER_TEMP && temperature <= MAX_HEATER_TEMP) {
- this.temperature = temperature;
- }
- }
- @Override
- public String displayStatus() {
- return super.displayStatus() + " and the temperature is " + getTemperature();
- }
- }
- public static class Camera extends SmartDevice implements Chargeable {
- static final int MAX_CAMERA_ANGLE = 60;
- static final int MIN_CAMERA_ANGLE = -60;
- private boolean charging;
- private boolean recording;
- private int angle;
- public Camera(Status status, boolean charging, boolean recording, int angle) {
- super(status);
- this.charging = charging;
- this.recording = recording;
- this.angle = angle;
- }
- public int getAngle() {
- return angle;
- }
- public void setCameraAngle(int angle) {
- if (isOn() && angle >= MIN_CAMERA_ANGLE && angle <= MAX_CAMERA_ANGLE) {
- this.angle = angle;
- }
- }
- public boolean startRecording() {
- if (isOn() && !recording) {
- recording = true;
- return true;
- }
- return false;
- }
- public boolean stopRecording() {
- if (isOn() && recording) {
- recording = false;
- return true;
- }
- return false;
- }
- public boolean isRecording() {
- return recording;
- }
- @Override
- public boolean isCharging() {
- return charging;
- }
- @Override
- public boolean startCharging() {
- if (charging) {
- return false;
- }
- charging = true;
- return true;
- }
- @Override
- public boolean stopCharging() {
- if (!charging) {
- return false;
- }
- charging = false;
- return true;
- }
- @Override
- public String displayStatus() {
- return super.displayStatus() + ", the angle is " + getAngle() + ", the charging status is "
- + (charging ? "true" : "false") + ", and the recording status is " + (recording ? "true" : "false");
- }
- }
- public static class Light extends SmartDevice implements Chargeable {
- private boolean charging;
- private BrightnessLevel brightnessLevel;
- LightColor lightColor;
- public Light(Status status, boolean charging, BrightnessLevel brightnessLevel, LightColor lightColor) {
- super(status);
- this.charging = charging;
- this.brightnessLevel = brightnessLevel;
- this.lightColor = lightColor;
- }
- public LightColor getLightColor() {
- return lightColor;
- }
- public void setLightColor(LightColor lightColor) {
- if (isOn())
- this.lightColor = lightColor;
- }
- public BrightnessLevel getBrightnessLevel() {
- return brightnessLevel;
- }
- public boolean setBrightnessLevel(BrightnessLevel brightnessLevel) {
- if (isOn()) {
- this.brightnessLevel = brightnessLevel;
- return true;
- }
- return false;
- }
- @Override
- public boolean isCharging() {
- return charging;
- }
- @Override
- public boolean startCharging() {
- if (charging) {
- return false;
- }
- charging = true;
- return true;
- }
- @Override
- public boolean stopCharging() {
- if (!charging) {
- return false;
- }
- charging = false;
- return true;
- }
- @Override
- public String displayStatus() {
- return super.displayStatus() + ", the color is " + lightColor + ", the charging status is "
- + (charging ? "true" : "false")
- + ", and the brightness level is " + brightnessLevel;
- }
- }
- public static boolean isValid(String name, int id) {
- if (name.equals("Light") && (id < 0 || id > 3)) {
- return false;
- }
- if (name.equals("Camera") && (id < 4 || id > 5)) {
- return false;
- }
- if (name.equals("Heater") && (id < 6 || id > 9)) {
- return false;
- }
- return true;
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- List<String> outputMessages = new ArrayList<>();
- SmartDevice[] devices = new SmartDevice[10];
- devices[0] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
- devices[0].deviceID = 0;
- devices[1] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
- devices[1].deviceID = 1;
- devices[2] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
- devices[2].deviceID = 2;
- devices[3] = new Light(Status.ON, false, BrightnessLevel.LOW, LightColor.YELLOW);
- devices[3].deviceID = 3;
- devices[4] = new Camera(Status.ON, false, false, 45);
- devices[4].deviceID = 4;
- devices[5] = new Camera(Status.ON, false, false, 45);
- devices[5].deviceID = 5;
- devices[6] = new Heater(Status.ON, 20);
- devices[6].deviceID = 6;
- devices[7] = new Heater(Status.ON, 20);
- devices[7].deviceID = 7;
- devices[8] = new Heater(Status.ON, 20);
- devices[8].deviceID = 8;
- devices[9] = new Heater(Status.ON, 20);
- devices[9].deviceID = 9;
- while (true) {
- String commandLine = scan.nextLine();
- if (commandLine.equals("end")) {
- break;
- }
- String[] commandParts = commandLine.split(" ");
- if (commandParts.length == 1) {
- if (commandParts[0].equals("DisplayAllStatus")) {
- for (SmartDevice d : devices) {
- outputMessages.add(d.displayStatus());
- }
- continue;
- } else {
- outputMessages.add("Invalid command");
- continue;
- }
- }
- if (commandParts.length < 3) {
- outputMessages.add("Invalid command");
- continue;
- }
- String command = commandParts[0];
- String deviceName = commandParts[1];
- int deviceId = -1;
- try {
- deviceId = Integer.parseInt(commandParts[2]);
- } catch (NumberFormatException e) {
- outputMessages.add("Invalid command");
- continue;
- }
- if (!isValid(deviceName, deviceId)) {
- outputMessages.add("The smart device was not found");
- continue;
- }
- SmartDevice device = devices[deviceId];
- switch (command) {
- case "TurnOn":
- if (device.turnOn()) {
- outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is turned on");
- } else if (device.isOn()) {
- outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is already on");
- } else {
- outputMessages.add("Invalid command");
- }
- break;
- case "TurnOff":
- if (device.turnOff()) {
- outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is turned off");
- } else if (device.isOn()) {
- outputMessages.add(device.getClass().getSimpleName() + " " + deviceId + " is already off");
- } else {
- outputMessages.add("Invalid command");
- }
- break;
- case "StartCharging":
- if (device instanceof Chargeable) {
- Chargeable chargeableDevice = (Chargeable) device;
- if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- if (chargeableDevice.startCharging()) {
- outputMessages.add(deviceName + " " + deviceId + " is charging");
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is already charging");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not chargeable");
- }
- break;
- case "StopCharging":
- if (device instanceof Chargeable) {
- Chargeable chargeableDevice = (Chargeable) device;
- if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- if (chargeableDevice.stopCharging()) {
- outputMessages.add(deviceName + " " + deviceId + " stopped charging");
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not charging");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not chargeable");
- }
- break;
- case "SetColor":
- if (device instanceof Light) {
- Light light = (Light) device;
- if (commandParts.length < 4) {
- outputMessages.add("Invalid command");
- } else if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- try {
- LightColor color = LightColor.valueOf(commandParts[3]);
- light.setLightColor(color);
- outputMessages.add("Light " + deviceId + " color is set to " + color);
- } catch (IllegalArgumentException e) {
- outputMessages.add("The light color can only be \"YELLOW\" or \"WHITE\"");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not a light");
- }
- break;
- case "SetBrightness":
- if (device instanceof Light) {
- Light light = (Light) device;
- if (commandParts.length < 4) {
- outputMessages.add("Invalid command");
- } else if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- try {
- BrightnessLevel brightness = BrightnessLevel.valueOf(commandParts[3]);
- light.setBrightnessLevel(brightness);
- outputMessages.add("Light " + deviceId + " brightness level is set to " + brightness);
- } catch (IllegalArgumentException e) {
- outputMessages.add("The brightness can only be one of \"LOW\", \"MEDIUM\", or \"HIGH\"");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not a light");
- }
- break;
- case "SetCameraAngle":
- if (device instanceof Camera) {
- if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- int angle = Integer.parseInt(commandParts[3]);
- if (angle >= -60 && angle <= 60) {
- ((Camera) device).setCameraAngle(angle);
- outputMessages.add(deviceName + " " + deviceId + " angle is set to " + angle);
- } else {
- outputMessages.add("Camera " + deviceId + " angle should be in the range [-60, 60]");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not a camera");
- }
- break;
- case "StartRecording":
- if (device instanceof Camera) {
- if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- if (((Camera) device).startRecording()) {
- outputMessages.add(deviceName + " " + deviceId + " started recording");
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is already recording");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not a camera");
- }
- break;
- case "StopRecording":
- if (device instanceof Camera) {
- if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- if (((Camera) device).stopRecording()) {
- outputMessages.add(deviceName + " " + deviceId + " stopped recording");
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not recording");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not a camera");
- }
- break;
- case "SetTemperature":
- if (device instanceof Heater) {
- if (!device.isOn()) {
- outputMessages
- .add("You can't change the status of the " + deviceName + " " + deviceId + " while it is off");
- } else {
- int temp = Integer.parseInt(commandParts[3]);
- if (temp >= 15 && temp <= 30) {
- ((Heater) device).setTemperature(temp);
- outputMessages.add(deviceName + " " + deviceId + " temperature is set to " + temp);
- } else {
- outputMessages.add("Heater " + deviceId + " temperature should be in the range [15, 30]");
- }
- }
- } else {
- outputMessages.add(deviceName + " " + deviceId + " is not a heater");
- }
- break;
- default:
- outputMessages.add("Invalid command");
- break;
- }
- }
- for (String message : outputMessages) {
- System.out.println(message);
- }
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement