Question

In: Computer Science

Use the code below as the basis for a hotel reservation system (HRS). The customer calls...

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:

  1. book one room,
  2. book two adjoining rooms,
  3. list all unoccupied rooms,
  4. list all occupied rooms,
  5. help lists information about available methods,
  6. report percent of hotel rooms that are unoccupied,
  7. quit the system (shut the system down).

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();
    }
}

Solutions

Expert Solution

//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


Related Solutions

using c++ You have been contracted to build a hotel reservation system. This is the system...
using c++ You have been contracted to build a hotel reservation system. This is the system that hotel desk clerks will use to take desk reservations. Assumptions: This is acting as though the only reservations taken are at the desk. All reservations are for one night. The hotel is empty when you start the program. First, this program will give the full inventory of rooms in the hotel. Total rooms: 122 There are four types of rooms with these prices:...
For this assignment you will be creating a basic Hotel Reservation System. The program must meet...
For this assignment you will be creating a basic Hotel Reservation System. The program must meet the following guidelines: User can reserve up to 3 rooms at a time Your program will need to loop through to continue to ask needed questions to determine cost. Room rates (room variants): Suite $250 2 Queens $150 1 King $175 Ocean view add $50 Fridge for Room $25 Pets additional $50 Sales tax rate will be 5.5% Your program will show out the...
Create a Database Schema for a hotel reservation system. indicate the Primary Keys, Foreign Keys, and...
Create a Database Schema for a hotel reservation system. indicate the Primary Keys, Foreign Keys, and the one-to-one or one-to-many relationships between the tables. Also describe in a small paragraph the scope of the back-end database, by explaining the different tables and their relations in your schema.
Railway Reservation System is a system used for booking railway tickets over the Internet. Any Customer...
Railway Reservation System is a system used for booking railway tickets over the Internet. Any Customer can book tickets for different trains. The Customer can book a ticket only if it is available. Thus, he first checks the availability of tickets, and then if the tickets are available he books the tickets after filling details in a form. The Customer can print the form when booking a ticket. Note that to book a ticket the customer has to checkout by...
Java homework problem. This is my hotel reservation system. I'm trying to add a few things...
Java homework problem. This is my hotel reservation system. I'm trying to add a few things to it. You will be changing your Hotel Reservation system to allow a user to serve more rooms and the rooms will be created as objects. For this project you will be modifying the Hotel Reservation system to allow a user to serve more rooms and the rooms will be created as objects. You will be create a Room object that will allow the...
bus reservation system code using c language in this system we can add seat and remove....
bus reservation system code using c language in this system we can add seat and remove. this code will be in c language using 2 d aray
What is the future Customer Relationship Management (CRM) that Hilton's Hotel will use ?
What is the future Customer Relationship Management (CRM) that Hilton's Hotel will use ?
discuss with salespersons about their prospecting system they use in planning their sales calls. (Subject: sales...
discuss with salespersons about their prospecting system they use in planning their sales calls. (Subject: sales management)
Design a system to automatically check temperature and facial mask use of a customer, dispense hand...
Design a system to automatically check temperature and facial mask use of a customer, dispense hand sanitiser and unlock the door if these conditions are satisfied, and sanitize the door handle after a customer enters or exits the building. Provide programming codes in a python language
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a...
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a loop with indirect or indexed addressing to reverse the elements of an integer array in place. Do not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF operators to make the program as flexible as possible if the array size and type should be changed in the future. .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD .data    ; define your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT