Question

In: Computer Science

The Menu-driven Program: Write a menu-driven program to implement a roomsboard for a classroom reservations system....

  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.

add it to the following code:

package Chegg;
public class ReservedRoom {
    private String roomID; //data member declaration
    private String courseID;
    private int time;

    public ReservedRoom(String roomID, String courseID, int time) { // constructor

        this.roomID = roomID;
        this.courseID = courseID;
        this.time = time;
    }

    public String getRoom() { //get methods
        return roomID;
    }

    public String getCourse() {
        return courseID;
    }

    public int getTime() {
        return time;
    }

    @Override
    public String toString() { // return string representation of ReservedRoom Object
        return "ReservedRoom [roomID=" + roomID + ", courseID=" + courseID + ", time=" + time + "]";
    }

}

----------------------------------------------------------------------------------------------------------------------------------

RoomsBoard class:

package Chegg;
public class RoomsBoard {
    ReservedRoomNode head;                 //Node for storing head of the list
    private int size=0;                    //variable for storing size
    public void add(ReservedRoom r)
    {
        ReservedRoomNode node=new ReservedRoomNode(r);
        ReservedRoomNode current;               //Node to traverse the list
        if(head==null|| head.getroom().getTime()>r.getTime())
        {
            node.setNext(head);
            head=node;
            size++;
        }
        else
        {
            current=head;
            while(current.getNext()!=null&& current.getNext().getroom().getTime()<r.getTime())
            {
                current=current.getNext();
            }
            node.setNext(current.getNext());
            current.setNext(node);
            size++;
        }

    }
    public void remove(String roomId)
    {
        if(head==null) {
            System.out.println("Number of Reserved Rooms: 0");
            return;
        }
        ReservedRoomNode current=head,temp=null;
        while(current!=null && current.getroom().getRoom()==roomId)
        {
            head=current.getNext();
            current=head;
            size--;
        }
        while(current!=null)
        {
            while(current!=null && current.getroom().getRoom()!=roomId)
            {
                temp=current;
                current=current.getNext();
            }
            if(current==null)
                return;
            temp.setNext(current.getNext());
            current=temp.getNext();
            size--;
        }
    }
    public void listRooms()
    {
        if(isEmpty()) {
            System.out.println("Number of Reserved Rooms: 0");
            return;
        }
        ReservedRoomNode current=head;                   //Node to traverse the list
        while(current!=null)
        {
            System.out.print(current);
            current=current.getNext();
        }
        System.out.println();
        System.out.println("-------------------------------------------------");
    }
    public void remove_all_but(String roomId)
    {
        if(isEmpty()) {
            System.out.println("Number of Reserved Rooms: 0");
            return;
        }
        ReservedRoomNode current=head;
        while(current.getroom().getRoom()!=roomId && current!=null)
        {
            current=current.getNext();
        }
        if(current==null)
            return;
        current.setNext(null);
        head=current;
        size=1;
    }
    public void removeAll()
    {
        head=null;                               //assigning null to head removes all nodes
        size=0;
    }
    public void split(RoomsBoard board1,RoomsBoard board2)
    {
        if(this.isEmpty())
        {
            System.out.println("Number of Reserved Rooms: 0");
            return;
        }
        ReservedRoomNode current=head;
        while(current!=null)
        {
            if(current.getroom().getTime()<=12)                    //board1 will store rooms with  timings before and at 12
                board1.add(current.getroom());
            else
                board2.add(current.getroom());                     //board2 will store rooms with timings after 12
            current=current.getNext();

        }
        System.out.print("Rooms before and at 12:");
        System.out.println();
        board1.listRooms();
        System.out.println();
        System.out.println("Rooms after 12:");
        board2.listRooms();

    }
    public void listReservations(String roomID)
    {
        if(isEmpty())
        {
            System.out.println("Number of Reserved Rooms: 0");
            return;
        }
        ReservedRoomNode current=head;
        while(current!=null)
        {
            if(current.getroom().getRoom()==roomID)
                System.out.println(current.getroom().toString());
            current=current.getNext();
        }
        System.out.println("--------------------------------------------------------");
    }
    public boolean isEmpty()
    {
        return head==null;
    }
    public int size()
    {
        return size;
    }
}

------------------------------------------------------------------------------------------------------------------------

RoomsBoardMain class

package Chegg;

public class RoomsBoardMain {
    public static void main(String[] args) {
        ReservedRoom r1=new ReservedRoom("98","maths",15);
        ReservedRoom r2=new ReservedRoom("94","physics",12);
        ReservedRoom r3=new ReservedRoom("99","english",9);
        ReservedRoom r4=new ReservedRoom("98","geography",11);
        ReservedRoom r5=new ReservedRoom("99","history",18);
        ReservedRoom r6=new ReservedRoom("101","geology",22);
        ReservedRoom r7=new ReservedRoom("98","chemistry",11);
        RoomsBoard list=new RoomsBoard();
        RoomsBoard board1=new RoomsBoard();
        RoomsBoard board2=new RoomsBoard();

        list.add(r1);
        list.add(r2);
        list.add(r3);
        list.add(r4);
        list.add(r5);
        list.add(r6);
        list.add(r7);
        list.listRooms();
        System.out.println("------------------------------------------");
        list.remove("98");
        System.out.println("Reserverd Rooms after removing room with ID=98");
        list.listRooms();
        list.split(board1,board2);
        System.out.println("Reservation for Room with ID=99");
        list.listReservations("99");
        System.out.print("Number of Reservations is Empty: ");
        System.out.println(list.isEmpty());
        System.out.println("Number of Reserved Rooms: "+list.size());
        System.out.println("----------------------------------------");
        System.out.println("Removing all Reserved Rooms except with id=101");
        list.remove_all_but("101");
        list.listRooms();
        System.out.println("------------------------------------");
        System.out.println("After Removing all reservations");
        list.removeAll();
        list.listRooms();
    }
}

-----------------------------------------------------------------------------------------------------------------------

ReservedRoomNode class

package Chegg;

public class ReservedRoomNode {                                     //class for creating nodes of ReservedRoom
     ReservedRoom room;
    ReservedRoomNode next;
    public ReservedRoomNode(ReservedRoom room)                             //constructor for creating Node
    {
        this.room=room;
    }

    public ReservedRoom getroom() {
        return room;
    }

    public void setRoom(ReservedRoom room) {
        this.room = room;
    }

    public ReservedRoomNode getNext() {
        return next;
    }

    public void setNext(ReservedRoomNode next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return room.toString() ;

    }
}

Solutions

Expert Solution

Answer:

/****************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

This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
Write a menu-driven program to handle the flow of widgets into and out of a warehouse....
Write a menu-driven program to handle the flow of widgets into and out of a warehouse.     The warehouse will have numerous deliveries of new widgets and orders for widgets     The widgets in a filled order are billed at a profit of 50 percent over their cost     Each delivery of new widgets may have a different cost associated with it     The accountants for the firm have instituted a last-in, first-out system for filling orders         the newest...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
Write a menu driven Java program which uses a method for each of the following operations:...
Write a menu driven Java program which uses a method for each of the following operations: (Note : The user should be allowed to repeat the operations as long as he wants to. Use appropriate number of parameters and return type for each method.) A. to find the sum of the following series (up to N terms). The program should    display the terms:              22 + 42 + 62… For example, if N=4, then the program should display the following...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for this game are special oriental dice. Each die has six different patterns. The six possible patterns are: Fish, Shrimp, Crab, Chicken, goldenCoin, Barrel. This game has three dice. In this game, the player can place his or her bet on any die-pattern. The amount of the winning prize is directly proportional to the number of matched dice after the two dice have been tossed...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT