Question

In: Computer Science

The ReservedRoom Class: Each ReservedRoom entry has three (data) fields: roomID (string) of the classroom e.g....

  1. The ReservedRoom Class:

Each ReservedRoom entry has three (data) fields:

  • roomID (string) of the classroom e.g. W330, W350, W361, etc.
  • courseID (string) which stores which course reserves this room
  • time (int): which stores the start time of the reserved slot i.e. 1820 means the reservation starts at 18:20

Thus, the ReservedRoom class will consist of three data members (roomID and courseID, time), in addition to the associated methods needed as follows.

  1. The constructor ReservedRoom (r, c, t): constructs a ReservedRoom object with the given parameters.
  2. String getRoom(): returns the roomID field.
  3. String getCourse(): returns the courseID field.
  4. int getTime(): returns the time field.
  5. String toString(): returns a String representation of this ReservedRoom entry.

Implement the ReservedRoom ADT using Java programming language.

The RoomsBoard Class:

RoomsBoard is a singly-linked list of ReservedRoom. The class RoomsBoard supports the following operations:

  1. void add(ReservedRoom r): adds a new ReservedRoom object to RoomsBoard. The ReservedRoom object should be inserted into the RoomsBoard list such that the nodes of the list appear in non-decreasing order of time.
  2. void remove(String roomID): removes all occurrences of the room with this ID.
  3. void remove_all_but(String roomID): removes all the reserved rooms except the room with this ID.
  4. void removeAll(): clears the RoomsBoard by removing all rooms entries.
  5. void split( RoomsBoard board1, RoomsBoard board2): splits the list into two lists, board1 stores the list of reserved rooms before 12:00 and board2 stores the list of rooms after 12:00.
  6. void listRooms(): prints a list of the reserved rooms in the roomsboard, in non-decreasing order.
  7. Void listReservations( roomID): prints the list of reservations for this room in the roomsboard, in non-decreasing order.
  8. int size(): returns the number of reserved rooms stored in the roomsboard.
  9. boolean isEmpty(): returns true if the RoomsBoard object is empty and false otherwise.

Implement the RoomsBoard ADT with a singly-linked list using Java programming language. The implementation must utilize the generic class Node as described in Section 3.2.1 of your text. Figure 1 shows a sample RoomsBoard object.

  1. The Menu-driven Program:

Write a menu-driven program to implement a roomsboard for a classroom reservations system. The menu includes the following options:

  1. Add new room.
    The program will prompt the user to enter the roomID, courseID and time of the new reserved room, then will call the add() method from the RoomsBoard class.
  2. Remove all occurrences of a room.
    The program will prompt the user to input the room ID to be removed, then call the remove() method from the RoomsBoard class.
  3. Remove all rooms except a room.
    The program will prompt the user to input the room ID to be kept, then call the remove_all_but () method from the RoomsBoard class.
  4. Clear the RoomsBoard.

The program will call the removeAll() method from the RoomsBoard class.

  1. Split the list of rooms into two lists by calling the method split() from the RoomsBoard class.
  2. Display RoomsBoard.
    The program will call the listRooms() method from the RoomsBoard class.
  3. Display the reservations of a room.
    The program will call the listReservations() method from the RoomsBoard class to display all reservations of this room with the given roomID.
  4. Exit.

The program should handle special cases and incorrect input, and terminate safely. Examples include (but not limited to) the following:

  1. Removing a RoomsEntry object from an empty RoomsBoard object.
  2. Removing a ReservedRoom object with a roomID that is not in the RoomsBoard object.
  3. Displaying an empty RoomsBoard object.
  4. Entering an incorrect menu option.

Solutions

Expert Solution

Hi,

Please find below code as per your requirement.

Let me know in case any changes required or any issue you face by comments.

Hope this answer helps you.

/****************JAVA CODE**************/

/************ReservedRoom.java************/

public class ReservedRoom {

   private String roomId;
   private String courseId;
   private int time;
  
   /**
   * Parameterized constructor
   * @param roomId
   * @param courseId
   * @param time
   */
   public ReservedRoom(String roomId, String courseId, int time) {
       this.roomId = roomId;
       this.courseId = courseId;
       this.time = time;
   }

