Monday, June 17, 2013

Java Switch Statements for Eclipse


Java Program for Switch Statements for the Programming Environment Eclipse



ObjectiveTo learn and practice with multi-way selections using switch statements. Problem Description
Your friend Jackie in Detroit is planning for a vacation. She needs to choose one destination from three possible locations. There are only two approaches for transportation – either drive or fly. If she drives, the gas price is $3.89 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 Washington DC, she may choose either fly or drive. The meal cost depends on the type of the location: regular ($31 per day) and high-cost ($52 per day).
She will 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):
Write a Java program to help Jackie to figure out the cost of the trip (round trip).
Requirements:
  1. Must include a well-written program description to describe the task of the problem. 
  2. Use named constants for the fixed values. 
  3. Display a menu for the user to make a selection. The menu items may be labeled 1, 2, 3, etc. Use switch statements to handle the user’s selection of destination and corresponding transportation method. Assign values to appropriate variables according to user’s selection. Check the validity of user’s inputs using default clause in switch statements. Terminate the execution using System.exit(1); when an invalid input is encountered. 
  4. Do the same for hotel selection. 
  5. Correctly calculate the costs for transportation (round trip), hotel, meals, and the total. 
destination
transportation
airfare
(round trip) 
driving distance
(one way)
meal
Cleveland
drive
169 miles
regular
Las Vegas
fly
$507
high cost
Washington DC 
drive or fly
$328
526 miles
high cost
hotel
cost per day
Comfort Inn 
$85
Marriott
$159
Hyatt
$238
page1image42320

page2image1016
6. The output produced by your program should include itemized costs (i.e. cost of transportation, cost of hotel, and cost of meals) in addition to the total cost, and must be descriptive and well formated.
7. Test your program with various input data, including the minimum of the following:
• Travel to Cleveland, stay at Comfort Inn. • Travel to Las Vegas, stay at Marriott.• Drive to Washington DC, stay at Marriott. • Fly to Washington DC, stay at Hyatt.
• Invalid input.
A sample run may look like this:

Here are the places you may go for vacation.
--------------------------------------------
   1. Cleveland
   2. Las Vegas
   3. Washington DC
--------------------------------------------
Please select the number of the destination --> 3
To Washington DC, do you want to drive (’d’) or fly (’f’)? d
These are the hotels having vacancies:
----------------------------------------
1. Comfort Inn
2. Marriott
3. Hyatt
$ 85.00
$159.00
$238.00
----------------------------------------
Please make your selection --> 2
Cost for your trip:
(drive to Washington DC, staying at Marriott for 3 days):
           ---------------------------
             Trasportation  $ 170.51
             Hotel cost   : $ 477.00
             Cost of meals: $ 156.00
           ---------------------------
             Total cost   : $ 803.51
           ---------------------------
Have a nice vacation!






The Code for this program is 


import java.util.Scanner;
public class Enter Your Class Here{
/**
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int destination;
int distance=1;
String dest= "dest";
double transportation;
double airfare, drivingdistance,gallon;
final double regularMeal= 31;
final double highMeal= 52;
double flying, driving;
double gasPrice = 3.89;
final String dest1 = "Cleaveland";
final String dest2 = "Las Vegas";
final String dest3 = "Washington DC";
String hotel0 = "hotel";
final String hotel1= "Comfort Inn ";
final String hotel2= "Marriott";
final String hotel3= "Hyatt";
final double gasMileage = 24.00;
double totaltran = 0 ;
final String cityName;
int hotel=0;
double hotelcost =1;
double mealtotal=0;
int travel =0;
//Users Input
System.out.println(" \t 1 \t \t Cleveland \t");
System.out.println(" \t 2 \t \t Las Vegas \t");
System.out.println(" \t 3 \t \t Washington \t");
System.out.println("Please select the number of the destination?");
destination = input.nextInt();
System.out.println("To" + destination + "do you want to drive ('1') or fly ('2')?");
travel= input.nextInt();
System.out.println("These are the hotels having vacancies:");
System.out.println(" \t 1 \t \t Comfort Inn\t $85.00");
System.out.println(" \t 2 \t \t Marriott \t $159.00");
System.out.println(" \t 3 \t \t Hyatt \t\t $238.00");
System.out.println("Please make your hotels selection");
hotel = input.nextInt();
//Body
switch(destination){
case 1 :dest = dest1;
distance = 169;
totaltran= ((distance = 169*2)/gasMileage)*gasPrice;
mealtotal= regularMeal*3;
break;
case 2 :dest = dest2;
totaltran=507;
mealtotal = highMeal*3;
break;
case 3 : dest = dest3;
mealtotal= highMeal*3;
switch(travel){
case 1: totaltran= ((distance = 526*2)/gasMileage)*gasPrice;
distance = 526;
break;
case 2: totaltran = 328;
break;
}
break;
default:
}
switch(hotel) {
case 1: hotel0=hotel1;
hotelcost = 3 * 85;
break;
case 2: hotel0=hotel2;
hotelcost = 3 * 159;
break;
case 3: hotel0= hotel3;
hotelcost = 3 * 238;
break;
default: System.out.println("hotels doesnt exist");
}
//Calculate and Print Cost 
System.out.println("Cost For Your Trip:");
System.out.println("(Drive to " + dest + ", staying at " + hotel0 + " for 3 days.):");
System.out.println("-----------------------------------------------");
System.out.printf("Transportation $ %4.2f\n" ,totaltran);
System.out.printf("Hotel Cost : $ %4.2f\n" , hotelcost);
System.out.printf("Cost of meals: $ %4.2f\n" , mealtotal);
System.out.println("-----------------------------------------------");
System.out.printf("Total cost : $ %1.2f\n" , totaltran + hotelcost+ mealtotal);
}
}

No comments:

Post a Comment