The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

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.

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.

Re: HELP: If-Else Statement

Please copy the full contents of the console window from when you execute the program and paste it here.
The image is unreadable.

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'

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.
Thank you for your time and business. Have a wonderful day!

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.

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.
Thank you for your time and business. Have a wonderful day!

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.

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.

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.

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
Thank you for your time and business. Have a wonderful day!

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:

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();

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

Re: HELP: If-Else Statement

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?