In: Computer Science
Put the ideas into practice by extending the original Room class (from lab 5) to create an AcademicRoom.java class.
Room.java:
import java.util.*;
public class Room
{
// fields
private String roomNumber;
private String buildingName;
private int capacity;
public Room()
{
this.capacity = 0;
}
public int compareTo(final Room o){
return Integer.compare(this.capacity, capacity);
}
/**
* Constructor for objects of class Room
*
* @param rN the room number
* @param bN the building name
* @param c the room capacity
*/
public Room(String rN, String bN, int c)
{
setRoomNumber(rN);
setBuildingName(bN);
setCapacity(c);
}
/**
* Mutator method (setter) for room number.
*
* @param rN a new room number
*/
public void setRoomNumber(String rN)
{
this.roomNumber = rN;
}
/**
* Mutator method (setter) for building name.
*
* @param bN a new building name
*/
public void setBuildingName(String bN)
{
this.buildingName = bN;
}
/**
* Mutator method (setter) for capacity.
*
* @param c a new capacity
*/
public void setCapacity(int c)
{
this.capacity = c;
}
/**
* Accessor method (getter) for room number.
*
* @return the room number
*/
public String getRoomNumber()
{
return this.roomNumber;
}
/**
* Accessor method (getter) for building name.
*
* @return the building name
*/
public String getBuildingName()
{
return this.buildingName;
}
/**
* Accessor method (getter) for capacity.
*
* @return the capacity
*/
public int getCapacity()
{
return this.capacity;
}
}
Below is your code:
Room.java
import java.util.*;
public class Room {
// fields
private String roomNumber;
private String buildingName;
private int capacity;
public Room() {
this.capacity = 0;
}
public int compareTo(final Room o) {
return Integer.compare(this.capacity, capacity);
}
/**
* Constructor for objects of class Room
*
* @param rN
* the room number
* @param bN
* the building name
* @param c
* the room capacity
*/
public Room(String rN, String bN, int c) {
setRoomNumber(rN);
setBuildingName(bN);
setCapacity(c);
}
/**
* Mutator method (setter) for room number.
*
* @param rN
* a new room number
*/
public void setRoomNumber(String rN) {
this.roomNumber = rN;
}
/**
* Mutator method (setter) for building name.
*
* @param bN
* a new building name
*/
public void setBuildingName(String bN) {
this.buildingName = bN;
}
/**
* Mutator method (setter) for capacity.
*
* @param c
* a new capacity
*/
public void setCapacity(int c) {
this.capacity = c;
}
/**
* Accessor method (getter) for room number.
*
* @return the room number
*/
public String getRoomNumber() {
return this.roomNumber;
}
/**
* Accessor method (getter) for building name.
*
* @return the building name
*/
public String getBuildingName() {
return this.buildingName;
}
/**
* Accessor method (getter) for capacity.
*
* @return the capacity
*/
public int getCapacity() {
return this.capacity;
}
}
AcademicRoom.java
public class AcademicRoom extends Room {
// fields
private String department;
private boolean isProjectorAvailable;
/**
* Constructor for objects of class AcademicRoom
*
* @param rN
* the room number
* @param bN
* the building name
* @param c
* the room capacity
* @param department
* the room department
* @param isProjectorAvailable
* the room isProjectorAvailable
*/
public AcademicRoom(String rN, String bN, int c, String department,
boolean isProjectorAvailable) {
super(rN, bN, c);
this.department = department;
this.isProjectorAvailable = isProjectorAvailable;
}
/**
* Accessor method (getter) for department.
*
* @return the department
*/
public String getDepartment() {
return department;
}
/**
* Mutator method (setter) for department.
*
* @param department
* a new department
*/
public void setDepartment(String department) {
this.department = department;
}
/**
* Accessor method (getter) for isProjectorAvailable.
*
* @return the isProjectorAvailable
*/
public boolean isProjectorAvailable() {
return isProjectorAvailable;
}
/**
* Mutator method (setter) for isProjectorAvailable.
*
* @param isProjectorAvailable
* a new isProjectorAvailable
*/
public void setProjectorAvailable(boolean isProjectorAvailable) {
this.isProjectorAvailable = isProjectorAvailable;
}
//method to get object as String
public String toString() {
return "Room Number: " + this.getRoomNumber() + "\nBuilding Name: "
+ this.getBuildingName() + "\nCapacity: " + this.getCapacity()
+ "\nDepartment: " + this.department
+ "\nIs Projector Available: " + this.isProjectorAvailable;
}
}
TestAcademicRoom.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestAcademicRoom {
public static void main(String[] args) {
List<AcademicRoom> rooms = new ArrayList<>();
rooms.add(new AcademicRoom("121A", "Oracle", 22, "Engineering", true));
rooms.add(new AcademicRoom("121B", "Oracle", 22, "Engineering", false));
rooms.add(new AcademicRoom("221A", "World", 22, "Education", true));
Scanner sc = new Scanner(System.in);
int choice = 0;
while (choice != 3) {
System.out.println();
printMenu();
choice = Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
System.out.print("Enter a room number: ");
String roomNum = sc.nextLine();
System.out.print("Enter a building name: ");
String buildingName = sc.nextLine();
System.out.print("Enter a room capacity: ");
int capacity = Integer.parseInt(sc.nextLine());
System.out.print("Enter a department name: ");
String department = sc.nextLine();
System.out.print("Is Projector Available in room (y/n): ");
String isProj = sc.nextLine();
if (isProj.equalsIgnoreCase("y")) {
rooms.add(new AcademicRoom(roomNum, buildingName, capacity,
department, true));
} else {
rooms.add(new AcademicRoom(roomNum, buildingName, capacity,
department, false));
}
break;
case 2:
System.out.println("Enter a building name: ");
String bName = sc.nextLine();
System.out.print("Do you want to enter a room number (y/n): ");
String isRoomEntered = sc.nextLine();
String roomN = "";
if (isRoomEntered.equalsIgnoreCase("y")) {
System.out.print("Enter a room Number: ");
roomN = sc.nextLine();
}
List<AcademicRoom> searchedRooms = new ArrayList<>();
if (isRoomEntered.equalsIgnoreCase("y")) {
for (AcademicRoom academicRoom : rooms) {
if (academicRoom.getBuildingName().equalsIgnoreCase(
bName)
&& academicRoom.getRoomNumber()
.equalsIgnoreCase(roomN)) {
searchedRooms.add(academicRoom);
}
}
} else {
for (AcademicRoom academicRoom : rooms) {
if (academicRoom.getBuildingName().equalsIgnoreCase(
bName)) {
searchedRooms.add(academicRoom);
}
}
}
System.out.println("\nSearched rooms are: ");
printList(searchedRooms);
break;
case 3:
System.out.println("\nThanks.");
break;
default:
System.out.println("Please enter a correct choice.");
}
}
}
public static void printMenu() {
System.out.println("Please select from below list: ");
System.out.println("1. Add a new room");
System.out.println("2. Search a room");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
}
public static void printList(List<AcademicRoom> rooms) {
for (AcademicRoom academicRoom : rooms) {
System.out.println(academicRoom);
System.out.println();
}
}
}
Output
Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 1
Enter a room number: 221B
Enter a building name: World
Enter a room capacity: 23
Enter a department name: Education
Is Projector Available in room (y/n): n
Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 2
Enter a building name:
World
Do you want to enter a room number (y/n): n
Searched rooms are:
Room Number: 221A
Building Name: World
Capacity: 22
Department: Education
Is Projector Available: true
Room Number: 221B
Building Name: World
Capacity: 23
Department: Education
Is Projector Available: false
Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 2
Enter a building name:
Oracle
Do you want to enter a room number (y/n): y
Enter a room Number: 121B
Searched rooms are:
Room Number: 121B
Building Name: Oracle
Capacity: 22
Department: Engineering
Is Projector Available: false
Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 3
Thanks.