Question: Part 1 : Room.java Define the Room class to meet the following criteria: 1 ) 3 fields, all public: String owner, int number, and boolean

Part 1: Room.java
Define the Room class to meet the following criteria:
1)3 fields, all public: String owner, int number, and boolean reserved.
2) Non-default constructor that has an int roomNumber as parameter. Set number to
roomNumber, as well as setting owner to N/A and reserved to true.
3) A method public void reserve(String 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.
4) A method public void unreserve(String 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 N/A, and report the
successful unreservation to the user.
5) 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
44 lines, for comparisons sake.
Part 2: Hotel.java
Define the Hotel class to meet the following criteria:
1)2 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 reserve/unreserve.
2) Non-default 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+1 as the room number (e.g. rooms[0] would get a new Room with number
set to 1).
3) A method public void reserveRoom(Scanner 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 for(Room room : rooms)) to look through all the rooms for the
one they requested. When the matching room number is found, you can say
room.reserve(currentUser) to use the reserve method you made in part 1 and
break; after so that the loop stops after reserving it.
4) A method public void unreserveRoom(Scanner keyboard). It should be identical to
reserveRoom (including flushing the stream after asking for room number), but
calling room.unreserve(currentUser) on the appropriate room instead of reserve.
5) 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.
6) A method public void updateName(Scanner 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 60 lines
long, but yours will vary depending on how you space/style your code.
Part 3: HotelDemo.java
With lab 2, I underestimated how difficult the demo file would be for most people, so project 1s
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 2 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 (while(true)) 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
---------
1: View all rooms.
2: Reserve a room.
3: Unreserve a room.
4: Change current user.
5: Exit simulation.
Choice:
After the menu is shown, ask them for their choice and do the appropriate action via an if-else
tree. Option 1 should lead to calling hotel.viewRooms(), option 2 should be
hotel.reserveRoom(keyboard), option 3 should be hotel.unreserveRoom(keyboard), option 4
should be hotel.updateName(keyboard), and option 5 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!