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 6 of 6

Thread: Need If-Else if-Else help on my code

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need If-Else if-Else help on my code

    Task Part 1:
    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.

    Task Part 2: (experiencing problems in this part)
    Modify the program you wrote for Programming Challenge 13 (Task 1) so it also calculates and displays 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 would be no savings, no message should be printed.

    The problem that I am experiencing is that I will get a negative number if the price of the monthly bill is less than $19.95.
    e.g. I enter 'A' and '14'.
    Answers: Monthly bill: $17.95
    Savings if you purchase Package B: $4.0
    Savings if you purchase Package C: $-2.0

    How can I fix this code and not receive the negative number?

    This is my code:
    package internetserviceproviderpart2;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String inputString;
            char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double totalFee;
            double savingAB;
            double savingAC;
            double savingBC;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            if (packageLetter=='A' && hoursUsed>10)
            {
                additionalHours = hoursUsed - 10;
                totalFee = 9.95 + (additionalHours * 2.00);
                savingAB = totalFee - 13.95;
                savingAC = totalFee - 19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
                        " if you purchase Package B.");
                JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
                        " if you purchase Package C.");
            }
            else if (packageLetter=='A' && hoursUsed<=10)
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            else if (packageLetter=='B' && hoursUsed>20)
            {
                additionalHours = hoursUsed - 20;
                totalFee = 13.95 + (additionalHours * 1.00);
                savingBC = totalFee -19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
                        " if you purchase Package C.");
            }
            else if (packageLetter=='B' && hoursUsed<=20)
                JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
            else if (packageLetter=='C')
                JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
            else
                JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
     
            System.exit(0);
        }
     
    }
    Last edited by Plural; October 11th, 2010 at 08:23 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Need If-Else if-Else help on my code

    Quote Originally Posted by Plural View Post
    Task Part 1:
    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.

    Task Part 2: (experiencing problems in this part)
    Modify the program you wrote for Programming Challenge 13 (Task 1) so it also calculates and displays 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 would be no savings, no message should be printed.

    The problem that I am experiencing is that I will get a negative number if the price of the monthly bill is less than $19.95.
    e.g. I enter 'A' and '14'.
    Answers: Monthly bill: $17.95
    Savings if you purchase Package B: $4.0
    Savings if you purchase Package C: $-2.0

    How can I fix this code and not receive the negative number?

    This is my code:
    package internetserviceproviderpart2;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String inputString;
            char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double totalFee;
            double savingAB;
            double savingAC;
            double savingBC;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            if (packageLetter=='A' && hoursUsed>10)
            {
                additionalHours = hoursUsed - 10;
                totalFee = 9.95 + (additionalHours * 2.00);
                savingAB = totalFee - 13.95;
                savingAC = totalFee - 19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
                        " if you purchase Package B.");
                JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
                        " if you purchase Package C.");
            }
            else if (packageLetter=='A' && hoursUsed<=10)
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            else if (packageLetter=='B' && hoursUsed>20)
            {
                additionalHours = hoursUsed - 20;
                totalFee = 13.95 + (additionalHours * 1.00);
                savingBC = totalFee -19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
                        " if you purchase Package C.");
            }
            else if (packageLetter=='B' && hoursUsed<=20)
                JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
            else if (packageLetter=='C')
                JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
            else
                JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
     
            System.exit(0);
        }
     
    }
    double can hold negative values. You could have an inner if statement to tell it to set it to 0.00 or something if the totalFee is less than 19.95.

    In fact, it'll do that every time your total fee is less than the amount you're subtracting.

    If you have some kind of if statement that will set it so that it will set it to 0 otherwise you will be getting values as low as -$4.00 for 0 additional hours.


  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need If-Else if-Else help on my code

    Quote Originally Posted by javapenguin View Post
    double can hold negative values. You could have an inner if statement to tell it to set it to 0.00 or something if the totalFee is less than 19.95.

    In fact, it'll do that every time your total fee is less than the amount you're subtracting.

    If you have some kind of if statement that will set it so that it will set it to 0 otherwise you will be getting values as low as -$4.00 for 0 additional hours.

    I've changed up my code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package internetserviceproviderpart2;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String inputString;
            char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double totalFee = 0;
            double savingAB = totalFee - 13.95;
            double savingAC = totalFee - 19.95;
            double savingBC = totalFee - 19.95;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            if (packageLetter=='A' && hoursUsed>10)
            {
                additionalHours = hoursUsed - 10;
                totalFee = 9.95 + (additionalHours * 2.00);
                savingAB = totalFee - 13.95;
                savingAC = totalFee - 19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
                        " if you purchase Package B.");
                if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
                        " if you purchase Package C.");
                else
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            }
            else if (packageLetter=='B' && hoursUsed>20)
            {
                additionalHours = hoursUsed - 20;
                totalFee = 13.95 + (additionalHours * 1.00);
                savingBC = totalFee - 19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                if(packageLetter=='B' && hoursUsed>20 && totalFee > 19.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
                        " if you purchase Package C.");
            else
                JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
            }
            else if (packageLetter=='C')
                JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
            else
                JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
     
            System.exit(0);
        }
     
    }

    Now, it is more effective.
    But, you said something about a statement that can set the value into 0.0 and what statement would that be?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Need If-Else if-Else help on my code

    Quote Originally Posted by Plural View Post
    I've changed up my code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package internetserviceproviderpart2;
    import javax.swing.JOptionPane;
    /**
     *
     * @author Home
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String inputString;
            char packageLetter;
            int hoursUsed;
            int additionalHours = 0;
            double totalFee = 0;
            double savingAB = totalFee - 13.95;
            double savingAC = totalFee - 19.95;
            double savingBC = totalFee - 19.95;
     
            inputString = JOptionPane.showInputDialog("Enter the letter of the " +
                    "package you purchased (either A, B, or C.)");
            inputString = inputString.toUpperCase();
            packageLetter = inputString.charAt(0);
     
            inputString = JOptionPane.showInputDialog("Enter the number of hours " +
                    "you used.");
            hoursUsed = Integer.parseInt(inputString);
     
            if (packageLetter=='A' && hoursUsed>10)
            {
                additionalHours = hoursUsed - 10;
                totalFee = 9.95 + (additionalHours * 2.00);
                savingAB = totalFee - 13.95;
                savingAC = totalFee - 19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
                        " if you purchase Package B.");
                if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
                        " if you purchase Package C.");
                else
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            }
            else if (packageLetter=='B' && hoursUsed>20)
            {
                additionalHours = hoursUsed - 20;
                totalFee = 13.95 + (additionalHours * 1.00);
                savingBC = totalFee - 19.95;
                JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
                        totalFee + ".");
                if(packageLetter=='B' && hoursUsed>20 && totalFee > 19.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
                        " if you purchase Package C.");
            else
                JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
            }
            else if (packageLetter=='C')
                JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
            else
                JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
     
            System.exit(0);
        }
     
    }

    Now, it is more effective.
    But, you said something about a statement that can set the value into 0.0 and what statement would that be?
    if (packageLetter=='A' && hoursUsed>10)
            {
                additionalHours = hoursUsed - 10;
                totalFee = 9.95 + (additionalHours * 2.00);
                savingAB = totalFee - 13.95;
    if (savingAB < 0)
    savingAB = 0.00;
    else
    savingAB = savingAB;

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need If-Else if-Else help on my code

    Quote Originally Posted by javapenguin View Post
    if (packageLetter=='A' && hoursUsed>10)
            {
                additionalHours = hoursUsed - 10;
                totalFee = 9.95 + (additionalHours * 2.00);
                savingAB = totalFee - 13.95;
    if (savingAB < 0)
    savingAB = 0.00;
    else
    savingAB = savingAB;
    This statement got me pretty confused.
    Can you include the Message Dialog for me so it can clear up my confusion?

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Need If-Else if-Else help on my code

    Quote Originally Posted by Plural View Post
    This statement got me pretty confused.
    Can you include the Message Dialog for me so it can clear up my confusion?
    That part for the message dialog need not be altered. The code I gave you will reset the value so that when it is passed to showMessageDialog() it doesn't return a negative number.

    Also
    if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
                        " if you purchase Package B.");
                if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
                        " if you purchase Package C.");
                else
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            }

    You said you had problems with if, else if, else, right?

    Well, that last else will go into affect when :

    if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
    JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
    " if you purchase Package C.");

    isn't true.

    Did you mean:

    if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
                        " if you purchase Package B.");
          else   if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
                        " if you purchase Package C.");
                else
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            }

    Well, it should perhaps go like this: though I'm not sure where the else at the end goes:

    if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
               {
     
             if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
                JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
                        " if you purchase Package C.");
    else
     JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
                        " if you purchase Package B.");
    }
                else
                JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
            }
    Last edited by javapenguin; October 11th, 2010 at 10:08 PM.