create a java code with scanner and formatt
Write a generic program to find the total cost of the business trip by creating a class file called lastname_trip_cost.java. Add three instance variables for the type of airline ticket, the type of hotel and the total number of nights staying at the hotel. Add methods to return values for
* the cost of airline ticket
* total number of days less than complete weeks
* total number of whole weeks
* cost of hotel
Create a file called
lastname_trip_cost_demo.java. In this file add an
object to pass the three values to the class. Add statements to
print the values shown in the output.
Round trip airfare rates from New York to Orlando
Economy class $300.00
Business class $800.00
First class $1,000.00
Hotel Rates Daily Rate Weekly
3 Star Hotel $120.00 $750.00
4 Star Hotel $160.00 $1,000.00
5 Star Hotel $200.00 $1,250.00
User Input:
Enter the type of airline ticket - 1 for Economy, 2 for business
and 3 for First class = 2
Enter the type of hotel - 1 for 3 star, 2 for 4 star and 3 for 5
star = 1
Enter the total number of nights staying at the hotel = 15
Expected Output:
Cost of airline ticket = $800.00
Total number of days less than complete weeks = 15 - 7(2) = 1
Total number of whole weeks = 2
Cost of hotel = 750(2) + 120(1) = $1620
Total cost = 800 + 1620 = $2420
// trip_cost.java
public class trip_cost
{
// attributes
private String airline_ticket;
private int hotel_type;
private int total_number_of_nights;
// constructor to initialize the attributes
public trip_cost(String airline_ticket, int
hotel_type, int total_number_of_nights)
{
this.airline_ticket =
airline_ticket;
this.hotel_type = hotel_type;
this.total_number_of_nights =
total_number_of_nights;
}
// method to return the cost of airline ticket based
on the type of the ticket
public double getCostOfTicket()
{
if(airline_ticket.equalsIgnoreCase("Economy"))
return
300;
else
if(airline_ticket.equalsIgnoreCase("Business"))
return
800;
else
return
1000;
}
// method to return the number of days stayed in
hotel which is less than complete weeks
public int daysLessThanCompleteWeeks()
{
return
total_number_of_nights%7;
}
// method to return the number of complete weeks
stayed in the hotel
public int completeWeeks()
{
return
total_number_of_nights/7;
}
// method to calculate and return the total hotel
cost
public double hotelCost()
{
// hotel cost is determined by the
type of hotel and number of whole weeks and number of days less
than whole weeks
if(hotel_type == 3)
return
(completeWeeks()*750 + daysLessThanCompleteWeeks()*120);
else if(hotel_type == 4)
return
(completeWeeks()*1000 + daysLessThanCompleteWeeks()*160);
else
return
(completeWeeks()*1250 + daysLessThanCompleteWeeks()*200);
}
}
//end of trip_cost.java
// trip_cost_demo.java
import java.util.Scanner;
public class trip_cost_demo {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
int choice;
String airline_ticket;
int hotel_type,
number_of_nights;
// input the type of airline ticket
choice
System.out.print("Enter the type of
airline ticket - 1 for Economy, 2 for business and 3 for First
class: ");
choice = scan.nextInt();
// based on ticket type choice
determine the ticket type
if(choice == 1)
airline_ticket =
"Economy";
else if(choice == 2)
airline_ticket =
"Business";
else
airline_ticket =
"First";
// input the hotel type
choice
System.out.print("Enter the type of
hotel - 1 for 3 star, 2 for 4 star and 3 for 5 star: ");
choice = scan.nextInt();
// based on hotel choice, determine
hotel type
if(choice == 1)
hotel_type =
3;
else if(choice == 2)
hotel_type =
4;
else
hotel_type =
5;
// input the number of nights the
user stayed
System.out.print("Enter the total
number of nights staying at the hotel: ");
number_of_nights =
scan.nextInt();
// create a new object of
trip_cost
trip_cost trip = new
trip_cost(airline_ticket, hotel_type, number_of_nights);
// get the airline cost
double airline_cost =
trip.getCostOfTicket();
// get the total hotel cost
double hotel_cost =
trip.hotelCost();
// display the summary data
containing all information
System.out.printf("\nCost of
airline ticket: $%.2f",airline_cost);
System.out.printf("\nTotal number
of days less than complete weeks:
%d",trip.daysLessThanCompleteWeeks());
System.out.printf("\nTotal number
of whole weeks: %d",trip.completeWeeks());
System.out.printf("\nCost of hotel:
$%.2f",hotel_cost);
System.out.printf("\nTotal cost:
$%.2f",airline_cost+hotel_cost);
}
}
//end of trip_cost_demo.java
Output:
Get Answers For Free
Most questions answered within 1 hours.