Results 1 to 4 of 4

Thread: Calculating savings

  1. #1
    Join Date
    Oct 2015
    Posts
    2
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default Calculating savings

    Hey everyone,

    I am new to programming. I first had to write a program using switch and if statements outputting a package you would want and the price. I did that fine. The next problem, I have to modify this problem to find the savings between packages A and B, A and C, as well as B and C. I am having such a hard time figuring this out and have tried everything I could think of. This is the code I have now, but it doesn't do what I need it to do. Any tips would be greatly appreciated!!

    Code:
    import java.util.Scanner; //Needed for the Scanner class
    
    //This program calculates ISP Savings
    
    public class InternetProvider2
    {
        public static void main(String[] args)
        {
            char letter; //Package letter
            double regularHours; //Hours used
            double extraHours;  //Extra hours used
            double monthlyFee;  //Monthly fee
            double hoursUsed;   //Total number of hours used
            double extraFee;    //Extra hours fee
            double totalFeeA=0;    //Total fee for package A
            double totalFeeB=0;   //Total fee for package B
            double totalFeeC=0;   //Total fee for package C
            double savingsAB;
            double savingsAC;
            double savingsBC;
            String input;   //Holds user input
    
            //Create a Scanner object for keyboard input
            Scanner keyboard = new Scanner(System.in);
    
             //Ask user for package letter
            System.out.print("Which package did you purchase? Enter A, B, or C: ");
            input = keyboard.nextLine();
            letter = input.charAt(0);
    
            //Ask user how many hours used
            System.out.print("How many hours did you use?: ");
            hoursUsed = keyboard.nextDouble();
    
    
            //Display monthly bill
            switch(letter)
            {
                case 'A':
                {
                if (hoursUsed < 10)
                System.out.println("Your monthly bill is $9.95");
                }
                if (hoursUsed > 10)
                {
                monthlyFee = 9.95;
                regularHours = 10;
                extraHours = (hoursUsed - regularHours);
                extraFee = (extraHours * 2);
                totalFeeA = (monthlyFee + extraFee);
                System.out.println("Your monthly bill is $" + totalFeeA);
                }
                break;
    
                case 'B':
                {
                if (hoursUsed < 20)
                System.out.println("Your monthly bill is $13.95");
                }
                if(hoursUsed > 20)
                {
                monthlyFee = 13.95;
                regularHours = 20;
                extraHours = (hoursUsed - regularHours);
                extraFee = (extraHours * 1);
                totalFeeB = (extraFee + monthlyFee);
                System.out.println("Your monthly bill is $" + totalFeeB);
                }
                break;
    
                case 'C':
                totalFeeC = 19.95;
                System.out.println("Your monthly bill is $" + totalFeeC);
                break;
                }
    
    
                       if(totalFeeA > totalFeeB)
                        {
                            savingsAB = totalFeeA - totalFeeB;
                            System.out.println("Total savings are: $" +savingsAB);
                        }
    
                        if(totalFeeA > totalFeeC)
                        {
                            savingsAC = totalFeeA - totalFeeC;
                            System.out.println("Total savings are: $" +savingsAC);
                        }
                        if(totalFeeB > totalFeeC)
                        {
                            savingsBC = totalFeeB - totalFeeC;
                            System.out.println("Total savings are: $" + savingsBC);
                        }
                        else
                    System.out.println("There are no savings available.");
    
    
         }
    }

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by GHero13 View Post
    I have to modify this problem to find the savings between packages A and B, A and C, as well as B and C.

    Java Code:
    if(totalFeeA > totalFeeB)
    {
        savingsAB = totalFeeA - totalFeeB;
        System.out.println("Total savings are: $" +savingsAB);
    }

    if(totalFeeA > totalFeeC)
    {
        savingsAC = totalFeeA - totalFeeC;
        System.out.println("Total savings are: $" +savingsAC);
    }
                       
    if(totalFeeB > totalFeeC)
    {
        savingsBC = totalFeeB - totalFeeC;
        System.out.println("Total savings are: $" + savingsBC);
    }

    else
        System.out.println("There are no savings available.");


    Keep in mind that you can do the ABSOLUTE value of the difference between the two numbers to get a positive result. Thus you don't have to check which one is larger than the other and worry about flipping the negative..


    Secondly, those if statements are each independent statements so your ELSE statement makes no sense as it is only attached to the LAST if statement. In other words, it does not do what you think it does.

    Example:

    Java Code:
    if (expression1) //compiler checks here first.
    {
        execute1();
    }

    if (expression2) //then it checks here.
    {
        execute2();
    }

    if (expression3) //then it checks here as well.
    {
        execute3();
    }


    will execute ALL 3 functions (depending on whether or not the expressions are true of course) and it will CHECK EVERY if statement regardless of whether or not the previous if statement executed.


    However:

    Java Code:
    if (blah) //compiler checks here first.
    {
        execute1();
    }
    else if (blah2) //it will only check here if blah was false!
    {
        execute2();
    }
    else //it will only go here if both blah and blah2 are false!
    {
        execute3();
    }


    Will execute ONE of the above statements. It will test if "blah" expression is true. If it is, only execute1 will be executed. If "blah" is false, it will then CHECK if "blah2" is true. If it is, then only execute2 will be executed. However, if both "blah" AND "blah2" is false, then it only execute3 will be executed.


    Java Code:
    savingsAB = Math.abs(totalFeeA - totalFeeB);
    savingsAC = Math.abs(totalFeeA - totalFeeC);
    savingsBC = Math.abs(totalFeeB - totalFeeC);



    System.out.println("Total savings are: $" +savingsAB);
    System.out.println("Total savings are: $" +savingsAC);
    System.out.println("Total savings are: $" + savingsBC);





    //OR:




    savingsAB = Math.abs(totalFeeA - totalFeeB);
    savingsAC = Math.abs(totalFeeA - totalFeeC);
    savingsBC = Math.abs(totalFeeB - totalFeeC);



    if (savingsAB > 0.0) {
        System.out.println("Total savings are: $" +savingsAB);
    }
    else if (savingsAC > 0.0) {
        System.out.println("Total savings are: $" +savingsAC);
    }
    else if (savingsBC > 0.0) {
        System.out.println("Total savings are: $" + savingsBC);
    }
    else {
        System.out.println("No savings.");
    }
    Last edited by Brandon; 10-15-2015 at 01:19 AM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Oct 2015
    Posts
    2
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Ah yes, I see what you are saying. Thanks a ton!!

  4. #4
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    Also, please use

    Code:
    if (condition) {
    
    }
    and not

    Code:
    if (condition)
    {
    
    }
    if your teacher has a problem with this, let me know and I will challenge him to physical combat
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •