0

I'm supposed to write a code for my programming class that will act as though you are ordering something online. I've reached the part where I have the program take the item number put in and return a price (without shipping costs), but every time I run it, it seems to have automatically set the variable I'm using to represent price to 13. It should be set to zero. Here's the part of my code that's giving me issues:

class Order {
    public Order() {}

    public double price(String odd, double quant, int g) {
        double set;
        if (g == 1) {
            if (odd.equals("AT413")) {
                set = (50.03 * quant) + set;
            } else if (odd.equals("AT414")) {
                set = (60.04 * quant) + set;
            } else if (odd.equals("AT415")) {
                set = (147.01 * quant) + set;
            } else if (odd.equals("AT416")) {
                set = (38.00 * quant) + set;
            } else if (odd.equals("AT417")); {
                set = (13.00 * quant) + set;
            }
        } else if (g == 2) {
            if (odd.equals("AT513")) {
                set = (50.03 * quant) + set;
            } else if (odd.equals("AT514")) {
                set = (50.00 * quant) + set;
            } else if (odd.equals("AT515")) {
                set = (130.02 * quant) + set;
            } else if (odd.equals("AT516")) {
                set = (25.03 * quant) + set;
            } else if (odd.equals("AT517")); {
                set = (13.00 * quant) + set;
            }
        } else if (g == 3) {
            if (odd.equals("AT613")) {
                set = (50.03 * quant) + set;
            } else if (odd.equals("AT614")) {
                set = (86.00 * quant) + set;
            } else if (odd.equals("AT615")) {
                set = (130.00 * quant) + set;
            } else if (odd.equals("AT616")) {
                set = (40.04 * quant) + set;
            } else if (odd.equals("AT617")); {
                set = (13.00 * quant) + set;
            }
        }
        return set;
    }
}

I tried a few different things. I had it get the variable set from the main code, but that didn't work because even if I sent down 0 to the class, it still set...set (sorry about that variable name) at 13. I tried getting rid of the +set at the end, but that didn't work because then it just printed out 13 as the order cost, even if I put in the String that's supposed to call up 50.03 for the price. I changed matching prices to see what would happen, and I realized that it's pulling the number it's set the variable at from very last line of code, with AT617, but I don't know why it's doing that or how to fix it. Help would be much appreciated, please.

7
  • 2
    It would be very helpful if you could format all those if statements... please. Commented Jan 17, 2014 at 21:56
  • 2
    it's that semicolon on the else if(odd.equals("AT417")); line Commented Jan 17, 2014 at 21:57
  • That if statement is an abomination :( Please format it better. Also consider using Enumerations or something to old your literals. Commented Jan 17, 2014 at 21:57
  • Not sure how you caught that Gus, but nice one! Commented Jan 17, 2014 at 22:00
  • Note that you still might end up not initializing set because those inner blocks still don't have a final unconditional else. What happens when, e.g., g is 3, but odd doesn't equal AT613, AT614, AT615, AT616, or AT617? Commented Jan 17, 2014 at 22:05

2 Answers 2

3

If you had formatted your code you could have seen that

public double price(String odd, double quant, int g) {
    double set = 0;
    if (g == 1) {
        if (odd.equals("AT413"))
            set = 50.03 * quant + set;
        else if (odd.equals("AT414"))
            set = 60.04 * quant + set;
        else if (odd.equals("AT415"))
            set = 147.01 * quant + set;
        else if (odd.equals("AT416"))
            set = 38.00 * quant + set;
        else if (odd.equals("AT417"))
            ;
        {
            set = 13.00 * quant + set;
        }
    } else if (g == 2) {
        if (odd.equals("AT513"))
            set = 50.03 * quant + set;
        else if (odd.equals("AT514"))
            set = 50.00 * quant + set;
        else if (odd.equals("AT515"))
            set = 130.02 * quant + set;
        else if (odd.equals("AT516"))
            set = 25.03 * quant + set;
        else if (odd.equals("AT517"))
            ;
        {
            set = 13.00 * quant + set;
        }
    } else if (g == 3) {
        if (odd.equals("AT613"))
            set = 50.03 * quant + set;
        else if (odd.equals("AT614"))
            set = 86.00 * quant + set;
        else if (odd.equals("AT615"))
            set = 130.00 * quant + set;
        else if (odd.equals("AT616"))
            set = 40.04 * quant + set;
        else if (odd.equals("AT617"))
            ;
        {
            set = 13.00 * quant + set;
        }
    }
    return set;
}

has very strange looking pieces like

        else if (odd.equals("AT417"))
            ;
        {
            set = 13.00 * quant + set;
        }

Remove that semicolon or the if (odd.equals("AT417")) case executes an empty statement, and any case the unnecessarily {} enclosed set = 13.00 * quant + set; which will mess up all your results.

Sign up to request clarification or add additional context in comments.

Comments

1

I really recommend formatting the code.

else if(odd.equals("AT517"));

Has a ;

So does

else if(odd.equals("AT617"));

3 Comments

Sorry about the non-formatted code, I thought I had it but apparently not. It's formatted right on my computer, can't believe I missed that...thanks for your help!
A tip is to use a text editor that allows multi-tab (selecting all code and add an extra tab before you paste it here, as it is required 4 spaces or a tab to be properly indented here.
@user2808951 or use eclipse, that has a warning specifically for ; after ifs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.