QuestionQuestion

Build a hotel reservation application and use multithreading to simulate multiple customers simultaneously attempting to reserve the room

You are given a class called Hotel. The hotel tracks the current reservations for your hotel for the month of December only. So it only maintains reservations for the 1st through 31st of December of the one room.
It contains the following methods.
• boolean requestReservation(String user, int firstDay, int lastDay)
o Attempt to make a reservation from the firstDay to the lastDay (inclusive) for the person specified by name. If firstDay == lastDay that is ok and is a one day reservation.
o Restrictions: a reservation will not be made if…
▪ The user already has a reservation
▪ If the values for firstDay or lastDay are invalid in any way.
▪ any of the requested days are available
▪ the days don’t make sense (lastDay is before firstDay)
o Return true if the reservation was made, false if it was not
• boolean cancelReservation(String user)
o If the specified person has a reservation, it will be cancelled (all of their days become available) and returns true
o Otherwise, returns false if they did not have a reservation to cancel
• String reservationInformation()
o Returns a string containing the full reservation information for the month. For each day displays either available or the name of the person who has the reservation for that day.
Exercise 1
Write an automated PeriodicCustomer class that implements Runnable. Your customer should have a name and should test your hotel’s commands (The customer will need a reference to the hotel object). Don’t involve user input, just test all of the commands ensuring that everything is working as it should.
• The client should periodically do one of the following. Each time choose randomly between the two.
(So, the first action may be cancel, even though that doesn’t make sense)
o Make a new reservation starting on a random day for a random number of days. (Make sure that the reservation is theoretically possible e.g. don’t request a 60 day reservation.)
o Cancel current reservations. .
• After doing so, print a message to the console stating what happened. The message should always include the customer’s name.
o Reservation made: Chris from 3 through 5
o Reservation unsuccessful: Chris from 3 through 5
o Reservations successfully canceled for Chris
o Reservations not canceled for Chris, no current reservation.
• The Customer should “shut down” if it has been interrupted. (It’s ok to just check for the exception created by the call to sleep) Display a message to the console such as "Periodic Test Customer Chris Shutting Down" before the run method exits.
Exercise 2
Create a HotelTester class that will create at least three new threads, each with its own customer object. After the program has been running long enough for the customers to attempt several actions, interrupt all of the threads. Ensure that all of the threads have shut down, and then print out the current reservation information
for the hotel.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.ArrayList;

/**
* A class that represents a one room hotel and allows reservations to be made
* and cancelled for the month of December.
*
* @author
*
*/
public class Hotel {
private final int DAY_COUNT = 31;
private ArrayList<String> reservations;

public Hotel() {
reservations = new ArrayList<String>(DAY_COUNT);
for (int i = 0; i < DAY_COUNT; i++) {
reservations.add(null);
}
}

/**
* Displays the full set of reservation information. Each day is marked with
* either the user with the reservation or Available
*/
public String toString() {
String out = "Hotel Reservation Info\n__________________\n";

for (int i = 0; i < DAY_COUNT; i++) {
out += (i + 1) + ": ";
if (reservations.get(i) == null) {
out += "Available\n";
} else {
out += reservations.get(i) + "\n";
}
}
return out...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$16.10
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats