Question: Why is this code not printing the savings portion under // Compute and print cost savings? import java.util.Scanner; public class CellService { public static void
Why is this code not printing the savings portion under "// Compute and print cost savings"?
import java.util.Scanner;
public class CellService {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Define constants for plan prices and minutes
final double PLAN1_PRICE = 29.99;
final int PLAN1_MINUTES = 400;
final double PLAN1_OVERAGE_RATE = 0.35;
final double PLAN2_PRICE = 49.99;
final int PLAN2_MINUTES = 800;
final double PLAN2_OVERAGE_RATE = 0.30;
final double PLAN3_PRICE = 59.99;
// Prompt user for name
System.out.print("Enter the customer's name: ");
String name = input.nextLine();
// Validate name
if (name.isEmpty() || name.length() < 2) {
System.out.println("Error: Invalid name");
return;
}
// Prompt user for plan choice
System.out.print("Enter a plan (1, 2, or 3): ");
int planChoice = input.nextInt();
// Validate plan choice
if (planChoice < 1 || planChoice > 3) {
System.out.println("Error: Invalid plan choice");
return;
}
// Prompt user for minutes used
System.out.print("Enter the number of minutes used: ");
int minutesUsed = input.nextInt();
// Validate minutes used
if (minutesUsed < 0) {
System.out.println("Error: Invalid number of minutes");
return;
}
// Compute bill based on plan choice and minutes used
double baseCharge = 0;
int baseMinutes = 0;
double overageRate = 0;
double overageCharge = 0;
if (planChoice == 1) {
baseCharge = PLAN1_PRICE;
baseMinutes = PLAN1_MINUTES;
overageRate = PLAN1_OVERAGE_RATE;
} else if (planChoice == 2) {
baseCharge = PLAN2_PRICE;
baseMinutes = PLAN2_MINUTES;
overageRate = PLAN2_OVERAGE_RATE;
} else {
baseCharge = PLAN3_PRICE;
baseMinutes = -1; // indicate unlimited minutes
overageRate = 0;
}
double totalBill = baseCharge;
int overageMinutes = 0;
if (baseMinutes >= 0 && minutesUsed > baseMinutes) {
overageMinutes = minutesUsed - baseMinutes;
overageCharge = overageMinutes * overageRate;
totalBill += overageCharge;
}
// Print bill
System.out.println(" Bill Name: " + name);
System.out.println("Plan: Plan " + planChoice);
if (baseMinutes >= 0) {
System.out.println("Plan Base Charge: $" + String.format("%.2f", baseCharge));
System.out.println("Plan Base Minutes: " + baseMinutes);
System.out.println("Overage Minutes: " + overageMinutes);
System.out.println("Price Per Minute Overage: $" + String.format("%.2f", overageRate));
System.out.println("Overage Charge: $" + String.format("%.2f", overageCharge));
}
System.out.println(" Total Bill: $" + String.format("%.2f", totalBill));
// Compute and print cost savings
if (planChoice == 1 && minutesUsed > PLAN2_MINUTES) {
double savings1 = (PLAN2_PRICE + (minutesUsed - PLAN2_MINUTES) * PLAN2_OVERAGE_RATE - totalBill);
double savings2 = (PLAN3_PRICE - totalBill);
if (savings1 > 0) {
System.out.println(" By switching to Plan 2, " + name + " could save $" +
String.format("%.2f", savings1) + " per month.");
}
if (savings2 > 0) {
System.out.println("By switching to Plan 3, " + name + " could save $" +
String.format("%.2f", savings2) + " per month.");
}
} else if (planChoice == 2 && minutesUsed > PLAN2_MINUTES) {
double savings = (PLAN3_PRICE - totalBill);
if (savings > 0) {
System.out.println(" By switching to Plan 3, " + name + " could save $" +
String.format("%.2f", savings) + " per month.");
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
