# HELP: If-Else Statement

• March 19th, 2013, 03:53 PM
JennyDang
HELP: If-Else Statement
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

Code :

```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

Code :

```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.
• March 19th, 2013, 03:56 PM
Norm
Re: HELP: If-Else Statement
Can you copy the full contents of the console window from when you execute the program and paste it here showing the output and input?
• March 19th, 2013, 04:20 PM
Norm
Re: HELP: If-Else Statement
Please copy the full contents of the console window from when you execute the program and paste it here.

On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.
• March 19th, 2013, 04:22 PM
JennyDang
Re: HELP: If-Else Statement
Which package do you have: A, B. or C?
How many hours did you use?
a
50
You have Package A
Your bill for this month is \$89.95
You would save \$0.0 if you changed to Package B and \$0.0 if you switched to Package C.
• March 19th, 2013, 04:32 PM
Norm
Re: HELP: If-Else Statement
Try debugging the code by finding what variable the 0.0 value is coming from and then tracking through the code to see where that variable is getting a value. Add some println statements that print out that variable's value every time it is changed. The print out should show you want the code is doing and why the value is 0.
• March 19th, 2013, 05:01 PM
JennyDang
Re: HELP: If-Else Statement
I found out that the zeros are coming from these:

(at the very top of the InternetPackage class)
private double savings1;
private double savings2;

When I do this:
private double savings1 = 1;
private double savings2 = 1;

The output would be:
Which package do you have: A, B. or C?
How many hours did you use?
a
50
You have Package A
Your bill for this month is \$89.95
You would save \$1.0 if you changed to Package B and \$1.0 if you switched to Package C.
• March 19th, 2013, 05:10 PM
Norm
Re: HELP: If-Else Statement
Did the println you added to the code execute and print out a message when a value was assigned to the savings1 variable?

What you have shown by changing the initial value of savings1 is that its value is never changed by the program.
Now you need to trace through the code to see why that is. Is the method where savings1 is assigned a value ever called?

Add lots of println statements to the code to print messages as they are executed so you can see where the execution flow goes and to see how the values of variables are changed.
• March 19th, 2013, 07:04 PM
JennyDang
Re: HELP: If-Else Statement
I'm so confused..I feel like the problem would be in the getSavingsB and getSavingsC methods, but I don't know what's wrong with it since I made it so that the values in savingsB and savingsC would equal to savings1 and savings2.
• March 19th, 2013, 07:09 PM
Norm
Re: HELP: If-Else Statement
Where are those methods called? If they are not called, they won't be executed and set the values of the variables that you want set.
• March 19th, 2013, 07:29 PM
JennyDang
Re: HELP: If-Else Statement
Code :

```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; }```
• March 19th, 2013, 07:31 PM
Norm
Re: HELP: If-Else Statement
Do you have any questions about what you posted?
• March 19th, 2013, 07:43 PM
JennyDang
Re: HELP: If-Else Statement
Oh yeah, I thought I wrote something after that.
I was wondering if it had to do with the "return" part such as "return savings1 = savingsB?"
Like when the user inputs the hour, then the value of the hour goes through the equation and is stored into savingsB.
I tried to make the value of savingsB equivalent to savings1 so savings1 would have a value too but it doesn't seem to look like it worked.
• March 19th, 2013, 07:47 PM
Norm
Re: HELP: If-Else Statement
Where are those two methods called? If they are not called, the code inside them will not be executed.

Do you understand what I mean when I ask: where are the methods called?
Here is where the code calls the getPackage() method:
Code :

`System.out.println("You have " + pack.getPackage())`
• March 19th, 2013, 08:11 PM
JennyDang
Re: HELP: If-Else Statement
I know to call them I have to go to my tester class and insert them here

Code :

```System.out.println("You have " + pack.getPackage()); System.out.println("Your bill for this month is \$" + money.format(pack.getAmount())); System.out.println("Savings B: " + money.format(pack.getSavingsB())); <----- System.out.println("Savings C: " + money.format(pack.getSavingsC())); <---- System.out.println("Thank you for your time and business. Have a wonderful day!");```

And the output will come out as:
Which package do you have: A, B. or C?
How many hours did you use?
A
60
You have Package A
Your bill for this month is \$109.95
Savings B: 55.00
Savings C: 90.00

But my problem is that I want it to go through an if/else statement so that if the user inputs A, it would show how much they would save if they chose B & C. If they didn't choose A and chose B, then it would show what they would save if they had C and if they input neither of those, then it won't output anything.
So that's why I had this part which is in my InternetPackage class:

Code :

```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; }```

But when I called this getToString() to my tester class, the savings values would change to 0

--- Update ---

OHHHHH I get what I did wrong!!!
I assumed that I had to call my methods and write them as System.out.println("pack.getSavingsC()"); instead of putting it just as pack.getSavingsC();

Code :

``` System.out.println("You have " + pack.getPackage()); System.out.println("Your bill for this month is \$" + money.format(pack.getAmount())); pack.getSavingsB(); pack.getSavingsC(); System.out.println(pack.getToString()); System.out.println("Thank you for your time and business. Have a wonderful day!");```

Thank you so much!! Sorry for bothering you for soo long with just this simple mistake. Thank you!!
• March 19th, 2013, 08:12 PM
Norm
Re: HELP: If-Else Statement
Quote:

the savings values would change to 0
Where are the savings amounts changed to non-zero values? You need to trace through where the code executes and find where the savings variables are assigned values and then trace their values to where they are used in the message you want to print showing their values.

Where you put the calls to those two methods is NOT where you say you want the values displayed. So that must be the wrong place. Where do you want the savings values calculated so you can use them in the message you want to show?