An internet service provider has three different subscription package for its cu
ID: 3540402 • Letter: A
Question
An internet service provider has three different subscription package 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 acustomer's monthly bill. It should ask the user to enter the letter of the package the customer has purchased (A,B,C) and the number of hours that were used.It should then display the total charges.
Complete Programming Challenge #13 Internet Service Provider on p. 215.
Include comments at the top of the file that gives your name and date and a brief description of the program%u2019s purpose. Include comments in the body of the code that describes what the code is doing. Be sure to use white space and indentation to make your program easy to understand.
You will need to accept package input as a String, then use charAt(0) to get the first letter. Consider how to handle input if user enters package a, rather than package A, etc.
Use the switch statement based on the package type to calculate the monthly bill, for example:
switch (package)
{
case %u2018A%u2019: // $9.95/month plus $2/hour over 10 hours
Default: // display error message if not A, B, or C
} //end switch (package)
Explanation / Answer
import java.util.Scanner; public class InternetServiceProvider { public static void main (String args[]) { while (true) { printMonthlyBill(calculateBill(getHours(), menu())); } } public static double getHours() { double hours; Scanner inputHours = new Scanner (System.in); System.out.print("Please enter the hours used: "); hours = inputHours.nextDouble(); inputHours.close(); return hours; } public static int menu () { int packageChoice; Scanner userInput = new Scanner (System.in); System.out.println("[1] Package A"); System.out.println("[2] Package B"); System.out.println("[3] Package C"); System.out.print("Please select your package: "); packageChoice = userInput.nextInt(); userInput.close(); return packageChoice; } public static double calculateBill(double hours, int packageChoice) { switch (packageChoice) { case 1: if (hours < 10) { return 9.95; } else { return (hours - 10)*2 + 9.95; } case 2: if (hours < 20) { return 13.95; } else { return (hours - 20) + 13.95; } case 3: return 19.95; default: System.out.println("Invalid input!"); return 0; } } public static void printMonthlyBill(double bill) { System.out.println("Your monthy bill is $" + bill); } } import java.util.Scanner; public class InternetServiceProvider { public static void main (String args[]) { while (true) { printMonthlyBill(calculateBill(getHours(), menu())); } } public static double getHours() { double hours; Scanner inputHours = new Scanner (System.in); System.out.print("Please enter the hours used: "); hours = inputHours.nextDouble(); inputHours.close(); return hours; } public static int menu () { int packageChoice; Scanner userInput = new Scanner (System.in); System.out.println("[1] Package A"); System.out.println("[2] Package B"); System.out.println("[3] Package C"); System.out.print("Please select your package: "); packageChoice = userInput.nextInt(); userInput.close(); return packageChoice; } public static double calculateBill(double hours, int packageChoice) { switch (packageChoice) { case 1: if (hours < 10) { return 9.95; } else { return (hours - 10)*2 + 9.95; } case 2: if (hours < 20) { return 13.95; } else { return (hours - 20) + 13.95; } case 3: return 19.95; default: System.out.println("Invalid input!"); return 0; } } public static void printMonthlyBill(double bill) { System.out.println("Your monthy bill is $" + bill); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.