In: Computer Science
Use the code below as the basis for a hotel reservation system (HRS). The customer calls the hotel front desk to book a room. The front desk is running your software on one of its computers. The basic operations available are:
The people at the front desk choose a particular operation, typically in response to a phone call.
Start by modularizing the existing program code below as follows. Create a method from the welcome message (and call this new method from main). Next, create a method from the end message (and also call it from main). Finally, create a method from the help information (and call from main too).
Now we'll implement the methods of the HRS. A new method, bookOneRoom, should randomly book one room when 'b' or 'B' is pressed. Once a room is chosen, it should call bookARoom to indicate that the room is now booked. A new method, bookTwoRooms, should randomly book two rooms when '2' is pressed. It should call bookARoom (twice). Also, write a method that lists (prints out) the room numbers of all of the unoccupied rooms (when the user presses 'l' - lowercase L). Similarly, write a method that lists (prints out) the room nummbers of all of the occupied rooms (when 'L' is entered). Finally, write a method that reports the percentage of the hotel rooms that are unoccupied. This method should use the existing occupiedCount method.
Note: Every method should be preceeded by a comment that explains what the method does.
Summary of what to submit:
A properly commented, indented, and working program.
/*
file: HRS.java
author: < your-name-here >
desc.: Hotel Reservation System (HRS)
*/
import java.util.Scanner;
class HRS {
final static int N = 101;
final static boolean Occupied = true;
//------------------------------------------------------------
//program initialization
static void initialize ( boolean[] rooms ) {
for (int i=0; i<rooms.length; i++)
rooms[i] = !Occupied;
}
//------------------------------------------------------------
//book a particular room
static void bookARoom ( int rm, boolean[] rooms ) {
assert( rooms[rm] == !Occupied ); //cause boom if bad
rooms[ rm ] = Occupied;
}
//------------------------------------------------------------
//determine how many rooms are occupied in our hotel
static int occupiedCount ( boolean[] rooms ) {
int count = 0;
for (int i=1; i<rooms.length; i++) {
if (rooms[i]) ++count;
}
return count;
}
//------------------------------------------------------------
//it all starts here
public static void main ( String[] args ) {
//say hello
System.out.println();
System.out.println( "Welcome to HRS (Hotel Reservation System)." );
System.out.println();
boolean[] hotel = new boolean[ N ];
initialize( hotel );
Scanner kbd = new Scanner( System.in );
boolean timeToQuit = false;
while (!timeToQuit) {
//prompt for and read a command
System.out.print( "HRS --> " );
String command = kbd.nextLine();
//process the command
if (command.equalsIgnoreCase("q")) {
timeToQuit = true;
} else if (command.equals("?")) {
System.out.println();
System.out.println( "Enter..." );
System.out.println( " 2 to book two adjoining rooms" );
System.out.println( " b/B to book a single room" );
System.out.println( " l to list all unoccupied rooms" );
System.out.println( " L to list all occupied rooms" );
System.out.println( " h/H/? for help" );
System.out.println( " p/P for percent unoccupied" );
System.out.println( " q/Q to quit" );
System.out.println();
} else {
System.out.println( " Please enter a valid command." );
}
}
//say goodbye
System.out.println();
System.out.println( "Thanks for using HRS!" );
System.out.println();
}
}
//Java code
/*
file: HRS.java
author: < your-name-here >
desc.: Hotel Reservation System (HRS)
*/
import java.util.Random;
import java.util.Scanner;
class HRS {
final static int N = 101;
final static boolean Occupied = true;
//------------------------------------------------------------
//program initialization
static void initialize ( boolean[] rooms ) {
for (int i=0; i<rooms.length; i++)
rooms[i] = !Occupied;
}
//------------------------------------------------------------
//book a particular room
static void bookARoom ( int rm, boolean[] rooms ) {
if( rooms[rm] == !Occupied ) //cause boom if bad
{
rooms[rm] = Occupied;
System.out.println("Room No."+(rm+1)+" has been booked successfully");
}
else
{
System.out.println("Room is already occupied");
}
}
//------------------------------------------------------------
//determine how many rooms are occupied in our hotel
static int occupiedCount ( boolean[] rooms ) {
int count = 0;
for (int i=1; i<rooms.length; i++) {
if (rooms[i]) ++count;
}
return count;
}
/**
* Print Welcome Message
*/
private static void welcomeMessage()
{
System.out.println();
System.out.println( "Welcome to HRS (Hotel Reservation System)." );
System.out.println();
}
/**
* Print the display message
*/
private static void endMessage()
{
System.out.println();
System.out.println( "Thanks for using HRS!" );
System.out.println();
}
/**
* help function
*/
private static void help()
{
System.out.println();
System.out.println( "Enter..." );
System.out.println( " 2 to book two adjoining rooms" );
System.out.println( " b/B to book a single room" );
System.out.println( " l to list all unoccupied rooms" );
System.out.println( " L to list all occupied rooms" );
System.out.println( " h/H/? for help" );
System.out.println( " p/P for percent unoccupied" );
System.out.println( " q/Q to quit" );
System.out.println();
}
/**
* bookOneRoom, should randomly book one room when 'b' or 'B'
* is pressed. Once a room is chosen,
* it should call bookARoom to indicate that the room is now booked.
*/
private static void bookOneRoom(boolean[] rooms)
{
Random random = new Random();
int randomRoom = random.nextInt(100);
bookARoom(randomRoom,rooms);
}
/**
* A new method, bookTwoRooms, should randomly book
* two rooms when '2' is pressed. It should call bookARoom (twice).
*/
private static void bookTwoRooms(boolean[] rooms)
{
bookOneRoom(rooms);
bookOneRoom(rooms);
}
/**
* write a method that lists (prints out) the room numbers of all of the
* unoccupied rooms (when the user presses 'l' - lowercase L).
*/
private static void listAllUnoccupiedRooms(boolean[] rooms)
{
for (int i = 0; i <rooms.length ; i++) {
if(!rooms[i])
{
System.out.println("Room No.: -> "+(i+1)+" is not occupied." );
}
}
}
/**
* lists (prints out) the room nummbers of
* all of the occupied rooms (when 'L' is entered).
*/
private static void listAllOccupiedRooms(boolean[] rooms)
{
for (int i = 0; i <rooms.length ; i++) {
if(rooms[i])
{
System.out.println("Room No.: -> "+(i+1)+" is occupied." );
}
}
}
/**
* method that reports the percentage of the hotel rooms that are unoccupied.
*/
private static void getPercentageOfUnoccupiedRooms(boolean[] rooms)
{
int occ = occupiedCount(rooms);
int unOcc = N-occ;
double per = (unOcc/(double)N)*100;
System.out.println("% of unoccupied rooms: "+ String.format("%.2f",per)+"%");
}
//------------------------------------------------------------
//it all starts here
public static void main ( String[] args ) {
new HRS();
//say hello
welcomeMessage();
boolean[] hotel = new boolean[ N ];
initialize( hotel );
Scanner kbd = new Scanner( System.in );
boolean timeToQuit = false;
while (!timeToQuit) {
//prompt for and read a command
System.out.print( "HRS --> " );
String command = kbd.nextLine();
switch (command)
{
case "H":
case "h":
case "?":
help();
break;
case "Q":
case "q":
timeToQuit = true;
break;
case "2":
bookTwoRooms(hotel);
break;
case "b":
case "B":
bookOneRoom(hotel);
break;
case "l":
listAllUnoccupiedRooms(hotel);
break;
case "L":
listAllOccupiedRooms(hotel);
break;
case "P":
case "p":
getPercentageOfUnoccupiedRooms(hotel);
break;
default:
System.err.println("ERROR!!! ... Please enter a valid choice..");
break;
}
/**
//process the command
if (command.equalsIgnoreCase("q")) {
timeToQuit = true;
} else if (command.equals("?")) {
help();
} else {
System.out.println( " Please enter a valid command." );
}
*/
}
//say goodbye
endMessage();
}
}
//Output

//If you need any help regarding this solution ....... please leave a comment ......... thanks