0

I'm trying to understand the switch statement. So I have this problem which I solved already. "A software company sells a package that retails for $99. Quantity discounts are given according to the following:

10-19 = 20%
20-49 = 30%
50-99 = 40%
100 or more = 50%

Write a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total Amount of the purchase after the discount.

I solved it using an if else if structure and a couple relational operators, it looks like this

//Determine total price based on discounts
    if (x >= 10 && x <= 19)
    {
        total = (((x*99) - (x * 99)* .2));
        JOptionPane.showMessageDialog(null, "Your total is $" + total 
                + " with a 20% discount");
    }
    else if(x >= 20 && x <= 49)
    {
        total = (((x*99) - (x * 99)* .3));
        JOptionPane.showMessageDialog(null, "Your total is $" + total 
                + " with a 30% discount.");
    .
    .
    .

I would like to know if its possible to store the possible range of numbers in a single variable and then use it in for the case statements? Would it make sense to use the switch statement in this case? I tried fitting the range of possible numbers(essentially an expression, stored in a variable declared as Boolean) in a variable but since I declared the variable(x) the parsed Integer value of the whatever number the user inputs for the JOptionPane input dialog box it won't let me use a boolean variable. So I'm still a bit confused on how exactly the switch statement works, but I would appreciate any help on what goes and what doesn't when using a switch statement.

Varaquilex
  • 3,447
  • 7
  • 40
  • 60
user2855985
  • 23
  • 1
  • 4

2 Answers2

4

how about

if(total >= 100)
{
    //use 50%
}
else if(total >= 50)
{
    //use 40%
}
else if(total >= 20)
{
    //use 30%
}
else if(total >= 10)
{
    //use 20%
}
else
{
    //no discount
}

If you reach the else if for total >= 50, you already know that total < 100. and so on. This is really what makes use of a good if..else if..else statement, when you can infer conditions based on the previous statements. Your code right now would be equivalent if you used just if statements.

Also

total = (((x*99) - (x * 99)* .2));

would be better written as

total = x * 99 * (1-.2)
Cruncher
  • 7,641
  • 1
  • 31
  • 65
  • Thanks @cruncher. I have a question, I'm obviously not a mathematician, may I ask why it is better written as x*99*(1-.2), I tried it out and you're correct, it gives the same answer as in the much longer way I wrote it. But I'm having trouble understanding the 1-.2 part. – user2855985 Oct 07 '13 at 20:06
  • @user2855985 Most people would consider the second form easier to read and understand. From algebra we know that (X+Y) * Z == X*Z + Y*Z. Cruncher used this to simplify the expression. – Paul Wagland Oct 07 '13 at 20:09
  • @user2855985 as PaulWagland said, this is true by the distributive(and the commutative property of multiplication as I dropped some parentheses) property. This isn't exactly how I look at it though. With percentages specifically, I intuitively look at it as: `20% off the price, is the same as 80% of the price`. Also in this case you can just evaluate 1-.2 to .8, but the compiler should do that for you. Leaving it in this form makes it easier to introduce a variable later to represent the percentage. – Cruncher Oct 07 '13 at 20:13
  • @PaulWagland I would also suspect this would run faster (though probably almost negligible). Not sure how much java would optimize x*99 occurring twice in an expression.. – Cruncher Oct 07 '13 at 20:21
  • Ok, I can see why it's easier that way, when you say that it makes it easier to use the expression in a variable that will rep. the percentage, do you mean like initializing a variable with the x*(1-.2) as in: double percent = x * (1-.2) ? – user2855985 Oct 07 '13 at 20:24
  • @user2855985 I mean if you were to declare your percentages for each range as a `static final`(often done so you can see the constants that your program uses at a glance). Then the line would be something like: `total = x * 99 * (1 - RATE1)`. It's really not necessary, just a style thing. Leaving the 1-.2 there really helps you see the 20% though than if you were to just write .8 – Cruncher Oct 07 '13 at 20:28
1

No, switch statements do not support ranges.

Think about it from the opposite end, consider storing your available discounts in an enum and have an abstract applyDiscount method that checks if each discount is applicable and applies it to the amount.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166