Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: HELP: If-Else Statement

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP: If-Else Statement

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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP: If-Else Statement

    Do you have any questions about what you posted?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    System.out.println("You have " + pack.getPackage())
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    JennyDang (March 19th, 2013)

  15. #14
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: HELP: If-Else Statement

    I know to call them I have to go to my tester class and insert them here

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

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

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

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  3. [SOLVED] If statement help
    By DarkPrince in forum Loops & Control Statements
    Replies: 1
    Last Post: November 7th, 2012, 04:15 PM
  4. not a statement?
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 23rd, 2011, 08:57 PM
  5. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM

Tags for this Thread