   /**
   * @return the roomId
   */
   public String getRoomId() {
       return roomId;
   }

   /**
   * @return the courseId
   */
   public String getCourseId() {
       return courseId;
   }

   /**
   * @return the time
   */
   public int getTime() {
       return time;
   }

   @Override
   public String toString() {
       return "ReservedRoom [roomId=" + roomId + ", courseId=" + courseId + ", time=" + time + "]";
   }
     
  
}

/**************RoomsBoard.java****************/

public class RoomsBoard {

   //Represent a node of the singly linked list
   class Node{
       ReservedRoom reservedRoom;
       Node next;

       //Constructor
       public Node(ReservedRoom reservedRoom) {
           this.reservedRoom = reservedRoom;
           this.next = null;
       }
   }

   //Represent the head of the singly linked list
   public Node head = null;
   //Default size of the list
   public int size = 0;

   /***
   * This method used to add element in order of date non-descending
   * @param reservedRoom
   */
   public void add(ReservedRoom reservedRoom) {
       //Creating new node before inserting
       Node newNode = new Node(reservedRoom);
       //This blocks executes when there is not reservation
       if (head == null || head.reservedRoom.getTime() >= newNode.reservedRoom.getTime())
       {
           newNode.next = head;
           head = newNode;
       }
       else
       {
           //Insert node in sorted manner to maintain sorting by time
           Node current = head;
           while (current.next != null && current.next.reservedRoom.getTime() < newNode.reservedRoom.getTime())
           {
               current = current.next;
           }
           newNode.next = current.next;
           current.next = newNode;

       }
       size++;
   }

   /***
   * This method used to remove all occurrences of room from list
   * @param roomId
   */
   public void remove(String roomId) {
       //This check if head is having given roomId to remove
       while (head != null && head.reservedRoom.getRoomId().equals(roomId)) {
   head = head.next;
   size -= 1;
   }

   if (head == null) {
   return;
   }

   //This will remove all occurrences of roomId present in each node
   Node current = head;
   while (current.next != null) {
   if (current.next.reservedRoom.getRoomId().equals(roomId)) {
   current.next = current.next.next;
   size -= 1;
   } else {
   current = current.next;
   }
   }

   }
  
   /***
   * This method used to remove all reservations except given room Id
   * @param roomId
   */
   public void remove_all_but(String roomId) {
       Node current = head;
       // loop to iterate the nodes and remove other rooms except given
       while(current != null)
       {
           if(!current.reservedRoom.getRoomId().equals(roomId)) {
               remove(current.reservedRoom.getRoomId());
           }
           current = current.next;
       }
   }

   /**
   * This method remove all rooms reservation from list
   */
   public void removeAll() {
       Node current = head;

       // loop to iterate the linked list
       while(current != null)
       {

           remove(current.reservedRoom.getRoomId());
           current = current.next;
       }
   }

   /**
   * This method splits room reservation by time
   * @param board1
   * @param board2
   */
   public void split(RoomsBoard board1,RoomsBoard board2) {


       Node current = head;
       //Iterate over all nodes
       while (current != null) {

           //Checks if reserved room is before 1200 then it gets added in board1 otherwise in board2
           if(current.reservedRoom.getTime() <= 1200) {
               if(board1.head == null) {
                   board1.head = new Node(current.reservedRoom);
               } else {
                   Node temp = board1.head;
                   while(temp.next != null) {
                       temp = temp.next;
                   }
                   temp.next = new Node(current.reservedRoom);
               }
               board1.size++;
           } else {
               if(board2.head == null) {
                   board2.head = new Node(current.reservedRoom);
               } else {
                   Node temp = board2.head;
                   while(temp.next != null) {
                       temp = temp.next;
                   }
                   temp.next = new Node(current.reservedRoom);
               }
               board2.size++;
           }
           current = current.next;
       }

   }

   /**
   * This method use to prints all reserved rooms
   */
   public void listRooms() {
       Node current = head;
       while (current != null) {
           System.out.println(current.reservedRoom.toString());
           current = current.next;
       }
   }

   /**
   * This methods prints all reservation of given roomId
   * @param roomId
   */
   public void listReservations(String roomId) {
       Node current = head;
       while (current != null){
           if(current.reservedRoom.getRoomId().equals(roomId)) {
               System.out.println(current.reservedRoom.toString());
           }
           current = current.next;
       }
   }

   /**
   * This method return size of list
   * @return
   */
   public int size() {
       return size;
   }

   /**
   * This method checks if reservation is empty or not
   * @return
   */
   public boolean isEmpty() {
       return (size == 0);
   }
}

/**************ReservedRoomMenu.java****************/

public class ReservedRoomMenu {

   public static void main(String[] args) {

      
       Scanner sc = new Scanner(System.in);
       RoomsBoard roomsBoard = new RoomsBoard();
      
       int input = 0;
       do{
           System.out.println("1 Add new room");
           System.out.println("2 Remove all occurrences of a room.");
           System.out.println("3 Remove all rooms except a room.");
           System.out.println("4 Clear the RoomsBoard.");
           System.out.println("5 Split");
           System.out.println("6 Display RoomsBoard.");
           System.out.println("7 Display the reservations of a room.");
           System.out.println("8 Exit");
           System.out.println("Plese enter above menu between 1 to 8");
          
           input = Integer.parseInt(sc.nextLine());
          
           if(input == 1) {
               System.out.print("Please enter RoomId :");
               String roomId = sc.nextLine();
               System.out.print("Please enter CourseId :");
               String courseId = sc.nextLine();
               System.out.print("Please enter Time :");
               int time = Integer.parseInt(sc.nextLine());
               ReservedRoom reservedRoom = new ReservedRoom(roomId, courseId, time);
               roomsBoard.add(reservedRoom);
           } else if(input == 2) {
               if(roomsBoard.isEmpty()) {
                   System.out.println("No rooms available to remove");
                   continue;
               } else {
                   System.out.print("Please enter Room No. to remove :");
                   String roomNumber = sc.nextLine();
                   roomsBoard.remove(roomNumber);
                   System.out.println("Rooms removed successfully.");
                   continue;
               }
              
           } else if(input == 3) {
               if(roomsBoard.isEmpty()) {
                   System.out.println("No rooms available to remove");
                   continue;
               } else {
                   System.out.print("Please enter Room No. to exclude :");
                   String roomNumber = sc.nextLine();
                   roomsBoard.remove_all_but(roomNumber);
                   System.out.println("Rooms removed successfully.");
                   continue;
               }
           } else if(input == 4) {
               if(roomsBoard.isEmpty()) {
                   System.out.println("No rooms available to remove");
                   continue;
               } else {
                   roomsBoard.removeAll();
                   System.out.println("Rooms removed successfully.");
                   continue;
               }
           } else if(input == 5) {
               if(roomsBoard.isEmpty()) {
                   System.out.println("No rooms available to split");
                   continue;
               } else {
                   RoomsBoard board1 = new RoomsBoard();
                   RoomsBoard board2 = new RoomsBoard();
                   roomsBoard.split(board1,board2);
                   System.out.println("Rooms splited successfully.");
                   System.out.println("Board 1 - ");
                   board1.listRooms();
                   System.out.println("Board 2 - ");
                   board2.listRooms();
                  
                   continue;
               }
           } else if(input == 6) {
               if(roomsBoard.isEmpty()) {
                   System.out.println("No rooms available to display");
                   continue;
               } else {
                   roomsBoard.listRooms();
                   continue;
               }
           } else if(input == 7) {
               if(roomsBoard.isEmpty()) {
                   System.out.println("No rooms available to display");
                   continue;
               } else {
                   System.out.print("Please enter Room No. to view reservation :");
                   String roomNumber = sc.nextLine();
                   roomsBoard.listReservations(roomNumber);
                   continue;
               }
           } else if(input == 8) {
               break;
           } else {
               System.out.println("Invalid input, Please enter menu number bwtween 1 to 8");
           }
          
          
       } while(true);
      
      
      
   }

}


Related Solutions

public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and address (String). Implement the initialization constructor . Implement the setters and getters for all attributes. Implement the toString() method to display all attributes. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. Implement the compareTo (Entry other) method that returns 0 if the two...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String),...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
Design a class named Robot. The Robot class has three private data fields: position x, position...
Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method named forward that...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT