Advertisement
eranseg

House Project

Sep 25th, 2019
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.35 KB | None | 0 0
  1. // ------------------------- Device.java ---------------------------
  2.  
  3. package cls;
  4.  
  5. import java.util.Objects;
  6.  
  7. public class Device {
  8.  
  9.     private String elementName;
  10.     private String elementType;
  11.     private boolean isActive;
  12.  
  13.     public Device(String elementName, String elementType) {
  14.         this.elementName = elementName;
  15.         this.elementType = elementType;
  16.         this.isActive = false;
  17.     }
  18.  
  19.     public String getDeviceName() {
  20.         return elementName;
  21.     }
  22.  
  23.     public String getDeviceType() {
  24.         return elementType;
  25.     }
  26.  
  27.     private void setActive(boolean active) {
  28.         isActive = active;
  29.     }
  30.  
  31.     public void activate() {
  32.         setActive(true);
  33.     }
  34.  
  35.     public void deActivate() {
  36.         setActive(false);
  37.     }
  38.  
  39.     @Override
  40.     public String toString() {
  41.         String s = isActive ? "On" : "Off";
  42.         return elementName + " is of type " + elementType + " and it is " + s;
  43.     }
  44.  
  45.     @Override
  46.     public boolean equals(Object o) {
  47.         if (this == o) return true;
  48.         if (o == null || getClass() != o.getClass()) return false;
  49.         Device element = (Device) o;
  50.         return Objects.equals(elementName, element.elementName) &&
  51.                 Objects.equals(elementType, element.elementType);
  52.     }
  53.  
  54.     @Override
  55.     public int hashCode() {
  56.  
  57.         return Objects.hash(elementName, elementType);
  58.     }
  59. }
  60.  
  61. // -------------------- Room.java ---------------------------------
  62.  
  63. package cls;
  64.  
  65. import java.util.LinkedHashSet;
  66. import java.util.Objects;
  67.  
  68. public class Room {
  69.  
  70.     private String roomName;
  71.     private boolean isActive;
  72.     private LinkedHashSet<Device> elementsInRoom;
  73.  
  74.     public Room(String roomName, boolean isActive, LinkedHashSet elementsInRoom) {
  75.         this.roomName = roomName;
  76.         this.isActive = isActive;
  77.         this.elementsInRoom = elementsInRoom;
  78.     }
  79.  
  80.     public Room(String roomName, boolean isActive) {
  81.         this(roomName, true, new LinkedHashSet<Device>());
  82.     }
  83.  
  84.     public Room(String roomName) {
  85.         this(roomName, true);
  86.     }
  87.  
  88.     public String getRoomName() {
  89.         return roomName;
  90.     }
  91.  
  92.     public boolean addElement(Device el) {
  93.         if(elementsInRoom.add(el)) {
  94.             System.out.println("Element: " + el.getDeviceName() + " of type " + el.getDeviceType() + " was added.");
  95.             return true;
  96.         }
  97.         System.out.println("Element: " + el.getDeviceName() + " of type " + el.getDeviceType() + " already exists.");
  98.         return false;
  99.     }
  100.  
  101.     public void deActivateElement(String elementName) {
  102.         for(Device e : elementsInRoom) {
  103.             if(e.getDeviceName().equals(elementName)) {
  104.                 e.deActivate();
  105.             }
  106.         }
  107.     }
  108.  
  109.     public void activateElement(String elementName) {
  110.         for(Device e : elementsInRoom) {
  111.             if(e.getDeviceName().equals(elementName)) {
  112.                 e.activate();
  113.             }
  114.         }
  115.     }
  116.  
  117.     public void deActivateAll() {
  118.         for(Device e : elementsInRoom) {
  119.             e.deActivate();
  120.         }
  121.     }
  122.  
  123.     public void activateAll() {
  124.         for(Device e : elementsInRoom) {
  125.             e.activate();
  126.         }
  127.     }
  128.  
  129.     @Override
  130.     public String toString() {
  131.         String s = "Room " + roomName  + " is ";
  132.         s += isActive ? "active\n" : "not active\n" + roomName + " contains the following devices:";
  133.         for(Device e : elementsInRoom) {
  134.             s += e.toString();
  135.             s += "\n";
  136.         }
  137.         return s;
  138.     }
  139.  
  140.     @Override
  141.     public boolean equals(Object o) {
  142.         if (this == o) return true;
  143.         if (o == null || getClass() != o.getClass()) return false;
  144.         Room room = (Room) o;
  145.         return Objects.equals(roomName, room.roomName);
  146.     }
  147.  
  148.     @Override
  149.     public int hashCode() {
  150.  
  151.         return Objects.hash(roomName);
  152.     }
  153. }
  154.  
  155. // -------------------------------- House.java ------------------------------------
  156.  
  157. package cls;
  158.  
  159. import java.util.LinkedHashSet;
  160.  
  161. /**
  162.  * House class represents a simple house with rooms
  163.  */
  164. public class House {
  165.  
  166.     /**
  167.      * rooms - A LinkedHashSet used to store the rooms in the house
  168.      */
  169.     private LinkedHashSet<Room> rooms;
  170.  
  171.     /**
  172.      * @Constructor used to create an empty house.
  173.      */
  174.     public House() {
  175.         this.rooms = new LinkedHashSet<Room>();
  176.     }
  177.  
  178.     /**
  179.      * A method for adding a new room to the house
  180.      * @param room - Represents the room that will be added to the house
  181.      * @return true if the room was added to the house successfully.
  182.      * If another room with the same name exists the room will not be added to the house.
  183.      * The check is done using the equal and hashCode methods implemented in the Room class.
  184.      */
  185.     public boolean addRoom(Room room) {
  186.         if(rooms.add(room)) {
  187.             System.out.println("Room " + room.getRoomName() + " was added.");
  188.             return true;
  189.         }
  190.         System.out.println("Room " + room.getRoomName() + " already exists.");
  191.         return false;
  192.     }
  193.  
  194.     /**
  195.      * A method used to get the desired room according to the room's name.
  196.      * @param rName - Represents the room's name.
  197.      * @return the room object on success, or null on failure.
  198.      */
  199.     public Room getRoom(String rName) {
  200.         for(Room r : rooms) {
  201.             if(r.getRoomName().equals(rName)) {
  202.                 return r;
  203.             }
  204.         }
  205.         return null;
  206.     }
  207.  
  208.     /**
  209.      * A method for indicating if the house contains the specified room
  210.      * @param rName - Represents the room's name.
  211.      * @return true if such room exists in the house, or false otherwise
  212.      */
  213.     public boolean containsRoom(String rName) {
  214.         for(Room r : rooms) {
  215.             if(r.getRoomName().equals(rName)) {
  216.                 return true;
  217.             }
  218.         }
  219.         return false;
  220.     }
  221.  
  222.     /**
  223.      * A method for activating all the devices in the specified room.
  224.      * @param rName - Represents the room's name.
  225.      */
  226.     public void activateAllInARoom(String rName) {
  227.         getRoom(rName).activateAll();
  228.         System.out.println("All elements in " + rName + " were activated.");
  229.     }
  230.  
  231.     /**
  232.      * A method for deactivating all the devices in the specified room.
  233.      * @param rName - Represents the room's name.
  234.      */
  235.     public void deActivateAllInARoom(String rName) {
  236.         getRoom(rName).deActivateAll();
  237.         System.out.println("All elements in " + rName + " were deactivated.");
  238.     }
  239.  
  240.     /**
  241.      * A method for deactivating a specific device in the specified room.
  242.      * @param rName - Represents the room's name.
  243.      * @param elName - Represents the device's name.
  244.      */
  245.     public void deActivateElementInARoom(String rName, String elName) {
  246.         getRoom(rName).deActivateElement(elName);
  247.     }
  248.  
  249.     /**
  250.      * A method for activating a specific device in the specified room.
  251.      * @param rName - Represents the room's name.
  252.      * @param elName - Represents the device's name.
  253.      */
  254.     public void activateElementInARoom(String rName, String elName) {
  255.         getRoom(rName).activateElement(elName);
  256.     }
  257.  
  258.     /**
  259.      * A method for adding a given device in the specified room.
  260.      * @param rName - Represents the room's name.
  261.      * @param el - Represents the device object.
  262.      */
  263.     public void addElementInRoom(String rName, Device el) {
  264.         if(containsRoom(rName)) {
  265.             getRoom(rName).addElement(el);
  266.         } else {
  267.             System.out.println("Room " + rName + " does not exist!");
  268.         }
  269.  
  270.     }
  271.  
  272.     /**
  273.      * A method that checks if the house has any rooms in it.
  274.      * @return true if the house is empty, false otherwise.
  275.      */
  276.     public boolean isEmpty() {
  277.         return rooms.isEmpty();
  278.     }
  279.  
  280.     /**
  281.      * A utility method for printing the house's details along with the details on each room and device.
  282.      * @return A string with the house description.
  283.      */
  284.     @Override
  285.     public String toString() {
  286.         if(!isEmpty()) {
  287.             String s = "The house contains the following rooms and elements:\n";
  288.             for(Room r : rooms) {
  289.                 s += "\n"+r.toString();
  290.             }
  291.             return s;
  292.         }
  293.         return "The house has no rooms!";
  294.     }
  295. }
  296.  
  297. // -------------------------------------- Main.java -------------------------------
  298.  
  299. package Tester;
  300.  
  301. import cls.*;
  302.  
  303. import java.util.Scanner;
  304.  
  305. public class Main {
  306.    
  307.     /**
  308.      * Scanner sc is declared outside the main method to let other methods use it.
  309.      */
  310.     private static Scanner sc = new Scanner(System.in);
  311.  
  312.     public static void main(String[] args) {
  313.  
  314.  
  315.         House house = new House();
  316.         String rName = "", elName, elType;
  317.         int choice = -1;
  318.         String title = "====================================\n";
  319.         title += "= Welcome to the house application =\n====================================";
  320.         String mainMenu = "\n\nPlease choose from the following options - \n";
  321.         mainMenu += "1 - add a room.\n2 - Add an element in a room.\n3 - Activate all devices in a room.";
  322.         mainMenu += "\n4 - Deactivate all elements in a room.\n5 - Activate an element in a room.";
  323.         mainMenu += "\n6 - Deactivate an element in a room.\n7 - Print house details.\n0 - Exit the program.\n";
  324.  
  325.         //flag is used to terminate the execution when the user chooses '0' from the menu.
  326.         //flag2 is used to terminate the execution when the user chooses 'E' or 'e' on each action.
  327.         boolean flag = true, flag2 = false;
  328.         do {
  329.             if(!flag2) {
  330.                 //Printing the menu on each iteration until the program is terminated
  331.                 System.out.println("\n\n\n\n\n" + title + mainMenu);
  332.                 choice = sc.nextInt(); // User's choice from the main menu
  333.             } else {
  334.                 choice = 0;
  335.             }
  336.             sc.nextLine(); //Clear the buffer
  337.             switch (choice) {
  338.                 case 0:
  339.                     flag = false;
  340.                     break;
  341.                 case 1:
  342.                     System.out.println("Please enter the room's name: ");
  343.                     rName = sc.nextLine();
  344.                     house.addRoom(new Room(rName));
  345.                     flag2 = contProc();
  346.                     break;
  347.                 case 2:
  348.                     if(house.isEmpty()) {
  349.                         System.out.println("The house has no rooms!");
  350.                     } else {
  351.                         boolean isRunning = true;
  352.                         do {
  353.                             System.out.println("Please enter the room's name: ");
  354.                             rName = sc.nextLine();
  355.                             if (house.containsRoom(rName)) {
  356.                                 isRunning = false;
  357.                             } else {
  358.                                 System.out.println("No such room!!!");
  359.                             }
  360.                         } while (isRunning);
  361.                         System.out.println("Please enter the element's type: ");
  362.                         elType = sc.nextLine();
  363.                         System.out.println("Please enter the element's name: ");
  364.                         elName = sc.nextLine();
  365.                         house.addElementInRoom(rName, new Device(elName, elType));
  366.                     }
  367.                     flag2 = contProc();
  368.                     break;
  369.                 case 3:
  370.                     if(house.isEmpty()) {
  371.                         System.out.println("The house has no rooms!");
  372.                     } else {
  373.                         System.out.println("Please enter the room's name: ");
  374.                         rName = sc.nextLine();
  375.                         house.activateAllInARoom(rName);
  376.                     }
  377.                     flag2 = contProc();
  378.                     break;
  379.                 case 4:
  380.                     if(house.isEmpty()) {
  381.                         System.out.println("The house has no rooms!");
  382.                     } else {
  383.                         System.out.println("Please enter the room's name: ");
  384.                         rName = sc.nextLine();
  385.                         house.deActivateAllInARoom(rName);
  386.                     }
  387.                     flag2 = contProc();
  388.                     break;
  389.                 case 5:
  390.                     if(house.isEmpty()) {
  391.                         System.out.println("The house has no rooms!");
  392.                     } else {
  393.                         System.out.println("Please enter the room's name: ");
  394.                         rName = sc.nextLine();
  395.                         System.out.println("Please enter the element's name: ");
  396.                         elName = sc.nextLine();
  397.                         house.activateElementInARoom(rName, elName);
  398.                     }
  399.                     flag2 = contProc();
  400.                     break;
  401.                 case 6:
  402.                     if(house.isEmpty()) {
  403.                         System.out.println("The house has no rooms!");
  404.                     } else {
  405.                         System.out.println("Please enter the room's name: ");
  406.                         rName = sc.nextLine();
  407.                         System.out.println("Please enter the element's name: ");
  408.                         elName = sc.nextLine();
  409.                         house.deActivateElementInARoom(rName, elName);
  410.                     }
  411.                     flag2 = contProc();
  412.                     break;
  413.                 case 7:
  414.                     System.out.println(house.toString());
  415.                     flag2 = contProc();
  416.                     break;
  417.                 default:
  418.                     System.out.println("Default");
  419.                     break;
  420.             }
  421.         } while (flag);
  422.     }
  423.  
  424.     /**
  425.      * A utility method to check if the user wants to continue or exit the program.
  426.      * This method will be executed on each iteration/user choice.
  427.      * @return true if the user wishes to terminate the program.
  428.      * @throws StringIndexOutOfBoundsException if the user presses the 'Enter' key without any input
  429.      */
  430.     private static boolean contProc() {
  431.         System.out.println("\nPress 'E' to exit, or any other key to continue:");
  432.         try {
  433.             return (sc.nextLine().toLowerCase() == "e") ? true : false;
  434.         } catch(StringIndexOutOfBoundsException sioobe) {}
  435.         return false;
  436.     }
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement