Objective You will learn to program using switch statements to make decisions. A
ID: 3756210 • Letter: O
Question
Objective You will learn to program using switch statements to make decisions. Assignment Your friend Karen in Detroit is planning for a vacation. She needs to choose one destination from five possible locations. There are only two approaches for transportation either drive or fly. If she drives, the gas price is S3.29 per gallon (assume same price across the country) and the average gas mileage of her car is 24 miles per gallon. If she flies, the airfare depends on the destination from Detroit. To St. Louis, she may choose either fly or drive. The meal cost depends on the type of the location: regular ($31 per day) and high-cost (S52 per day). destination transportation airfare driving distancemeal Niagara Falls drive drive 257 miles 303 miles Chicago San Francisco Las Vegas St. Louis 630 $415 $290 high cost igh cost high cost regular drive or fly 551 miles She wll stay at a hotel of her choice at the destination for 3 days. The costs of the hotel rooms are shown below (assume the hotels of the same hotel chain cost the same across the country) hotel chain Budgetel Holiday Inn Sheraton Hyatt cost per day $109 $138 215 Write a Java program to help Karen to figure out the cost of the trip (round trip). The output produced by your program must be descriptive and well formatted. Test your program with various input data, including the minimum of the following: 1. Travel to Niagara Falls, stay at Sheraton 2. Travel to San Francisco, stay at Holiday Inn 3. Drive to St. Louis, stay at Budget Inn 4. Fly to St. Louis, stay at Hyatt 5. Invalid inputExplanation / Answer
import java.util.*;
import java.io.*;
class TravelCostCalculator
{
public static void main (String[] args) throws IOException
{
int n, htype, type;
double tcost, mcost, hcost;
tcost=mcost=hcost=0.0d;
Scanner sc = new Scanner(System.in);
System.out.println("Here are the places you may go for Vacation");
System.out.println("-------------------------------------------");
System.out.println("1. Niagra Falls");
System.out.println("2. Chicago");
System.out.println("3. San Franscisco");
System.out.println("4. Las Vegas");
System.out.println("5. St. Louis");
System.out.println("-------------------------------------------");
System.out.println("Please enter the number of the destination -->");
n=sc.nextInt();
switch(n)
{
case 1: tcost = (257 / 24) * 3.29;
mcost = (31 * 3);
htype = hoteltype();
System.out.print("Cost for your trip(drive to Niagra Falls, staying at ");
switch(htype)
{
case 76: System.out.print("Budgetel for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 109:System.out.print("Holiday Inn for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 138:System.out.print("Sheraton for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 215:System.out.print("Hyatt for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
default: break;
}
System.out.println("----------------------------------------------------");
System.out.println("Transportation: $ "+tcost);
System.out.println("Hotel Cost : $ "+hcost);
System.out.println("Cost of Meals : $ "+mcost);
System.out.println("------------------------------------------------------");;
System.out.println("Total Cost: $ "+(hcost+mcost+tcost));
System.out.println("Have a Nice Vacation!");
break;
case 2:tcost = (303 / 24) * 3.29;
mcost = (52 * 3);
htype = hoteltype();
System.out.print("Cost for your trip(drive to Chicago, staying at ");
switch(htype)
{
case 76: System.out.print("Budgetel for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 109:System.out.print("Holiday Inn for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 138:System.out.print("Sheraton for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 215:System.out.print("Hyatt for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
default: break;
}
System.out.println("----------------------------------------------------");
System.out.println("Transportation: $ "+tcost);
System.out.println("Hotel Cost : $ "+hcost);
System.out.println("Cost of Meals : $ "+mcost);
System.out.println("------------------------------------------------------");;
System.out.println("Total Cost: $ "+(hcost+mcost+tcost));
System.out.println("Have a Nice Vacation!");
break;
case 3:tcost = 630.0;
mcost = (52 * 3);
htype = hoteltype();
System.out.print("Cost for your trip(fly to San Francisco, staying at ");
switch(htype)
{
case 76: System.out.print("Budgetel for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 109:System.out.print("Holiday Inn for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 138:System.out.print("Sheraton for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 215:System.out.print("Hyatt for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
default: break;
}
System.out.println("----------------------------------------------------");
System.out.println("Transportation: $ "+tcost);
System.out.println("Hotel Cost : $ "+hcost);
System.out.println("Cost of Meals : $ "+mcost);
System.out.println("------------------------------------------------------");;
System.out.println("Total Cost: $ "+(hcost+mcost+tcost));
System.out.println("Have a Nice Vacation!");
break;
case 4:tcost = 415.0;
mcost = (52 * 3);
htype = hoteltype();
System.out.print("Cost for your trip(fly to Las Vegas, staying at ");
switch(htype)
{
case 76: System.out.print("Budgetel for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 109:System.out.print("Holiday Inn for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 138:System.out.print("Sheraton for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 215:System.out.print("Hyatt for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
default: break;
}
System.out.println("----------------------------------------------------");
System.out.println("Transportation: $ "+tcost);
System.out.println("Hotel Cost : $ "+hcost);
System.out.println("Cost of Meals : $ "+mcost);
System.out.println("------------------------------------------------------");;
System.out.println("Total Cost: $ "+(hcost+mcost+tcost));
System.out.println("Have a Nice Vacation!");
break;
case 5: mcost = (31 * 3);
System.out.println("Would You rather:");
System.out.println("1. Drive");
System.out.println("2. Fly");
System.out.print("-->");
type = sc.nextInt();
switch(type)
{
case 1: tcost = (551 / 24) * 3.29;
System.out.print("Cost for your trip(drive to St. Louis, staying at ");
break;
case 2: tcost = 290.0;
System.out.print("Cost for your trip(fly to St. Louis, staying at ");
break;
default: System.out.println("Invalid Input, please restart");
break;
}
htype = hoteltype();
switch(htype)
{
case 76: System.out.print("Budgetel for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 109:System.out.print("Holiday Inn for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 138:System.out.print("Sheraton for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
case 215:System.out.print("Hyatt for 3 days):");
System.out.println("-------------------------------------");
hcost = htype * 3;
break;
default: break;
}
System.out.println("----------------------------------------------------");
System.out.println("Transportation: $ "+tcost);
System.out.println("Hotel Cost : $ "+hcost);
System.out.println("Cost of Meals : $ "+mcost);
System.out.println("------------------------------------------------------");;
System.out.println("Total Cost: $ "+(hcost+mcost+tcost));
System.out.println("Have a Nice Vacation!");
break;
default: System.out.println("Invalid Input, please restart");
}
}
public static int hoteltype()
{
int htype;
Scanner sc = new Scanner(System.in);
System.out.println("These are the hotels having vacancies:");
System.out.println("--------------------------------------");
System.out.println("1. Budgetel");
System.out.println("2. Holiday Inn");
System.out.println("3. Sheraton");
System.out.println("4. Hyatt");
System.out.println("--------------------------------------");
System.out.println("Please make your selection --> ");
htype=sc.nextInt();
switch(htype)
{
case 1: return (76);
case 2: return (109);
case 3: return (138);
case 4: return (215);
default: System.out.println("Invalid Input, please restart");
return 0;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.