Hi everyone, first time entry. I have a problem with my java program output. It looks like the program logic is correct, but there are still some line of statements that are executed that I do not want.
Here's the description of the problem:
And here's my java source code:An Internet service provider has 3 different subscription packages for its customers:
Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
Package B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
Package C: For $19.95 per month unlimited access is provided.
Write a program that calculates a customer's monthly bill. It should ask the user to enter the letter of the package the customer has purchased (A, B, or C) and the number of hours that were used. It should then display the total charges. If the package is not valid, or if the number of hours is greater than 744, display an error message.
import java.util.Scanner; public class internetPackage { public static void main(String[] args) { String input; char servicePackage; int hoursUsed, regularHours, extraHours; double monthlyFee, totalFee, extraHoursFee; // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Prompt user to choose Internet Service Package. System.out.print("Internet Service Provider Monthly Bill\n"); System.out.print("\nEnter package type (A,B, or C): "); input = keyboard.nextLine(); servicePackage = input.charAt(0); // Prompt user to enter number of hours. System.out.print("Enter number of hours: "); hoursUsed = keyboard.nextInt(); if (hoursUsed < 0) System.out.println("Invalid input. Hours cannot be negative."); switch (servicePackage) { case 'a': case 'A': monthlyFee = 9.95; regularHours = 10; if (hoursUsed < regularHours && hoursUsed >= 0) { totalFee = monthlyFee; System.out.println("Total due: $" + totalFee); break; } else { extraHours = hoursUsed - regularHours; extraHoursFee = extraHours * 2.00; totalFee = monthlyFee + extraHoursFee; System.out.println("Total due: $" + totalFee); break; } case 'b': case 'B': monthlyFee = 13.95; regularHours = 20; if (hoursUsed < regularHours) { totalFee = monthlyFee; System.out.println("Total due: $" + totalFee); break; } else { extraHours = hoursUsed - regularHours; extraHoursFee = extraHours * 1.00; totalFee = monthlyFee + extraHoursFee; System.out.println("Total due: $" + totalFee); break; } case 'c': case 'C': monthlyFee = 19.95; if (hoursUsed < 744) { totalFee = monthlyFee; System.out.println("Total due: $" + totalFee); break; } else { System.out.println("Invalid input. Hours cannot exceed 744."); break; } default: System.out.println("Invalid package type"); } } }
Rules:
And here are some sample outputs. The idea is to show results when choosing A, B, or C.:1) If the user inputs a negative number, the program should automatically display: "Invalid input. Hours cannot be negative."
2) If other letters (except A, B, and C) are inputted, the program should automatically display: "Invalid package type".
Much appreciated for any help I can get. Thanks for reading!Enter package type (A,B, or C): A
Enter number of hours: -1
Invalid input. Hours cannot be negative.
Total due: $-12.05 <--THIS SHOULD NOT SHOW UP, BUT IT STILL DOES
Enter package type (A,B, or C): D
Enter number of hours: 2 <--THIS SHOULD NOT SHOW UP, BUT IT STILL DOES
Invalid package type