In: Computer Science
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.
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();
}
}