In: Computer Science
Write a menu-driven program to implement a roomsboard for a classroom reservations system. The menu includes the following options:
The program will call the removeAll() method from the RoomsBoard class.
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() ; } }
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);
}
}