Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------- Device.java ---------------------------
- package cls;
- import java.util.Objects;
- public class Device {
- private String elementName;
- private String elementType;
- private boolean isActive;
- public Device(String elementName, String elementType) {
- this.elementName = elementName;
- this.elementType = elementType;
- this.isActive = false;
- }
- public String getDeviceName() {
- return elementName;
- }
- public String getDeviceType() {
- return elementType;
- }
- private void setActive(boolean active) {
- isActive = active;
- }
- public void activate() {
- setActive(true);
- }
- public void deActivate() {
- setActive(false);
- }
- @Override
- public String toString() {
- String s = isActive ? "On" : "Off";
- return elementName + " is of type " + elementType + " and it is " + s;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Device element = (Device) o;
- return Objects.equals(elementName, element.elementName) &&
- Objects.equals(elementType, element.elementType);
- }
- @Override
- public int hashCode() {
- return Objects.hash(elementName, elementType);
- }
- }
- // -------------------- Room.java ---------------------------------
- package cls;
- import java.util.LinkedHashSet;
- import java.util.Objects;
- public class Room {
- private String roomName;
- private boolean isActive;
- private LinkedHashSet<Device> elementsInRoom;
- public Room(String roomName, boolean isActive, LinkedHashSet elementsInRoom) {
- this.roomName = roomName;
- this.isActive = isActive;
- this.elementsInRoom = elementsInRoom;
- }
- public Room(String roomName, boolean isActive) {
- this(roomName, true, new LinkedHashSet<Device>());
- }
- public Room(String roomName) {
- this(roomName, true);
- }
- public String getRoomName() {
- return roomName;
- }
- public boolean addElement(Device el) {
- if(elementsInRoom.add(el)) {
- System.out.println("Element: " + el.getDeviceName() + " of type " + el.getDeviceType() + " was added.");
- return true;
- }
- System.out.println("Element: " + el.getDeviceName() + " of type " + el.getDeviceType() + " already exists.");
- return false;
- }
- public void deActivateElement(String elementName) {
- for(Device e : elementsInRoom) {
- if(e.getDeviceName().equals(elementName)) {
- e.deActivate();
- }
- }
- }
- public void activateElement(String elementName) {
- for(Device e : elementsInRoom) {
- if(e.getDeviceName().equals(elementName)) {
- e.activate();
- }
- }
- }
- public void deActivateAll() {
- for(Device e : elementsInRoom) {
- e.deActivate();
- }
- }
- public void activateAll() {
- for(Device e : elementsInRoom) {
- e.activate();
- }
- }
- @Override
- public String toString() {
- String s = "Room " + roomName + " is ";
- s += isActive ? "active\n" : "not active\n" + roomName + " contains the following devices:";
- for(Device e : elementsInRoom) {
- s += e.toString();
- s += "\n";
- }
- return s;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Room room = (Room) o;
- return Objects.equals(roomName, room.roomName);
- }
- @Override
- public int hashCode() {
- return Objects.hash(roomName);
- }
- }
- // -------------------------------- House.java ------------------------------------
- package cls;
- import java.util.LinkedHashSet;
- /**
- * House class represents a simple house with rooms
- */
- public class House {
- /**
- * rooms - A LinkedHashSet used to store the rooms in the house
- */
- private LinkedHashSet<Room> rooms;
- /**
- * @Constructor used to create an empty house.
- */
- public House() {
- this.rooms = new LinkedHashSet<Room>();
- }
- /**
- * A method for adding a new room to the house
- * @param room - Represents the room that will be added to the house
- * @return true if the room was added to the house successfully.
- * If another room with the same name exists the room will not be added to the house.
- * The check is done using the equal and hashCode methods implemented in the Room class.
- */
- public boolean addRoom(Room room) {
- if(rooms.add(room)) {
- System.out.println("Room " + room.getRoomName() + " was added.");
- return true;
- }
- System.out.println("Room " + room.getRoomName() + " already exists.");
- return false;
- }
- /**
- * A method used to get the desired room according to the room's name.
- * @param rName - Represents the room's name.
- * @return the room object on success, or null on failure.
- */
- public Room getRoom(String rName) {
- for(Room r : rooms) {
- if(r.getRoomName().equals(rName)) {
- return r;
- }
- }
- return null;
- }
- /**
- * A method for indicating if the house contains the specified room
- * @param rName - Represents the room's name.
- * @return true if such room exists in the house, or false otherwise
- */
- public boolean containsRoom(String rName) {
- for(Room r : rooms) {
- if(r.getRoomName().equals(rName)) {
- return true;
- }
- }
- return false;
- }
- /**
- * A method for activating all the devices in the specified room.
- * @param rName - Represents the room's name.
- */
- public void activateAllInARoom(String rName) {
- getRoom(rName).activateAll();
- System.out.println("All elements in " + rName + " were activated.");
- }
- /**
- * A method for deactivating all the devices in the specified room.
- * @param rName - Represents the room's name.
- */
- public void deActivateAllInARoom(String rName) {
- getRoom(rName).deActivateAll();
- System.out.println("All elements in " + rName + " were deactivated.");
- }
- /**
- * A method for deactivating a specific device in the specified room.
- * @param rName - Represents the room's name.
- * @param elName - Represents the device's name.
- */
- public void deActivateElementInARoom(String rName, String elName) {
- getRoom(rName).deActivateElement(elName);
- }
- /**
- * A method for activating a specific device in the specified room.
- * @param rName - Represents the room's name.
- * @param elName - Represents the device's name.
- */
- public void activateElementInARoom(String rName, String elName) {
- getRoom(rName).activateElement(elName);
- }
- /**
- * A method for adding a given device in the specified room.
- * @param rName - Represents the room's name.
- * @param el - Represents the device object.
- */
- public void addElementInRoom(String rName, Device el) {
- if(containsRoom(rName)) {
- getRoom(rName).addElement(el);
- } else {
- System.out.println("Room " + rName + " does not exist!");
- }
- }
- /**
- * A method that checks if the house has any rooms in it.
- * @return true if the house is empty, false otherwise.
- */
- public boolean isEmpty() {
- return rooms.isEmpty();
- }
- /**
- * A utility method for printing the house's details along with the details on each room and device.
- * @return A string with the house description.
- */
- @Override
- public String toString() {
- if(!isEmpty()) {
- String s = "The house contains the following rooms and elements:\n";
- for(Room r : rooms) {
- s += "\n"+r.toString();
- }
- return s;
- }
- return "The house has no rooms!";
- }
- }
- // -------------------------------------- Main.java -------------------------------
- package Tester;
- import cls.*;
- import java.util.Scanner;
- public class Main {
- /**
- * Scanner sc is declared outside the main method to let other methods use it.
- */
- private static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- House house = new House();
- String rName = "", elName, elType;
- int choice = -1;
- String title = "====================================\n";
- title += "= Welcome to the house application =\n====================================";
- String mainMenu = "\n\nPlease choose from the following options - \n";
- mainMenu += "1 - add a room.\n2 - Add an element in a room.\n3 - Activate all devices in a room.";
- mainMenu += "\n4 - Deactivate all elements in a room.\n5 - Activate an element in a room.";
- mainMenu += "\n6 - Deactivate an element in a room.\n7 - Print house details.\n0 - Exit the program.\n";
- //flag is used to terminate the execution when the user chooses '0' from the menu.
- //flag2 is used to terminate the execution when the user chooses 'E' or 'e' on each action.
- boolean flag = true, flag2 = false;
- do {
- if(!flag2) {
- //Printing the menu on each iteration until the program is terminated
- System.out.println("\n\n\n\n\n" + title + mainMenu);
- choice = sc.nextInt(); // User's choice from the main menu
- } else {
- choice = 0;
- }
- sc.nextLine(); //Clear the buffer
- switch (choice) {
- case 0:
- flag = false;
- break;
- case 1:
- System.out.println("Please enter the room's name: ");
- rName = sc.nextLine();
- house.addRoom(new Room(rName));
- flag2 = contProc();
- break;
- case 2:
- if(house.isEmpty()) {
- System.out.println("The house has no rooms!");
- } else {
- boolean isRunning = true;
- do {
- System.out.println("Please enter the room's name: ");
- rName = sc.nextLine();
- if (house.containsRoom(rName)) {
- isRunning = false;
- } else {
- System.out.println("No such room!!!");
- }
- } while (isRunning);
- System.out.println("Please enter the element's type: ");
- elType = sc.nextLine();
- System.out.println("Please enter the element's name: ");
- elName = sc.nextLine();
- house.addElementInRoom(rName, new Device(elName, elType));
- }
- flag2 = contProc();
- break;
- case 3:
- if(house.isEmpty()) {
- System.out.println("The house has no rooms!");
- } else {
- System.out.println("Please enter the room's name: ");
- rName = sc.nextLine();
- house.activateAllInARoom(rName);
- }
- flag2 = contProc();
- break;
- case 4:
- if(house.isEmpty()) {
- System.out.println("The house has no rooms!");
- } else {
- System.out.println("Please enter the room's name: ");
- rName = sc.nextLine();
- house.deActivateAllInARoom(rName);
- }
- flag2 = contProc();
- break;
- case 5:
- if(house.isEmpty()) {
- System.out.println("The house has no rooms!");
- } else {
- System.out.println("Please enter the room's name: ");
- rName = sc.nextLine();
- System.out.println("Please enter the element's name: ");
- elName = sc.nextLine();
- house.activateElementInARoom(rName, elName);
- }
- flag2 = contProc();
- break;
- case 6:
- if(house.isEmpty()) {
- System.out.println("The house has no rooms!");
- } else {
- System.out.println("Please enter the room's name: ");
- rName = sc.nextLine();
- System.out.println("Please enter the element's name: ");
- elName = sc.nextLine();
- house.deActivateElementInARoom(rName, elName);
- }
- flag2 = contProc();
- break;
- case 7:
- System.out.println(house.toString());
- flag2 = contProc();
- break;
- default:
- System.out.println("Default");
- break;
- }
- } while (flag);
- }
- /**
- * A utility method to check if the user wants to continue or exit the program.
- * This method will be executed on each iteration/user choice.
- * @return true if the user wishes to terminate the program.
- * @throws StringIndexOutOfBoundsException if the user presses the 'Enter' key without any input
- */
- private static boolean contProc() {
- System.out.println("\nPress 'E' to exit, or any other key to continue:");
- try {
- return (sc.nextLine().toLowerCase() == "e") ? true : false;
- } catch(StringIndexOutOfBoundsException sioobe) {}
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement