Question

In: Computer Science

Operation  This application calculates the charges for a stay at a hotel based on the...

Operation
 This application calculates the charges for a stay at a hotel based on the arrival and departure dates.
 The application begins by prompting the user for the month, day, and year of the arrival and the departure.
 Next, the application displays the arrival date, the departure date, the room rate, the total price, and the number of nights.

Specifications
 Create a class named Reservation that defines a reservation. This class should contain instancevariables for the arrival date and departure date. It should also contain a constant initialized to thenightly rate of $115.00.
 The Reservation class should contain a constructor that accepts the arrival and departure dates asparameters of type Date, as well as methods that return the number of nights for the stay (calculatedby subtracting the arrival date from the departure date) and the total price (calculated by multiplyingthe number of nights for the stay by the nightly room rate). This class should also override thetoString method to return a string like this:

Arrival Date: Monday, May 16, 2005Departure Date: Wednesday, May 18, 2005Price: $115.00 per nightTotal price: $230.00 for 2 nights
 The main method for the application class should contain a loop that asks the user for the arrival anddeparture date information, creates a Reservation object, and displays the string returned by thetoString method.
 Assume valid data is entered.

Enhancements
 Add validation so the user must enter values that will result in a correct date. Allow the user to enter the date in the form mm/dd/yyyy. Allow the user to enter the room rate or select the rate from one of several available rates. Use the BigDecimal class rather than the double type for the price calculation.

Note
This application requires the use of the GregorianCalendar class to create dates from the int valuesentered by the user. Then, it requires the use of the getTime method to convert the GregorianCalendarobject to a Date object.

Solutions

Expert Solution

Here is the program with date validation enhancement.Instead of asking int values for the day,month and year, user enters in the required format specified in the enhancement section of the question. comments are inline. Also a sample run of the program is attached. Please do rate the answer.

===================================
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

//A reservation class accepts date of arrival and departure and gives no of nights and also displays the price and other details in toString()
public class Reservation {
   //The fixed nightly rate
   static final int NIGHTLY_RATE=115;
  
   //A date formatter allows to format date as per our requirement. EEEE stands for expanded day name, MMM is 3 letter month and d is for date, and yyyy is year
   static final SimpleDateFormat FORMATTER=new SimpleDateFormat("EEEE,MMM d,yyyy");
  
   Date arrival,departure;
   //Constructor
   public Reservation(Date arr,Date dep)
   {
       arrival=arr;
       departure=dep;
   }
  
  
   public int getNights()
   {
       //This method calculates the difference between departure and arrival date in terms of days
       long diff=departure.getTime()-arrival.getTime();
       return (int)TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
  
   }
  
   public String toString()
   {
       return "Arrival Date: "+FORMATTER.format(arrival)+
               " Departure Date:"+FORMATTER.format(departure)+
               " Price:$"+NIGHTLY_RATE+" per night"+
               " Total price:$"+getNights()*NIGHTLY_RATE+" for "+getNights()+" nights";
      
      
   }
   public static void main(String[] args) {
       String date,d;
       Scanner scanner=new Scanner(System.in);
      
       SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
       sdf.setLenient(false); //to validate the dates entered are in the desired format
      
       String answer;
       Date arr,dep;
      
       //keep looping as long as user wants to enter another reservation details
       do{
           while(true)
           {
               System.out.println("Enter the date of arrival (mm/dd/yyyy):");
              
               date=scanner.nextLine();
               try {
                   arr= sdf.parse(date); //will throw exception if date is not in the format specified, in case of exception, loop back to allow user to enter correct date
                   break;
               } catch (Exception e) {
                   System.out.println("Invalid date format!");
               }
           }
          
           while(true)
           {
               System.out.println("Enter the date of departure (mm/dd/yyyy):");
              
               date=scanner.nextLine();
                 
               try {
                   dep= sdf.parse(date);
                  
                   break;
               } catch (ParseException e) {
                   System.out.println("Invalid date format!");
               }
           }
          
           //create the new reservation object and display using toString()
           Reservation res=new Reservation(arr, dep);
           System.out.println(res.toString());
          
           System.out.println("Enter another reservation? (yes/no) :");
           answer=scanner.nextLine();
       }while(answer.equals("yes") || answer.equals("YES"));
       scanner.close();
      
   }

}


Related Solutions

How can we write an application that calculates connascence of an application?
How can we write an application that calculates connascence of an application?
A hotel manager claims that the average of the pricethat customer will pay to stay...
A hotel manager claims that the average of the price that customer will pay to stay at their hotel is 500. At α = 0.01 is the claim realistic? The data on the price per night for a sample of six rooms in the hotel are shown.Price pernight 713   300    292    311    598    401    618Give a reason why the claim might be deceptive.
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel....
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations,...
Write a program that calculates the occupancy rate for each floor of a hotel. (Use a...
Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations, the program...
Avila Hotel has 300 rooms and charges $120 per room. The hotel estimates that the demand...
Avila Hotel has 300 rooms and charges $120 per room. The hotel estimates that the demand for the rooms follows a uniform distribution between 270 and 330 each day. Reservations may be canceled by 6pm with no penalty. The hotel estimates that 3% of the reservations are canceled by 6pm. Hence, the hotel allows overbooking by 20 room (the hotel allows up to 320 customers to make reservation). When more customers arrive than can be handled (e.g., more than 300...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy...
Using Loops for the Hotel Occupancy calculator. You will write a program that calculates the occupancy of a hotel. Rules: 1# The hotel must have more than 2 floors and less than or equal 5 floors. 2# Each floor in the hotel can have a different number of rooms on the floor. 3# You must set the number of occupied rooms. Again, there must less rooms occupied than the number of rooms. 4# Using the total number of rooms and...
Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has.
Use JAVAProgram #1: Hotel Occupancy: Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A for loop should then iterate once for each floor. In each iteration of the for loop, the program should ask the user for the number of rooms of the floor and how many of them are occupied. After all of the iterations are complete the program should display how...
A hotel has 210 units. All rooms are occupied when the hotel charges ​$90 per day...
A hotel has 210 units. All rooms are occupied when the hotel charges ​$90 per day for a room. For every increase of x dollars in the daily room​ rate, there are x rooms vacant. Each occupied room costs $ 36 per day to service and maintain. What should the hotel charge per day in order to maximize daily​ profit?
Application: Elasticity and hotel rooms. The following graph input tool shows the daily demand for hotel...
Application: Elasticity and hotel rooms. The following graph input tool shows the daily demand for hotel rooms at the Big Winner Hotel and Casino in Las Vegas, Nevada. To help the hotel management better understand the market, an economist identified three primary factors that affect the demand for rooms each night. These demand factors, along with the values corresponding to the initial demand curve, are shown in the following table and alongside the graph input tool. Demand Factor Initial Value...
Create an application that calculates and displays the amount of a homeowner’s property tax. The tax...
Create an application that calculates and displays the amount of a homeowner’s property tax. The tax is 1.35% of the property’s assessed value, which will be entered by the user. a. Prepare a Planning Chart for the application. b. Draw a sketch of an appropriate interface. Be sure to follow the GUI design guidelines covered in the chapter. The guidelines are summarized in Figure 2-20. (If you want to include an image in the interface, you can either use your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT