I have to write a code to simulate giving a car rental quote where the user inputs the color, amount of days to rent, and chooses between two types of vehicles. There are two rates for each type, one weekly, one daily. I then have to find the best rate and output it. When I input white, economy, and 4 in the code below it doesn't output anything. I think I have an error in the rate for less than a week but I can't figure out how to fix it.
import java.util.Scanner;
public class lab3
{
public static final double ECONOMY_DAILY = 25.5;
public static final double FULL_DAILY = 39.4;
public static final double ECONOMY_WEEKLY = 120.5;
public static final double FULL_WEEKLY = 216.25;
public static void main (String[] args)
{
Scanner Keyboard = new Scanner(System.in);
System.out.println("Enter the color of the vehicle:");
//string the next input as color
String color = Keyboard.next();
System.out.println ("Economy or Full:");
// string the type of the vehicle
String Type = Keyboard.next();
// to get the character value to uppercase for the switch statement
char FirstTypeLetter = Type.toUpperCase()
.charAt(0);
System.out.println("For how many days?");
//set days as the next integer entered and calculate the amount of weeks and daysleftover using the / and % operators
int days = Keyboard.nextInt();
int weeks = days/7;
int daysLeftOver = days%7;
double weeksRounded = ((days/7)*100)/100;
double rate1,rate2,rate3;
// create a switch using the variable defined earlier
switch (FirstTypeLetter)
{
// if the Type entry starts with an e
case 'F':
// calculate the 3 rates for full size using the full size constants (could have put this code anywhere above the next if statement.)
rate1 = weeksRounded * FULL_WEEKLY;
rate2 = (weeks * FULL_WEEKLY) + (days * FULL_DAILY);
rate3 = (days * ECONOMY_DAILY);
case 'E':
// calculate all available rents for economy using the constants defined earlier
rate1 = weeksRounded * ECONOMY_WEEKLY;
rate2 = (weeks * ECONOMY_WEEKLY) + (days * ECONOMY_DAILY);
rate3 = (days * ECONOMY_DAILY);
// if the first rate is the lowest rate
if ((rate1 < rate2) & (rate1 < rate3) & (rate1 != 0))
{
// print out the first rate as well as the color and type that the user entered
System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f",rate1);
// if not, and if the second rate is cheapest
if ((rate2 < rate1) & (rate2 < rate3) & (rate1 != 0))
{
// print out the second rate as well as the color and type that the user entered
System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f", rate2);
}
// if the third rate is cheapest then print out that rate
else if ((rate3 < rate2) & (rate3 < rate1) & (rate3 != 0))
System.out.printf("This is the best rate for a" + " " + color + " " + Type + " vehicle for" + " " + days + "days:" + "%.2f", rate3);
}
//end the case and start the case for full rate
break;
//if the type starts with f
// create a default case in the event that an incompatible type was entered.
default:
System.out.println("Try Again!");
break;
}
}
}
Aucun commentaire:
Enregistrer un commentaire