In: Computer Science
Room.java:
public class Room
{
// fields
private String roomNumber;
private String buildingName;
private int capacity;
public Room()
{
this.capacity = 0;
}
/**
* 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;
}
}
Put the ideas into practice by extending the original Room class to create an AcademicRoom.java class.
import java.util.ArrayList;
public class AcademicRoom extends Room {
String AcademicDepartment;
Boolean projector;
public AcademicRoom(String buildingName, String roomNo, int capacity, String department, Boolean projector) {
super(roomNo, buildingName, capacity);
setAcademicDepartment(department);
setProjector(projector);
}
public void setAcademicDepartment(String academicDepartment) {
this.AcademicDepartment = academicDepartment;
}
public void setProjector(Boolean projector) {
this.projector = projector;
}
public String getAcademicDepartment() {
return this.AcademicDepartment;
}
public Boolean getProjector() {
return this.projector;
}
@Override
public String toString() {
return "Room No:" + getRoomNumber() + ", Building Name: " + getBuildingName() + ", Capacity: " + getCapacity() + ", Academic Department: " + getAcademicDepartment() + ", Projector Present: " + getProjector();
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class TestAcademicRoom {
public TestAcademicRoom() {
}
boolean compare(AcademicRoom room, ArrayList<AcademicRoom> roomList) {
for (int i = 0; i < roomList.size(); i++) {
AcademicRoom room3 = roomList.get(i);
if(room3.getProjector().equals(room.getProjector()) && room3.getAcademicDepartment().equals(room.getAcademicDepartment()) && room3.getBuildingName().equals(room.getBuildingName()) && room3.getCapacity() == room.getCapacity() && room3.getRoomNumber().equals(room.getRoomNumber()))
return true;
}
return false;
}
ArrayList<AcademicRoom> search(ArrayList<AcademicRoom> roomList, String buildingName, String roomNo, boolean status) {
ArrayList<AcademicRoom> result = new ArrayList<AcademicRoom>();
if(status) {
for(int i = 0; i < roomList.size(); i++) {
AcademicRoom room1 = roomList.get(i);
if(room1.getBuildingName().equals(buildingName) && room1.getRoomNumber().equals(roomNo)) {
result.add(room1);
}
}
}else {
for(int i = 0; i < roomList.size(); i++) {
AcademicRoom room2 = roomList.get(i);
if(room2.getBuildingName().equals(buildingName))
result.add(room2);
}
}
return result;
}
public static void main(String[] args) {
TestAcademicRoom testRoom = new TestAcademicRoom();
ArrayList<AcademicRoom> roomList = new ArrayList<AcademicRoom>();
// Adding rooms to list of own choice
roomList.add(new AcademicRoom("james", "216", 300, "Science", true));
roomList.add(new AcademicRoom("F2", "219", 305, "Arts", false));
roomList.add(new AcademicRoom("XDD", "345", 100, "Literature", true));
roomList.add(new AcademicRoom("GHJHFD", "556", 598, "Commerce", false));
roomList.add(new AcademicRoom("JJHDG", "789", 200, "Robotics", false));
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("1. Add Another Room To the List.\n2. Search For rooms. \n3. Exit.");
int input = sc.nextInt();
if(input == 1) {
String roomno, buildingName, department, projector;
int capacity;
System.out.print("Enter Room NO.: ");
roomno = sc.next();
System.out.print("Enter Building Name: ");
buildingName = sc.next();
System.out.print("Enter Capacity: ");
capacity = sc.nextInt();
System.out.print("Enter Department ");
department = sc.next();
System.out.print("Projector Present 'y or n': ");
projector = sc.next();
boolean p = false;
if(projector.equals('Y') || projector.equals('y'))
p = true;
AcademicRoom room = new AcademicRoom(buildingName, roomno, capacity, department, p);
if(!testRoom.compare(room, roomList)){
roomList.add(new AcademicRoom(buildingName, roomno, capacity, department, p));
System.out.println("Room Added....");
}
}else if(input == 2) {
String buildingName, roomNo = "none", status;
ArrayList<AcademicRoom> rooms;
System.out.print("Enter Building Name: ");
buildingName = sc.next();
System.out.print("Enter Room no 'Y or N' ");
status = sc.next();
if(status.equals("Y") || status.equals("y")){
roomNo = sc.next();
rooms = testRoom.search(roomList, buildingName, roomNo, true);
if(rooms.size() < 1)
System.out.print("No Rooms to show.");
else {
for(int i = 0; i < rooms.size(); i++) {
System.out.println(rooms.get(i));
}
}
}else {
rooms = testRoom.search(roomList, buildingName, roomNo, false);
if(rooms.size() < 1)
System.out.println("No Rooms to show...");
else{
System.out.println("Rooms in the building: " + buildingName);
for (int i = 0; i < rooms.size(); i++) {
System.out.println(rooms.get(i));
}
}
}
}else if(input == 3){
break;
}
else {
System.out.println("Wrong Input. Try Again....");
}
}
}
}