An Internet service provider has three 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 $14.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.
For any of the packages, a fraction of an hour will be charged for an entire hour (hint: there's a Math method that will help).
Design a class that calculates a customer's monthly bill.
-It should store the letter of the package the customer has purchased (A, B, or C) and the number of hours that were used.
-It should have a method that returns the total charges.
[I'm having trouble on this part]
-It should also calulate the amount of money Package A customers would save if they purchased packages B or C, and the amount of money Package B customers would save if they purchased Package C.
If there is no savings, no message should be printed.
The tester class would prompt the user for the package and the number of hours used, construct a new instance of the class and invoke the method(s) to calculate and display the bill.
This is my class
public class InternetPackages { //declaring constants and variables private char packages; private double hours; private double PACKAGE_A_PER_MONTH = 9.95; private double PACKAGE_B_PER_MONTH = 14.95; private double PACKAGE_C_PER_MONTH = 19.95; private double savings1 ; private double savings2; //argument constructor public InternetPackages (char pack, double hr){ packages = pack; hours = hr; } //Methods: To get Package Type, Amount, and Savings public String getPackage(){ String p; //if/else statement to decide user's package if (packages == 'A' || packages == 'a') p = "Package A"; else if (packages == 'B' || packages == 'b') p = "Package B"; else if (packages == 'C' || packages == 'c') p = "Package C"; else p = "Illegal input. Please try again."; return p; } public double getAmount(){ double amount; //if/else statement to determine total amount of the bill if (packages == 'A'|| packages == 'a') amount = (Math.ceil(hours)-10)*2 + PACKAGE_A_PER_MONTH; else if (packages == 'B' || packages == 'b') amount = (Math.ceil(hours)-20) + PACKAGE_B_PER_MONTH; else if (packages == 'C' || packages == 'c') amount = PACKAGE_C_PER_MONTH; else amount = 0; return amount; } public double getSavingsB(){ final double SAVINGS_B =((Math.ceil(hours)-10)*2 + PACKAGE_A_PER_MONTH)-((Math.ceil(hours)-20) + PACKAGE_B_PER_MONTH); double savingsB; //if/else statement to calculate what the user could've saved if they had the B or C packages if (packages == 'A' || packages == 'a') savingsB = SAVINGS_B; else savingsB = 0; return savings1 = savingsB; } public double getSavingsC(){ final double SAVINGS_C = ((hours-10)*2 + PACKAGE_A_PER_MONTH) - (PACKAGE_C_PER_MONTH); final double SAVINGS_C_FOR_PACKB = ((hours-20) + PACKAGE_B_PER_MONTH) - (PACKAGE_C_PER_MONTH); double savingsC; //if/else statement to calculate what the user could've saved if they had the B or C packages if (packages == 'A' || packages == 'a') savingsC = SAVINGS_C; else if (packages == 'B'|| packages == 'b') savingsC = SAVINGS_C_FOR_PACKB; else savingsC = 0; return savings2 = savingsC; } public String getToString(){ String savings; //if/else statement to output savings if (packages == 'A' || packages == 'a') savings = "You would save $" + savings1 + " if you changed to Package B and $" + savings2 + " if you switched to Package C."; else if (packages == 'B' || packages == 'b') savings = "You would save $"+ savings2 + " if you switched to Package C."; else savings = "This is the best package!"; return savings; } }
and this is my tester
import java.text.DecimalFormat; import java.util.Scanner; public class InternetPackagesTester { public static void main(String[] args) { //declaring input Scanner input = new Scanner (System.in); //asking user for package and hour info System.out.println("Which package do you have: A, B. or C?"); System.out.println("How many hours did you use?"); InternetPackages pack = new InternetPackages (input.next().charAt(0), input.nextDouble()); //round by the hundredth decimal point DecimalFormat money = new DecimalFormat("00.00"); System.out.println("You have " + pack.getPackage()); System.out.println("Your bill for this month is $" + money.format(pack.getAmount())); System.out.println(pack.getToString()); System.out.println("Thank you for your time and business. Have a wonderful day!"); } }
When I run it, everything thing is fine except for the savings.
If I were to input "A" and 30 hrs, it would print out:
"You would save $0.0 if you changed to Package B and $0.0 if you switched to Package C."
I don't understand why the values of the savings would appear instead of the 0.0.