package reservation;
import java.util.*; //For Scanner Class
import java.time.*; //For LocalDate
import java.time.format.*; //For DateTimeFormatter
public class Reservation
{
double perNight = 105.00, finalAmt;
long noDays;
int m, d, y;
LocalDate std, end;
//Sets the Arrival date inputed by the user
void setArrivalDate(LocalDate arrivalDate)
{
std = arrivalDate;
}
//Sets the Departure date inputed by the user
void setDepartureDate(LocalDate departureDate)
{
end = departureDate;
}
//Returns the Arrival date
LocalDate getArrivalDate()
{
return std;
}
//Returns the Departure date
LocalDate getDepartureDate()
{
return end;
}
//Formats the Arrival date as per the requirement given question
String getArrivalDateFormatted()
{
String stdFormat;
stdFormat = \"Arrival Date: \" + std.getMonth() + \" \" + String.valueOf(std.getDayOfMonth())
+ \", \" + String.valueOf(std.getYear());
return stdFormat;
}
//Formats the Departure date as per the requirement given question
String getDepartureDateFormatted()
{
String stdFormat;
stdFormat = \"Departure Date: \" + end.getMonth() + \" \" +
String.valueOf(end.getDayOfMonth()) + \", \" + String.valueOf(end.getYear());
return stdFormat;
}
//Returns the Price per night as per the format
String getPricePerNightFormatted()
{
String pr = \"Price: $\" + String.valueOf(perNight) + \" per night\";
return pr;
}
//Returns the number of nights
long getNumberOfNights()
{
//toEpochDay() returns number of days
noDays = end.toEpochDay() - std.toEpochDay();
return noDays;
}
//Calculates final Amount to be paid
double getTotalPrice()
{
finalAmt = (double)getNumberOfNights() * perNight;
return finalAmt;
}
//Returns the Total amount to be paid as per the format given in the question
String getTotalPriceFormatted()
{
String finA = \"Total price: $\" + String.valueOf(getTotalPrice()) + \" for \" +
String.valueOf(getNumberOfNights()) + \" night\" ;
return finA;
}
//Accepts the Arrival date and Departure date from console
void accept()
{
//Creates object of LocaleDate
LocalDate std1, end1;
//Creates the Date Time Formater object and applies Format Style
//SHORT in mm/dd/yy format
DateTimeFormatter fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
//Creates Scanner class object to accept data from console
Scanner s = new Scanner(System.in);
System.out.println(\"Enter starting date mm/dd/yyyy: \");
System.out.println(\"Enter the arrival month (1-12):\");
m = s.nextInt(); //Accept data in integer format
System.out.println(\"Enter the arrival day (1-31):\") ;
d = s.nextInt();
System.out.println(\"Enter the arrival year:\") ;
y = s.nextInt();
//Initializes the LocalDate object for Arrival date
std1 = LocalDate.of(y, m, d);
//Sets the SHORT date format
fmt.format(std1);
//Assigns Arrival date to class data member
setArrivalDate(std1);
System.out.println(\"Enter ending date mm/dd/yyyy: \");
System.out.println(\"Enter the departure month (1-12):\");
m = s.nextInt(); //Accept data in integer format
System.out.println(\"Enter the departur.
package reservation; import java.util.; For Scanner Class .pdf
1. package reservation;
import java.util.*; //For Scanner Class
import java.time.*; //For LocalDate
import java.time.format.*; //For DateTimeFormatter
public class Reservation
{
double perNight = 105.00, finalAmt;
long noDays;
int m, d, y;
LocalDate std, end;
//Sets the Arrival date inputed by the user
void setArrivalDate(LocalDate arrivalDate)
{
std = arrivalDate;
}
//Sets the Departure date inputed by the user
void setDepartureDate(LocalDate departureDate)
{
end = departureDate;
}
//Returns the Arrival date
LocalDate getArrivalDate()
{
return std;
}
//Returns the Departure date
LocalDate getDepartureDate()
{
return end;
2. }
//Formats the Arrival date as per the requirement given question
String getArrivalDateFormatted()
{
String stdFormat;
stdFormat = "Arrival Date: " + std.getMonth() + " " + String.valueOf(std.getDayOfMonth())
+ ", " + String.valueOf(std.getYear());
return stdFormat;
}
//Formats the Departure date as per the requirement given question
String getDepartureDateFormatted()
{
String stdFormat;
stdFormat = "Departure Date: " + end.getMonth() + " " +
String.valueOf(end.getDayOfMonth()) + ", " + String.valueOf(end.getYear());
return stdFormat;
}
//Returns the Price per night as per the format
String getPricePerNightFormatted()
{
String pr = "Price: $" + String.valueOf(perNight) + " per night";
return pr;
}
//Returns the number of nights
long getNumberOfNights()
{
//toEpochDay() returns number of days
noDays = end.toEpochDay() - std.toEpochDay();
return noDays;
}
//Calculates final Amount to be paid
3. double getTotalPrice()
{
finalAmt = (double)getNumberOfNights() * perNight;
return finalAmt;
}
//Returns the Total amount to be paid as per the format given in the question
String getTotalPriceFormatted()
{
String finA = "Total price: $" + String.valueOf(getTotalPrice()) + " for " +
String.valueOf(getNumberOfNights()) + " night" ;
return finA;
}
//Accepts the Arrival date and Departure date from console
void accept()
{
//Creates object of LocaleDate
LocalDate std1, end1;
//Creates the Date Time Formater object and applies Format Style
//SHORT in mm/dd/yy format
DateTimeFormatter fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
//Creates Scanner class object to accept data from console
Scanner s = new Scanner(System.in);
System.out.println("Enter starting date mm/dd/yyyy: ");
System.out.println("Enter the arrival month (1-12):");
m = s.nextInt(); //Accept data in integer format
System.out.println("Enter the arrival day (1-31):") ;
d = s.nextInt();
System.out.println("Enter the arrival year:") ;
y = s.nextInt();
//Initializes the LocalDate object for Arrival date
4. std1 = LocalDate.of(y, m, d);
//Sets the SHORT date format
fmt.format(std1);
//Assigns Arrival date to class data member
setArrivalDate(std1);
System.out.println("Enter ending date mm/dd/yyyy: ");
System.out.println("Enter the departure month (1-12):");
m = s.nextInt(); //Accept data in integer format
System.out.println("Enter the departure day (1-31):") ;
d = s.nextInt();
System.out.println("Enter the departure year:") ;
y = s.nextInt();
//Initializes the LocalDate object for Departure date
end1 = LocalDate.of(y, m, d);
//Sets the SHORT date format
fmt.format(std1);
//Assigns Departure date to class data member
setDepartureDate(end1);
}
public static void main(String[] args)
{
char ch;
System.out.println("Welcome to the Reservation Calculator ");
//Creates Reservation class object
Reservation cc = new Reservation();
Scanner s = new Scanner(System.in);
//Loops till Y is pressed
do
6. y
Enter starting date mm/dd/yyyy:
Enter the arrival month (1-12):
7
Enter the arrival day (1-31):
20
Enter the arrival year:
2016
Enter ending date mm/dd/yyyy:
Enter the departure month (1-12):
7
Enter the departure day (1-31):
27
Enter the departure year:
2016
Arrival Date: JULY 20, 2016
Departure Date: JULY 27, 2016
Price: $105.0 per night
Total price: $735.0 for 7 night
Would you like to continue? (y/n)
n
Solution
package reservation;
import java.util.*; //For Scanner Class
import java.time.*; //For LocalDate
import java.time.format.*; //For DateTimeFormatter
public class Reservation
{
double perNight = 105.00, finalAmt;
long noDays;
int m, d, y;
LocalDate std, end;
7. //Sets the Arrival date inputed by the user
void setArrivalDate(LocalDate arrivalDate)
{
std = arrivalDate;
}
//Sets the Departure date inputed by the user
void setDepartureDate(LocalDate departureDate)
{
end = departureDate;
}
//Returns the Arrival date
LocalDate getArrivalDate()
{
return std;
}
//Returns the Departure date
LocalDate getDepartureDate()
{
return end;
}
//Formats the Arrival date as per the requirement given question
String getArrivalDateFormatted()
{
String stdFormat;
stdFormat = "Arrival Date: " + std.getMonth() + " " + String.valueOf(std.getDayOfMonth())
+ ", " + String.valueOf(std.getYear());
return stdFormat;
}
//Formats the Departure date as per the requirement given question
String getDepartureDateFormatted()
8. {
String stdFormat;
stdFormat = "Departure Date: " + end.getMonth() + " " +
String.valueOf(end.getDayOfMonth()) + ", " + String.valueOf(end.getYear());
return stdFormat;
}
//Returns the Price per night as per the format
String getPricePerNightFormatted()
{
String pr = "Price: $" + String.valueOf(perNight) + " per night";
return pr;
}
//Returns the number of nights
long getNumberOfNights()
{
//toEpochDay() returns number of days
noDays = end.toEpochDay() - std.toEpochDay();
return noDays;
}
//Calculates final Amount to be paid
double getTotalPrice()
{
finalAmt = (double)getNumberOfNights() * perNight;
return finalAmt;
}
//Returns the Total amount to be paid as per the format given in the question
String getTotalPriceFormatted()
{
String finA = "Total price: $" + String.valueOf(getTotalPrice()) + " for " +
String.valueOf(getNumberOfNights()) + " night" ;
return finA;
}
9. //Accepts the Arrival date and Departure date from console
void accept()
{
//Creates object of LocaleDate
LocalDate std1, end1;
//Creates the Date Time Formater object and applies Format Style
//SHORT in mm/dd/yy format
DateTimeFormatter fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
//Creates Scanner class object to accept data from console
Scanner s = new Scanner(System.in);
System.out.println("Enter starting date mm/dd/yyyy: ");
System.out.println("Enter the arrival month (1-12):");
m = s.nextInt(); //Accept data in integer format
System.out.println("Enter the arrival day (1-31):") ;
d = s.nextInt();
System.out.println("Enter the arrival year:") ;
y = s.nextInt();
//Initializes the LocalDate object for Arrival date
std1 = LocalDate.of(y, m, d);
//Sets the SHORT date format
fmt.format(std1);
//Assigns Arrival date to class data member
setArrivalDate(std1);
System.out.println("Enter ending date mm/dd/yyyy: ");
System.out.println("Enter the departure month (1-12):");
m = s.nextInt(); //Accept data in integer format
System.out.println("Enter the departure day (1-31):") ;
d = s.nextInt();
10. System.out.println("Enter the departure year:") ;
y = s.nextInt();
//Initializes the LocalDate object for Departure date
end1 = LocalDate.of(y, m, d);
//Sets the SHORT date format
fmt.format(std1);
//Assigns Departure date to class data member
setDepartureDate(end1);
}
public static void main(String[] args)
{
char ch;
System.out.println("Welcome to the Reservation Calculator ");
//Creates Reservation class object
Reservation cc = new Reservation();
Scanner s = new Scanner(System.in);
//Loops till Y is pressed
do
{
cc.accept();
System.out.println(cc.getArrivalDateFormatted());
System.out.println(cc.getDepartureDateFormatted());
System.out.println(cc.getPricePerNightFormatted());
System.out.println(cc.getTotalPriceFormatted());
System.out.println("Would you like to continue? (y/n)");
ch = s.next().charAt(0);
if(ch == 'y' || ch == 'Y')
continue;
else
break;
}while(true);
11. }
}
Output
Welcome to the Reservation Calculator
Enter starting date mm/dd/yyyy:
Enter the arrival month (1-12):
5
Enter the arrival day (1-31):
16
Enter the arrival year:
2016
Enter ending date mm/dd/yyyy:
Enter the departure month (1-12):
5
Enter the departure day (1-31):
18
Enter the departure year:
2016
Arrival Date: MAY 16, 2016
Departure Date: MAY 18, 2016
Price: $105.0 per night
Total price: $210.0 for 2 night
Would you like to continue? (y/n)
y
Enter starting date mm/dd/yyyy:
Enter the arrival month (1-12):
7
Enter the arrival day (1-31):
20
Enter the arrival year:
2016
Enter ending date mm/dd/yyyy:
Enter the departure month (1-12):
7
Enter the departure day (1-31):
27
12. Enter the departure year:
2016
Arrival Date: JULY 20, 2016
Departure Date: JULY 27, 2016
Price: $105.0 per night
Total price: $735.0 for 7 night
Would you like to continue? (y/n)
n