Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Help with Java program that uses: Decision constructs Looping constructs Basic operations on an Array of objects (find, change, access all elements). More than one
Help with Java program that uses:
Decision constructs
Looping constructs
Basic operations on an Array of objects (find, change, access all elements).
More than one class and has multiple objects
- Develop a simple Hotel program. We will have two classes, a Hotel class representing an individual hotel and a Room class. The Hotel class will contain several Room objects and will have several operations. We will also have a driver program to test the Hotel class.
- Build a Hotel class that will store information about a Hotel. It will include a name and location. It should also include an Array of instances of class Room to hold information about each room. This Array of Room objects may be a partially filled array, thus it must also have an integer to track how many rooms are in the hotel.
- The program must use a standard Java Array. The Array should be able to hold up to 10 Room objects.
- Build a HotelTest class to test your application.
- Your test class should not require any interaction with the user.
- It should verify the correct operation of the constructor and all public methods in the Hotel class.
- Create at least 5 Rooms in the Hotel.
- Display output info for the hotel - should roughly match what is given below under Sample Hotel Output. This should be done by using the toString methods you create.
- "Book" several rooms, that is, set it's status to occupied.
- Be sure to test unsuccessful bookings as well, no matching room exists, or matching room is occupied.
- Verify that the hotel info has been updated.
- Be sure that you are testing all of the methods in both the Hotel and Room class. This can include indirect testing where a method is called by another method that does get tested directly.
Room
- Instance Variables
- roomNumber: int - number of the room
- bedType: String - type of bed(ie king, queen etc)
- rate: int - cost of the room
- occupantName: String - name of occupant, or the empty String ("") if not occupied
- Constructors
- Default constructor sets all instance variables to a default value
- Parameterized Constructor
- Takes in room number, bed type, and room rate, as parameters and sets the appropriate instance variable to those values
- Sets the occupants name to the empty String ("");
- Methods
- isOccupied - method returns true if the room is occupied, false otherwise.
- toString
- Provides all the details of a room - room number, name of guest (if occupied), bed type, and rental rate in a nicely formatted manner such as that given in Sample Room Output below.
- One attribute on each line is preferred.
Room Number: 101 Occupant name: Not Occupied Bed Type: queen Rate: 100 Hotel
- Instance Variables
- rooms: An Array of Room Objects
- name: String - hotel name
- location: String - hotel location
- numberOfRooms: int number of rooms in the hotel.
- private static final int NOT_FOUND = -1;: this is similar to a constant in other languages and is used here to indicate that a search was unsuccessful.
- Constructors
- Should use mutator methods to set all instance variable.
- A default constructors that sets the Array to a size of 10 and all instance fields to a default value.
- Parameterized constructor
- Takes in parameters for name and location and sets those instance variables to the parameter values.
- Assigns numberOfRooms to zero. numberOfRooms indicates how many rooms are in the hotel. It will create a 10 element array.
- Methods
- isFull - returns a boolean that is true if all the rooms in the hotel are occupied.
- addRoom - returns true if room is added, false if not.
- Takes 3 arguments: the room number, bed type, and room rate.
- If numberOfRooms is less than the capacity of rooms:
- Create a Room object from the parameters list.
- Adds the Room object to rooms.
- Increments numberOfRooms (indicates how many actual rooms are in the hotel).
- Returns true.
- Otherwise, returns false since no more rooms can be added.
- addReservation
- Takes two arguments: the occupant's name and the requested bed type.
- Searches the array of rooms for one that matches the bed type. Remember to use the partially filled array value numberOfRooms in your search loop.
- If an unoccupied room with the correct attributes is found, the occupant's name will be set and the occupied attribute will be set to true. In either case a message will be printed that will state whether or not the reservation was made.
- findReservation
- Private method - only accessible from other method sn the class.
- Takes a String arguments representing the occupant's name.
- Searches the occupied rooms for a reservation with that occupant name.
- Returns the index of the room or NOT_FOUND if not found.
- cancelReservation
- Takes in a string representing the name of a guest.
- If a room is found to be occupied by the specified guest, update the occupantName and isOccupied values for the room. If multiple rooms exist with the same occupant, you are free to either clear only the first match, or all.
- In either case, output a message stating whether or not the reservation was cancelled.
- This method should utilize the private utility method findReservation() to scan the list of rooms looking for a room by guest name.
- The findReservation method will return the index of the room in the Array of rooms or NOT_FOUND if the room is not found.
- toString
- Returns a nicely formatted string giving hotel and room details.
- Calls the Room class's toString method.
-
Sample Hotel Output
Hotel Name: Beach Marriot Location: Pensacola Number of Rooms : 5 Current revenue: $198 =========================== Room Number: 101 Occupant name: Not Occupied Bed Type: queen Rate: 100 Room Number: 102 Occupant name: Popeye Bed Type: king Rate: 110 Room Number: 103 Occupant name: Donald Duck Bed Type: king Rate: 88 Room Number: 104 Occupant name: Not Occupied Bed Type: twin Rate: 100 Room Number: 105 Occupant name: Not Occupied Bed Type: queen Rate: 99
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started