Small Legend
Small Legend

Reputation: 688

Make if statement neater, unsure how to create a neat loop JAVA programming

  @Override
    int getDiscountRate(float priceThisYear, float priceLastYear) {
         float totalPrice;
        totalPrice = priceThisYear + priceLastYear;
        if (totalPrice >= 250 && totalPrice < 350) {
            return 5;
        } else if (totalPrice >= 350 && totalPrice < 450) {
            return 6;
        } else if (totalPrice >= 450 && totalPrice < 550) {
            return 7;
        } else if (totalPrice >= 550 && totalPrice < 650) {
            return 8;
        } else if (totalPrice >= 650 && totalPrice < 750) {
            return 9;
        } else if (totalPrice >= 750 && totalPrice < 850) {
            return 10;
        } else if (totalPrice >= 850 && totalPrice < 950) {
            return 11;
        } else if (totalPrice >= 950 && totalPrice < 1050) {
            return 12;
        } else if (totalPrice >= 1050 && totalPrice < 1150) {
            return 13;
        } else if (totalPrice >= 1150 && totalPrice < 1250) {
            return 14;
        } else if (totalPrice >= 1250 && totalPrice < 1350) {
            return 15;
        } else if (totalPrice >= 1350 && totalPrice < 1450) {
            return 16;
        } else if (totalPrice >= 1450 && totalPrice < 1550) {
            return 17;
        } else if (totalPrice >= 1550 && totalPrice < 1650) {
            return 18;
        } else if (totalPrice >= 1650 && totalPrice < 1750) {
            return 19;
        } else if (totalPrice >= 1750 && totalPrice < 1850) {
            return 20;
        }
        else if (totalPrice > 1850){
        return 20;
        }
          return 0;
    }

Trying to make the code look neater, preferably by decreasing the number of else ifs and maybe implementing some form of loop.

The idea is to have a discount rate which starts at 5 percent when the user has spent over $350 and then a further 1 percent with every $100 spent giving a maximum discount of 20 percent

I understand to experienced programmers this is most likely trivial. The program itself works but I know that can't be the best way to code it.

Upvotes: 0

Views: 181

Answers (5)

Boris the Spider
Boris the Spider

Reputation: 61168

Disclaimer I wrote this against the code (which gives a 5% discount for purchases 250 <= totalPrice < 350) and not the spec, which says the 5% should start at 350.

First, you are rechecking bounds; for example:

if (totalPrice >= 250 && totalPrice < 350) {
        return 5;
} else if (totalPrice >= 350 && totalPrice < 450) {

We know that, if we have not entered the first if, the amount must be >= 350 unless it is < 250. So if you checked that 250 condition first then you could certainly cut down on code:

if (totalPrice < 250) {
   return 0;
} else if (totalPrice < 350) {
   return 5;
} else if (totalPrice < 450) {
    return 6;

So, what are we actually doing here? We're looking for bounds, how about a Map<Integer, Integer> where we map minSpend -> discount. We would need to use a TreeMap to maintain order:

final Map<Integer, Integer> discounts = new TreeMap<>();
discounts.put(250, 5);
discounts.put(350, 6);
//and so on

In order to find the discount, we loop backwards over the Map and find the first key which is less than totalPrice:

for(final Entry<Integer, Integer> e : discounts.descendingMap().entrySet()) {
    if(e.geyKey() <= totalPrice) {
        return e.getValue();
    }
}
return 0;

This allows for a completely flexible bound to discount mapping. If you have some fixed rule, which you seem to, then you can simply describe it mathematically.

You have "5 at 250 then another 1 for each 100" which comes down to:

return totalPrice < 250 ? 0 : 5 + (totalPrice - 250)/100

Upvotes: 1

Aify
Aify

Reputation: 3537

Req: 5% at 350 spent, +1% per 100 after that

Step 1: Store the amt spent. Step 2: Check the amt spent, and apply necessary discounts. I would use a subtraction/mod approach. Step 3: return the discount percentage.

Implementation:

public static int discountPrice(int moneySpent) {
    int percentage = 0;

    // get out early if it's less than the needed amt
    if (moneySpent < 350) {
        return percentage;
    }
    // if at least 350 was spent
    if (moneySpent >= 350) {
        percentage += 5; // add 5 to the percentage
        moneySpent -= 350; // subtract 350 since we've already accounted for it
    }
    // while loop to calculate bonus percentage
    while (moneySpent >= 100) {
        percentage += 1; // add one to the percentage.
        moneySpent -= 100;
    }

    // return a max of 20
    if (percentage > 20) { percentage = 20; }
    return percentage;
}

Putting it in a function like this one makes it easy to change price - discount values. Base percentage can be modified easily in the second if block, as can the minimum money needed to be spent (first and second). Percentage increases per x price can also be easily modified in the last block.

Upvotes: 1

JFPicard
JFPicard

Reputation: 5168

If I saw correctly, it's the floor of (value - 250) / 75

Maybe try:

int minDiscount = 5;

if (totalPrice < 250) {
   return 0;
}

return Math.Floor((totalPrice - 250) / 75) + minDiscount;

Upvotes: 0

Marcus Widegren
Marcus Widegren

Reputation: 540

I'd suggest something like this:

if(totalPrice >= 350) {
  int baseDiscount = 5;
  int additionalDiscount = (totalPrice - 350) / 100;
  if(additionalDiscount > 15) additionalDiscount = 15;

  return baseDiscount + additionalDiscount;
}
return 0;

Then we simply follow what you said in words. 5 percent if above 350, then 1 percent additional for each 100$ above that. And I added the "max 20%" rule that you had in your code. :)

(haven't tested it)

Upvotes: 2

Kevin Workman
Kevin Workman

Reputation: 42174

The idea is to have a discount rate which starts at 5 percent when the user has spent over $350 and then a further 1 percent with every $100 spent

You can just do something like this:

int spent = 500;
if(spent > 350){
   int discount = 5 + (spent-350)/100;
}

Might require some tweaking, but you get the basic idea. Also note that the code you posted does not agree with your above requirement.

Upvotes: 0

Related Questions