Hey there everyone, I’m trying to finish up a java project on Abstract classes a
ID: 3700961 • Letter: H
Question
Hey there everyone, I’m trying to finish up a java project on Abstract classes and Interfaces. A good portion of the code is already done, but I could use some help with my last few requirements. Any help would be appreciated, thanks. My current code is listed below the requirements and is in 3 packages, with thier beginning and end stated in bold and each class is seperated by "#########". ?The requirements are as follows:
Requirement 7
Modify the show room functionality so the user can see the rooms ordered as they choose. The
following should be presented to the user after they select 4 and then the rooms should be displayed in
the correct order.
Enter the sort option:
1) Sort by number of seats ascending
2) Sort by room number ascending
Requirement 8
Create an abstract class called RoomBase. This should have the following:
abstract method:
? - getRoomDetailsFromUser()
attributes:
? - roomNumber (which is an int)
? - numberOfSeats (which is an int)
The Room class already has this method and attributes, but modify the Room class to ensure that it
inherits attributes/methods from the RoomBase class.
Requirement 9
Create an interface called Bookable. The interface should have the following abstract methods:
reserveThisRoom()
releaseThisRoom()
Make sure that the Room class implements this interface.
My current code is:
---Start of "room.bo" package---
package room.bo;
import java.util.Scanner;
/**
* Biology lab is equipped to allow working with biology.
*
* @since
* @author
*/
public class BiologyLab extends Room {
private int numberOfMicroscopes;
private boolean hasFumeHood;
private boolean hasBunsonBurners;
/**
* default constructor
*
* @since
* @author
*/
public BiologyLab() {
}
/**
* Custom constructor that sets all attributes
*
* @param numberOfMicroscopes
* @param hasFumeHood
* @param hasBunsonBurners
* @param roomNumber
* @param numberOfSeats
* @param reserved
* @param hasSmartBoard
* @since
* @author
*/
public BiologyLab(int numberOfMicroscopes, boolean hasFumeHood, boolean hasBunsonBurners, int roomNumber, int numberOfSeats, boolean reserved, boolean hasSmartBoard) {
super(roomNumber, numberOfSeats, reserved, hasSmartBoard);
this.numberOfMicroscopes = numberOfMicroscopes;
this.hasFumeHood = hasFumeHood;
this.hasBunsonBurners = hasBunsonBurners;
}
public int getNumberOfMicroscopes() {
return numberOfMicroscopes;
}
public void setNumberOfMicroscopes(int numberOfMicroscopes) {
this.numberOfMicroscopes = numberOfMicroscopes;
}
public boolean isHasFumeHood() {
return hasFumeHood;
}
public void setHasFumeHood(boolean hasFumeHood) {
this.hasFumeHood = hasFumeHood;
}
public boolean isHasBunsonBurners() {
return hasBunsonBurners;
}
public void setHasBunsonBurners(boolean hasBunsonBurners) {
this.hasBunsonBurners = hasBunsonBurners;
}
@Override
public String toString() {
return super.toString() + " Number of microscopes: " + numberOfMicroscopes + " Has fumehood: " + hasFumeHood + " Has Bunson burners: " + hasBunsonBurners;
}
/**
* Get the details from the user about this class. This will invoke the
* super method to get the base class attributes.
*
* @since
* @author
*/
@Override
public void getRoomDetailsFromUser() {
super.getRoomDetailsFromUser();
Scanner input = new Scanner(System.in);
System.out.print("How many microscopes does this room have? ");
numberOfMicroscopes = input.nextInt();
input.nextLine();
System.out.print("Does this lab have a fume hood (y/n)? ");
hasFumeHood = input.nextLine().equalsIgnoreCase("y");
System.out.print("Does this lab have bunson burners(y/n)? ");
hasBunsonBurners = input.nextLine().equalsIgnoreCase("y");
}
}
##################################################################################
package room.bo;
import java.util.Scanner;
/**
* BoardRoom is a type of room used for meetings
*
* @since
* @author
*/
public class BoardRoom extends Room {
private boolean hasTeleconferencing;
/**
* default constructor
*
* @since
* @author
*/
public BoardRoom() {
}
/**
* Custom constructor which sets all attributes of the class
*
* @param hasTeleconferencing
* @param roomNumber
* @param numberOfSeats
* @param reserved
* @param hasSmartBoard
* @author
* @since
*/
public BoardRoom(boolean hasTeleconferencing, int roomNumber, int numberOfSeats, boolean reserved, boolean hasSmartBoard) {
super(roomNumber, numberOfSeats, reserved, hasSmartBoard);
this.hasTeleconferencing = hasTeleconferencing;
}
public boolean isHasTeleconferencing() {
return hasTeleconferencing;
}
public void setHasTeleconferencing(boolean hasTeleconferencing) {
this.hasTeleconferencing = hasTeleconferencing;
}
public String toString() {
return super.toString() + " Has teleconferencing: " + hasTeleconferencing;
}
/**
* Get the details from the user about this class. This will invoke the
* super method to get the base class attributes.
*
* @since
* @author
*/
public void getRoomDetailsFromUser() {
super.getRoomDetailsFromUser();
Scanner input = new Scanner(System.in);
System.out.print("Does this room have teleconferencing (y/n) ");
hasTeleconferencing = input.nextLine().equalsIgnoreCase("y");
;
}
}
##################################################################################
package room.bo;
import java.util.Scanner;
/**
* Computer room is a room which is equipped with computer stations.
*
* @since
* @author
*/
public class ComputerRoom extends Room {
int numberOfComputers;
/**
* Default constructor
*/
public ComputerRoom() {
}
/**
* Custom constructor that sets all of the class attributes.
*
* @param numberOfComputers
* @param roomNumber
* @param numberOfSeats
* @param reserved
* @param hasSmartBoard
* @since
* @author
*/
public ComputerRoom(int numberOfComputers, int roomNumber, int numberOfSeats, boolean reserved, boolean hasSmartBoard) {
super(roomNumber, numberOfSeats, reserved, hasSmartBoard);
this.numberOfComputers = numberOfComputers;
}
/**
* Get the details from the user about this class. This will invoke the
* super method to get the base class attributes.
*
* @since
* @author
*/
public void getRoomDetailsFromUser() {
super.getRoomDetailsFromUser();
Scanner input = new Scanner(System.in);
System.out.print("Enter number of computers: ");
numberOfComputers = input.nextInt();
input.nextLine();
}
public int getNumberOfComputer() {
return numberOfComputers;
}
public void setNumberOfComputer(int numberOfComputer) {
this.numberOfComputers = numberOfComputer;
}
public String toString() {
return super.toString() + " Number of Computers" + numberOfComputers;
}
}
##################################################################################
package room.bo;
import java.util.Scanner;
/**
*
* This is the base type of room. Has the basics for a class room but not much specialized
* equipment.
*
* @since
* @author
*/
public class Room {
Scanner input = new Scanner(System.in);
private int roomNumber;
private int numberOfSeats;
private String reservedBy = "";
private boolean reserved;
private boolean hasSmartBoard;
/**
* Default constructor
* @since
* @author
*/
public Room() {
}
/**
* Custom constructor that accepts all of the attributes of the class.
* @param roomNumber
* @param numberOfSeats
* @param reserved
* @param hasSmartBoard
* @since
* @author
*/
public Room(int roomNumber, int numberOfSeats, boolean reserved, boolean hasSmartBoard) {
this.roomNumber = roomNumber;
this.numberOfSeats = numberOfSeats;
this.reserved = reserved;
this.hasSmartBoard = hasSmartBoard;
}
/**
* Get the attribute values from the user.
*
* @since
* @author
*/
public void getRoomDetailsFromUser() {
System.out.print("Enter number of seats: ");
numberOfSeats = input.nextInt();
input.nextLine();
System.out.print("Does this classroom have a smart board? (y/n)");
hasSmartBoard = input.nextLine().equalsIgnoreCase("y");
}
public boolean isHasSmartBoard() {
return hasSmartBoard;
}
public void setHasSmartBoard(boolean hasSmartBoard) {
this.hasSmartBoard = hasSmartBoard;
}
public int getNumberOfSeats() {
return numberOfSeats;
}
public void setNumberOfSeats(int numberOfSeats) {
this.numberOfSeats = numberOfSeats;
}
public String getReservedBy() {
return reservedBy;
}
public void setReservedBy(String reservedBy) {
this.reservedBy = reservedBy;
}
public boolean isReserved() {
return reserved;
}
public void setReserved(boolean reserved) {
this.reserved = reserved;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getRoomNumber() {
return roomNumber;
}
/**
* Update the room to reserved and get the reserved by.
*
* @since
* @author
*/
public void reserveThisRoom() {
this.reserved = true;
System.out.println("Enter the name of the person reserving this room: ");
reservedBy = input.nextLine();
}
/**
* Update the room to not reserved and clear the reserved by.
*
* @since
* @author
*/
public void releaseThisRoom() {
this.reserved = false;
reservedBy = "";
System.out.println("Room has been released ");
}
public String toString() {
String output = " "
+ " Room Number: " + roomNumber
+ " Number of Seats: " + numberOfSeats
+ " Reserved By: " + reservedBy
+ " Reserved: " + reserved
+ " Smart Board: " + hasSmartBoard;
return output;
}
}
---End of "room.bo" package---
##################################################################################
--- Start of "room.main" package---
package room.main;
import room.bo.ComputerRoom;
import room.bo.BiologyLab;
import room.bo.BoardRoom;
import room.bo.Room;
import room.util.RoomUtil;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This main method will invoke functionality for booking college rooms.
*
* Detailed description:
* https://docs.google.com/document/d/1jyrvSJHXS6BZuXKVswYkmt2muBmPI71OxXTLQxerDVU/edit
*
* @author
*/
public class RoomMain {
private static final int ROOM_DOES_NOT_EXIST = -1;
private static ArrayList<Room> rooms = new ArrayList();
/**
* Main method controls program and user interface.
*
* @param args the command line arguments
* @since
* @author
*/
public static void main(String[] args) {
RoomUtil.createTestRooms(rooms);
Scanner input = new Scanner(System.in);
String menu = "------------------------ - CIS Room Booking "
+ "------------------------ "
+ "Choose an option: "
+ "1) Add Room "
+ "2) Reserve Room "
+ "3) Release Room "
+ "4) Show Rooms "
+ "5) Show Room Count Details "
+ "6) Search For Room "
+ "7) Exit";
int selection = 0;
while (selection != 7) {
System.out.println(menu);
selection = input.nextInt();
input.nextLine();
switch (selection) {
case 1:
addRoom();
break;
case 2:
reserveRoom();
break;
case 3:
releaseRoom();
break;
case 4:
showRooms();
break;
case 5:
break;
case 6:
break;
case 7:
System.out.println("Goodbye");
break;
default:
System.out.println("Invalid option.");
}
}
}
/**
* Loop through the rooms to check if the room already exists.
*
* @param roomNumber
* @return the index of the room number
* @since
* @author
*/
public static int getRoomNumberIfExists(int roomNumber) {
int index = -1;
for (int i = 0; i < rooms.size(); i++) {
if (rooms.get(i).getRoomNumber() == roomNumber) {
index = i;
}
}
return index;
}
/**
* This method will allow the user to add a new room to the collection of rooms.
*
*/
public static void addRoom() {
//***********************************************************
//Ask which room number the user wants to add
//***********************************************************
Room room = null;
Scanner input = new Scanner(System.in);
System.out.print("Enter room number: ");
int roomNumber = input.nextInt();
input.nextLine();
//***********************************************************
//Check to see if the room already exists
//***********************************************************
int roomNumberIndex = getRoomNumberIfExists(roomNumber);
//If the room does not already exist.
if (roomNumberIndex == ROOM_DOES_NOT_EXIST) {
roomNumberIndex = rooms.size();
boolean finished = false;
do {
System.out.print("What type of room is this? " + "1) Add Room "
+ "2) Computer Lab "
+ "3) Board Room "
+ "4) Biology lab ");
String choice = input.nextLine();
//***********************************************************
//Based on the user input, create the correct type of room.
//***********************************************************
switch (choice) {
case "1":
room = new Room();
finished = true;
break;
case "2":
room = new ComputerRoom();
finished = true;
break;
case "3":
room = new BoardRoom();
finished = true;
break;
case "4":
room = new BiologyLab();
finished = true;
break;
default:
System.out.println("Invalid option");
}
} while (!finished);
//Set the details for the room
room.setRoomNumber(roomNumber);
//Note the correct method will be invoked based on which type of room was created above.
room.getRoomDetailsFromUser();
//Add the room to the collection of rooms. Note that as long as an object 'is a' Room
//(all of the types of rooms above are rooms), then it can be added to the collection of
//rooms.
rooms.add(room);
} else {
String choice = "";
System.out.println("Room already exists. Do you want to continue? (Y/N)");
choice = input.nextLine();
//If the user wants to continue, invoke the method to change the value of attributes in
//the room
if (choice.equalsIgnoreCase("y")) {
rooms.get(roomNumberIndex).getRoomDetailsFromUser();
}
}
}
/**
* This method will allow the user to reserve a room.
*/
public static void reserveRoom() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the room number you would like to book");
int roomNumber = input.nextInt();
input.nextLine();
//Check to see if the room exists.
int roomNumberIndex = getRoomNumberIfExists(roomNumber);
if (roomNumberIndex < 0) {
System.out.println("This room does not exist");
} else {
//Put the room from the ArrayList into a local variable.
Room room = rooms.get(roomNumberIndex);
if (!room.isReserved()) {
room.reserveThisRoom();
} else {
System.out.println("This room is already booked!");
}
}
}
public static void releaseRoom() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the room number you would like to release");
int roomNumber = input.nextInt();
input.nextLine();
//Check if the room exists.
int roomNumberIndex = getRoomNumberIfExists(roomNumber);
if (roomNumberIndex < 0) {
System.out.println("This room does not exist");
} else {
//Put the room from the ArrayList into a local variable.
Room room = rooms.get(roomNumberIndex);
//If the room is reserved, allow them to release.
if (room.isReserved()) {
room.releaseThisRoom();
} else {
System.out.println("This room is not booked!");
}
}
}
/**
* Show the details for each room
*/
public static void showRooms() {
System.out.println("");
System.out.println("------------------------");
System.out.println("- Room List");
System.out.println("------------------------");
if(rooms.size() == 0){
System.out.println("There are no rooms.");
}
for (int i = 0; i < rooms.size(); i++) {
System.out.println(rooms.get(i));
}
System.out.println("");
}
}
---End of "room.main" package---
##################################################################################
---Start of "room.util" package---
package room.util;
import room.bo.BoardRoom;
import room.bo.ComputerRoom;
import room.bo.Room;
import java.util.ArrayList;
/**
*
* @author
*/
public class RoomUtil {
public static void createTestRooms(ArrayList<Room> rooms){
rooms.add(new ComputerRoom(20,107, 24, false, true));
rooms.add(new ComputerRoom(18,110, 18, false, true));
rooms.add(new ComputerRoom(20,205, 24, false, true));
rooms.add(new BoardRoom(true,15, 10, false, false));
rooms.add(new Room(118, 28, false, false));
rooms.add(new Room(124, 40, false, false));
}
}
##################################################################################
package room.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
* Utility class for generic program functions.
*
* @author
* @since
*/
public class Util {
public static final boolean DEBUGGING = false;
private static Scanner input = new Scanner(System.in);
private static boolean isGUI = false;
/**
* Get a scanner object.
*
* @return Scanner
* @since
* @author
*/
public static Scanner getInput() {
return input;
}
/**
* Get input from the user using the appropriate input method
*
* @return String entered by user.
* @since
* @author
*/
public static String getInputString(String output) {
if (isGUI) //use JoptionPane
{
return JOptionPane.showInputDialog(output);
} else {
display(output);
return input.nextLine();
}
}
/**
* Get input from the user using the appropriate input method
*
* @return int entered by user
* @since
* @author
*/
public static int getInputInt(String output) {
if (isGUI) //use JoptionPane
{
return Integer.parseInt(JOptionPane.showInputDialog(output));
} else {
display(output);
return Integer.parseInt(input.nextLine());
}
}
/**
* Get input from the user using the appropriate input method
*
* @return double entered by user
* @since
* @author
*/
public static double getInputDouble(String output) {
if (isGUI) //use JoptionPane
{
return Double.parseDouble(JOptionPane.showInputDialog(output));
} else {
display(output);
return Double.parseDouble(input.nextLine());
}
}
/**
* Get input from the user using the appropriate input method
*
* @param output
* @return boolean entered by user.
* @since
* @author
*/
public static boolean getInputBoolean(String output) {
boolean answer = false;
if (isGUI) { //use JoptionPane
if (JOptionPane.showInputDialog(output).equalsIgnoreCase("Y")) {
answer = true;
}
} else {
display(output);
if (input.nextLine().equalsIgnoreCase("Y")) {
answer = true;
}
}
return answer;
}
/**
* Display output to the user using the appropriate input method
*
* @return void
* @since
* @author
*/
public static void display(String output) {
if (isGUI) {
JOptionPane.showMessageDialog(null, output);
} else {
System.out.println(output);
}
}
/**
* Set the gui indicator. If gui then JOptionPane will be used.
*
* @author
* @param setter
*/
public static void setGUI(boolean setter) {
isGUI = setter;
}
/**
* Get today in the specified format
*
* @since
* @author
* @param format The format for the date
*/
public static String getNow(String format) {
if (format.isEmpty()) {
format = "yyyy/MM/dd HH:mm:ss"; //Default
}
DateFormat dateFormat = new SimpleDateFormat(format);
Date date = new Date();
return dateFormat.format(date);
}
}
---End of "room.util" package---
##################################################################################
Explanation / Answer
The following changes have been made to the code to add the abstract class and interface and to sort according to option entered. All codes separated by ------. All code changes are highlighted with boldcase
Bookable.java
package room.bo;
public interface Bookable {
public void reserveThisRoom();
public void releaseThisRoom();
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
RoomBase.java
package room.bo;
public abstract class RoomBase {
abstract void getRoomDetailsFromUser();
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
Room.java
package room.bo;
import java.util.Scanner;
/**
*
* This is the base type of room. Has the basics for a class room but not much specialized
* equipment.
*
* @since
* @author
*/
public class Room extends RoomBase implements Bookable{ /* Room class will use getRoomDetailsFromUser() method of RoomBase abstract class By extending it and reserveThisRoom(), releaseThisRoom() methods of Bookable interface by implementing it */
Scanner input = new Scanner(System.in);
private int roomNumber;
private int numberOfSeats;
private String reservedBy = "";
private boolean reserved;
private boolean hasSmartBoard;
/**
* Default constructor
* @since
* @author
*/
public Room() {
}
/**
* Custom constructor that accepts all of the attributes of the class.
* @param roomNumber
* @param numberOfSeats
* @param reserved
* @param hasSmartBoard
* @since
* @author
*/
public Room(int roomNumber, int numberOfSeats, boolean reserved, boolean hasSmartBoard) {
this.roomNumber = roomNumber;
this.numberOfSeats = numberOfSeats;
this.reserved = reserved;
this.hasSmartBoard = hasSmartBoard;
}
/**
* Get the attribute values from the user.
*
* @since
* @author
*/
public void getRoomDetailsFromUser() {
System.out.print("Enter number of seats: ");
numberOfSeats = input.nextInt();
input.nextLine();
System.out.print("Does this classroom have a smart board? (y/n)");
hasSmartBoard = input.nextLine().equalsIgnoreCase("y");
}
public boolean isHasSmartBoard() {
return hasSmartBoard;
}
public void setHasSmartBoard(boolean hasSmartBoard) {
this.hasSmartBoard = hasSmartBoard;
}
public int getNumberOfSeats() {
return numberOfSeats;
}
public void setNumberOfSeats(int numberOfSeats) {
this.numberOfSeats = numberOfSeats;
}
public String getReservedBy() {
return reservedBy;
}
public void setReservedBy(String reservedBy) {
this.reservedBy = reservedBy;
}
public boolean isReserved() {
return reserved;
}
public void setReserved(boolean reserved) {
this.reserved = reserved;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public int getRoomNumber() {
return roomNumber;
}
/**
* Update the room to reserved and get the reserved by.
*
* @since
* @author
*/
public void reserveThisRoom() {
this.reserved = true;
System.out.println("Enter the name of the person reserving this room: ");
reservedBy = input.nextLine();
}
/**
* Update the room to not reserved and clear the reserved by.
*
* @since
* @author
*/
public void releaseThisRoom() {
this.reserved = false;
reservedBy = "";
System.out.println("Room has been released ");
}
public String toString() {
String output = " "
+ " Room Number: " + roomNumber
+ " Number of Seats: " + numberOfSeats
+ " Reserved By: " + reservedBy
+ " Reserved: " + reserved
+ " Smart Board: " + hasSmartBoard;
return output;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
RoomMain.java
package room.main;
import room.bo.ComputerRoom;
import room.bo.BiologyLab;
import room.bo.BoardRoom;
import room.bo.Room;
import room.bo.RoomComparator;
import room.bo.SeatComparator;
import room.util.RoomUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* This main method will invoke functionality for booking college rooms.
*
* Detailed description:
* https://docs.google.com/document/d/1jyrvSJHXS6BZuXKVswYkmt2muBmPI71OxXTLQxerDVU/edit
*
* @author
*/
public class RoomMain{
private static final int ROOM_DOES_NOT_EXIST = -1;
private static ArrayList<Room> rooms = new ArrayList();
/**
* Main method controls program and user interface.
*
* @param args the command line arguments
* @since
* @author
*/
public static void main(String[] args) {
RoomUtil.createTestRooms(rooms);
Scanner input = new Scanner(System.in);
String menu = "------------------------ - CIS Room Booking "
+ "------------------------ "
+ "Choose an option: "
+ "1) Add Room "
+ "2) Reserve Room "
+ "3) Release Room "
+ "4) Show Rooms "
+ "5) Show Room Count Details "
+ "6) Search For Room "
+ "7) Exit";
int selection = 0;
while (selection != 7) {
System.out.println(menu);
selection = input.nextInt();
input.nextLine();
switch (selection) {
case 1:
addRoom();
break;
case 2:
reserveRoom();
break;
case 3:
releaseRoom();
break;
case 4:
showRooms();
break;
case 5:
break;
case 6:
break;
case 7:
System.out.println("Goodbye");
break;
default:
System.out.println("Invalid option.");
}
}
}
/**
* Loop through the rooms to check if the room already exists.
*
* @param roomNumber
* @return the index of the room number
* @since
* @author
*/
public static int getRoomNumberIfExists(int roomNumber) {
int index = -1;
for (int i = 0; i < rooms.size(); i++) {
if (rooms.get(i).getRoomNumber() == roomNumber) {
index = i;
}
}
return index;
}
/**
* This method will allow the user to add a new room to the collection of rooms.
*
*/
public static void addRoom() {
//***********************************************************
//Ask which room number the user wants to add
//***********************************************************
Room room = null;
Scanner input = new Scanner(System.in);
System.out.print("Enter room number: ");
int roomNumber = input.nextInt();
input.nextLine();
//***********************************************************
//Check to see if the room already exists
//***********************************************************
int roomNumberIndex = getRoomNumberIfExists(roomNumber);
//If the room does not already exist.
if (roomNumberIndex == ROOM_DOES_NOT_EXIST) {
roomNumberIndex = rooms.size();
boolean finished = false;
do {
System.out.print("What type of room is this? " + "1) Add Room "
+ "2) Computer Lab "
+ "3) Board Room "
+ "4) Biology lab ");
String choice = input.nextLine();
//***********************************************************
//Based on the user input, create the correct type of room.
//***********************************************************
switch (choice) {
case "1":
room = new Room();
finished = true;
break;
case "2":
room = new ComputerRoom();
finished = true;
break;
case "3":
room = new BoardRoom();
finished = true;
break;
case "4":
room = new BiologyLab();
finished = true;
break;
default:
System.out.println("Invalid option");
}
} while (!finished);
//Set the details for the room
room.setRoomNumber(roomNumber);
//Note the correct method will be invoked based on which type of room was created above.
room.getRoomDetailsFromUser();
//Add the room to the collection of rooms. Note that as long as an object 'is a' Room
//(all of the types of rooms above are rooms), then it can be added to the collection of
//rooms.
rooms.add(room);
} else {
String choice = "";
System.out.println("Room already exists. Do you want to continue? (Y/N)");
choice = input.nextLine();
//If the user wants to continue, invoke the method to change the value of attributes in
//the room
if (choice.equalsIgnoreCase("y")) {
rooms.get(roomNumberIndex).getRoomDetailsFromUser();
}
}
}
/**
* This method will allow the user to reserve a room.
*/
public static void reserveRoom() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the room number you would like to book");
int roomNumber = input.nextInt();
input.nextLine();
//Check to see if the room exists.
int roomNumberIndex = getRoomNumberIfExists(roomNumber);
if (roomNumberIndex < 0) {
System.out.println("This room does not exist");
} else {
//Put the room from the ArrayList into a local variable.
Room room = rooms.get(roomNumberIndex);
if (!room.isReserved()) {
room.reserveThisRoom();
} else {
System.out.println("This room is already booked!");
}
}
}
public static void releaseRoom() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the room number you would like to release");
int roomNumber = input.nextInt();
input.nextLine();
//Check if the room exists.
int roomNumberIndex = getRoomNumberIfExists(roomNumber);
if (roomNumberIndex < 0) {
System.out.println("This room does not exist");
} else {
//Put the room from the ArrayList into a local variable.
Room room = rooms.get(roomNumberIndex);
//If the room is reserved, allow them to release.
if (room.isReserved()) {
room.releaseThisRoom();
} else {
System.out.println("This room is not booked!");
}
}
}
/**
* Show the details for each room
*/
public static void showRooms() {
int option = 0;
Scanner input = new Scanner(System.in);
System.out.println("");
System.out.println("Enter the sort option:");
System.out.println("1) Sort by number of seats ascending 2) Sort by room number ascending");
option = input.nextInt();
switch(option) {
case 1:
Collections.sort( rooms,new SeatComparator()); /* The sort will call the compare method in SeatComparator class*/
break;
case 2:
Collections.sort( rooms,new RoomComparator()); /* The sort will call the compare method in RoomComparator class*/
break;
}
System.out.println("------------------------");
System.out.println("- Room List");
System.out.println("------------------------");
if(rooms.size() == 0){
System.out.println("There are no rooms.");
}
for (int i = 0; i < rooms.size(); i++) {
System.out.println(rooms.get(i));
}
System.out.println("");
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
SeatComparator.java
package room.bo;
import java.util.Comparator;
public class SeatComparator implements Comparator<Room> {
@Override
public int compare(Room obj1, Room obj2) {
/* This method sorts the list based on number of seats in ascending order*/
return ((Integer)obj1.getNumberOfSeats()).compareTo((Integer)obj2.getNumberOfSeats());
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
RoomComparator.java
package room.bo;
import java.util.Comparator;
public class RoomComparator implements Comparator<Room> {
@Override
public int compare(Room obj1, Room obj2) {
/* This method sorts the list based on room number in ascending order*/
return ((Integer)obj1.getRoomNumber()).compareTo((Integer)obj2.getRoomNumber());
}
}