Question: Part 1 : Room.java Define the Room class to meet the following criteria: 3 fields, all public: String owner, int number, and boolean reserved. Non
Part : Room.java
Define the Room class to meet the following criteria:
fields, all public: String owner, int number, and boolean reserved.
Nondefault constructor that has an int roomNumber as parameter. Set number to roomNumber, as well as setting owner to NA and reserved to false.
A method public void reserveString name It should first check if the room is already reserved, and if so report that to the user and return; out. If not, update reserved to true, update owner to name, and report to the user that the reservation was successful.
A method public void unreserveString name It should first check if the room is already unreserved, and if so report that to the user and return; out. Next, it should check if name is the same as owner, so that only the rooms owner can unreserve the room. If they dont match, tell them that and return; out. If they do match, you can update reserved to be false, reset owner back to NA and report the successful unreservation to the user.
A method public void printInfo All it needs to do is print out preferably on one line for cleanliness the rooms number, reservation status, and owner.
Depending on how you style your code, the length of this class will vary my version is around lines, for comparisons sake.
Part : Hotel.java
Define the Hotel class to meet the following criteria:
fields: private Room rooms and public String currentUser. rooms represents all the rooms in this hotel, while currentUser is used to track what name to use for requests to reserveunreserve
Nondefault constructor that has an int roomCount and String name as parameters. First, set currentUser to name, and set rooms to be a new Room with roomCount as the size. Then, use a for loop to fill the indexes of rooms with new Room objects, using i as the room number eg rooms would get a new Room with number set to
A method public void reserveRoomScanner keyboard Start by asking the user for which room number they want to reserve, and then make sure that number is valid in range If not, report the invalid number and return; out. Additionally, you need to flush the stream after the nextInt call here. If the room number is fine, use a foreach loop like forRoom room : rooms to look through all the rooms for the one they requested. When the matching room number is found, you can say room.reservecurrentUser to use the reserve method you made in part and break; after so that the loop stops after reserving it
A method public void unreserveRoomScanner keyboard It should be identical to reserveRoom including flushing the stream after asking for room number but calling room.unreservecurrentUser on the appropriate room instead of reserve.
A method public void viewRooms All it needs is a similar foreach loop to reserve and unreserve, but it doesnt need to check for any room numbers. It should simply call room.printInfo on every room.
A method public void updateNameScanner keyboard It should ask the user to enter a new username for the program to use, so that you can simulate logging in as another hotel guest. Update the value of currentUser to whatever they type in here.
Again, for comparisons sake, Ill mention that my implementation of this class is exactly lines long, but yours will vary depending on how you spacestyle your code.
Part : HotelDemo.java
With lab I underestimated how difficult the demo file would be for most people, so project s is a bit toned down. Nearly all the heavy lifting in this project is done by Room and Hotel, leaving this part to purely be what allows the user to choose which operation to do
Start by setting up a Scanner object and asking the user for their name that will be used to reserve rooms theyll be able to change it later Next, ask how many rooms they want this simulated hotel to contain. Using those pieces of data, you can then create the programs new Hotel object. You need to flush the stream after asking for the room count, since nextInt nextLine is one of the cases that causes problems with Scanner.
After that, youll want to set up an infinite while loop whiletrue that repeatedly asks the user to choose from a menu prompt of options. The fact that it is infinite is OK because one of the options is to exit the loop. The menu prompt should look something like:
MENU
: View all rooms.
: Reserve a room.
: Unreserve a room.
: Change current user.
: Exit simulation.
Choice:
After the menu is shown, ask them for their choice and do the appropriate action via an ifelse tree. Option should lead to calling hotel.viewRooms option should be hotel.reserveRoomkeyboard option should be hotel.unreserveRoomkeyboard option should be hotel.updateNamekeyboard and option should be a break; You can handle unrecognized input via an else case.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
