In: Computer Science
Each ReservedRoom entry has three (data) fields:
Thus, the ReservedRoom class will consist of three data members (roomID and courseID, time), in addition to the associated methods needed as follows.
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:
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.
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.
The program should handle special cases and incorrect input, and terminate safely. Examples include (but not limited to) the following:
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);
}
}