# Determining savings for different ISP packages

My first assignment was to determine the monthly bill depending on what package you have for your ISP. This is the exact assignment.

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 $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. Here is my code for that program. I'm pretty sure it's inefficient but it's okay for a beginner. By the way, I know DrJava is old, but my school forces us to use it. // Adam King import java.util.*; public class Bill{ public static void main (String [] args){ Scanner keyboard = new Scanner(System.in); System.out.println("Package A: For$9.95 per month 10 hours of access are provided.  Additional hours are $2.00 per hour."); System.out.println("Package B: For$13.95 per month 20 hours of access are provided.  Additional hours are $1.00 per hour."); System.out.println("Package C: For$19.95 per month unlimited access is provided.");
System.out.println("Which package did you purchase? (A,B, or C)");
String pkg = keyboard.nextLine();
System.out.println("How many hours did you use?");
double hrs = keyboard.nextDouble();
if (hrs < 0){hrs = 0;}
double ahrs = hrs - 10;
double bhrs = hrs - 20;

if (pkg.equals("A")){
double prc = 9.95 + ((ahrs * 2));
System.out.println("Your bill this month is $"+prc+"."); } if (pkg.equals("B")){ double prc = (13.95 + (bhrs)); System.out.println("Your bill this month is$"+prc+".");
}
if (pkg.equals("C")){
double prc = (19.95);
System.out.println("Your bill this month is $"+prc+"."); } keyboard.close(); } } /* Welcome to DrJava. Working directory is F:\java > run Bill 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.
Which package did you purchase? (A,B, or C)
[DrJava Input Box]
How many hours did you use?
[DrJava Input Box]
Your bill this month is $13.95. > */  Now the real problem is the next assignment, which is based off of the last one. Here's the exact assignment. Modify the program you wrote for the last program so it also calculates and displays the amount of money you would save by switching to a different package. If you don't save any money, tell the user that there is no savings. Only show the package with the most savings. This is what I have so far, and I'm sure something isn't right. It compiles, and runs correctly, but I feel like the math is wrong. // Adam King import java.util.*; public class Save{ public static void main (String [] args){ Scanner keyboard = new Scanner(System.in); System.out.println("Package A: For$9.95 per month 10 hours of access are provided.  Additional hours are $2.00 per hour."); System.out.println("Package B: For$13.95 per month 20 hours of access are provided.  Additional hours are $1.00 per hour."); System.out.println("Package C: For$19.95 per month unlimited access is provided.");
System.out.println("Which package do you currently have? (A,B, or C)");
String pkg = keyboard.nextLine();
System.out.println("How many hours did you use this month?");
double hrs = keyboard.nextDouble();
if (hrs < 0){hrs = 0;}
double ahrs = hrs - 10;
double bhrs = hrs - 20;
double aprc = 9.95 + ((ahrs * 2));
double bprc = (13.95 + (bhrs));
double cprc = (19.95);

if (pkg.equals("A")){
System.out.println("Your bill this month is $"+aprc+"."); if (aprc >= cprc){ System.out.println("You can save$"+(aprc -cprc)+" by switching to Package C.");
}
else if (aprc >= bprc){
System.out.println("You can save $"+(aprc -bprc)+" by switching to Package B."); } else if (aprc < cprc && aprc < bprc){ System.out.println("Your current package is the cheapest package."); } } if (pkg.equals("B")){ System.out.println("Your bill this month is$"+bprc+".");
if (bprc >= cprc){
System.out.println("You can save $"+(bprc -cprc)+" by switching to Package C."); } else if (bprc >= aprc){ System.out.println("You can save$"+(bprc -aprc)+" by switching to Package A.");
}
else if (bprc < aprc && bprc < cprc){
System.out.println("Your current package is the cheapest package.");
}
}

if (pkg.equals("C")){
System.out.println("Your bill this month is $"+cprc+"."); if (cprc > aprc){ System.out.println("You can save$"+(cprc -aprc)+" by switching to Package A.");
}
else if (cprc > bprc){
System.out.println("You can save $"+(cprc -bprc)+" by switching to Package B."); } else if (cprc < aprc && cprc < bprc){ System.out.println("Your current package is the cheapest package."); } } keyboard.close(); } } /* Welcome to DrJava. Working directory is F:\java > run Save 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.
Which package do you currently have? (A,B, or C)
[DrJava Input Box]
How many hours did you use this month?
[DrJava Input Box]
Your bill this month is $13.95. You can save$8.0 by switching to Package B.
>
*/


If anyone can check this over and tell me if anything is wrong I'd be ecstatic.

• You are correct. The code is wrong. I don't know that I'd describe it as a math error though. The math seems right, but the logic looks wrong. Pick package A with 16 to 20 hours of usage. The answer should be B, but you'll answer C (pick a value and work out the math manually or change the code to display all the prices for testing). At 15 hours, you should say A but will say C. It may also be wrong for B and C, but I didn't look for an incorrect value for those. Your code is also wrong for A and B because they use >=. Your last else if in each structure should use <=. Apr 7, 2017 at 14:16

## 1 Answer

In regard to your first program, consider this case: you opt in for Package B, and you end up taking advantage of 10 hours of internet. According to your program, you'll be charged \$3.95. In response to this logic error, consider using an if statement to decide whether or not the customer/user is above the monthly limit (if (hrs > 10)) prescribed by their Package.

Also, consider cleaning up your code on line if (hrs < 0){hrs = 0;} by breaking up the elements onto their own lines. This means putting what's between the brackets on it's own line and bringing the last bracket onto a line.

@mdfst13 mentioned in the comments how your logic is wrong in the second program. Consider drawing on paper a diagram for how each output will be calculated. It might look like a tree diagram or a flowchart.