ID:1334338
 
(See the best response by Jp.)
Code:
double applyDiscounts(int orderAmt);

int main()
{
const double tier0Cost = 0.95; //normal price per product
const double tier1Cost = 0.9; //cost per product over 1,000th
const double tier2Cost = 0.8; //cost per product over 5,000th
double final_cost = 0.0;
int orderAmt = 0;
cout<<"Enter order amount: ";
cin>>orderAmt;
final_cost = applyDiscounts(orderAmt);
cout<<"Final cost is "<<final_cost;
system("pause");
return 0;
}

//I then have an 'apply discounts' method that takes
//the current order amount for the argument
double applyDiscounts(int orderAmt)
{
double total = 0.0;
if(orderAmt <= 1000)
{
total += orderAmt*tier0Cost;
}
else if(orderAmt > 1000 && orderAmt <= 5000)
{
total += 1000*tier0Cost;
total += (orderAmt-1000)*tier1Cost;
}
else if(orderAmt > 5000)
{
total += 1000*tier0Cost;
total += 4000*tier1Cost;
total += (orderAmt-5000)*tier2Cost;
}
return total;
}


Problem description:

Above is the snippet I use to accomplish the following task. Applying different discount rates to different amounts of ordered product. As of right now, I Just use a chain of if/else if/else, but I can't help but think there is a better way of handling this scenario.

How the discounts work, is that for the first 1,000 product ordered, the normal price is applied. For every product ordered over 1,000 (so #1,001 and above), the tier 1 cost is applied, and so on.

For example, if I were to order 1,500 product, 1,000 of it would cost the normal 0.95 per, while the final 500 of it would cost 0.9 per, for a total of (1000*0.95)+(500*0.9)=(950)+(450)=1,400 for the final cost.

EDIT
There may be a couple minor syntax errors from my writing this up real quick. This isn't the exact program, but instead just something I wrote just now to get the idea across.
You got it down pretty nice, I doubt there's another way to do it that's less complicated than what you have here.
Best response
total += std::min(1000, amount) * tier0cost + std::min(4000, std::max(0, amount - 1000)) * tier1cost + std::max(0, amount - 5000)*tier2cost


But what you've got is much, much more readable. Also presumably you're sending std::endl after cout-ing and have a using clause and so on, but that's minor.
I fail to see how that even compiles, but that's not the point.

If you define two arrays and just use loop to search through it'll be possible to apply this to any number of discounts.
In response to Zaoshi
How would this not compile? Obviously I excluded the #include <iostream> and using namespace std;, but that's not the point of the post.

Unless you're referring to the fact that I put the const variables inside main() instead of outside main() like a smarter person would haha.
In response to Jp
I actually like your idea of using min/max instead of a chain of if/else if/else statements. However, I spread each one out into its own line rather than what you did, just to make it 100x easier to read. I've never been a fan of squishing as much as possible onto a single line of code for the sake of saving lines of code haha.
//apply normal price
total += min(amount, 1000)*tier0Cost;
//apply tier 1 discount
total += min(4000, max(0, amount-1000))*tier1Cost;
//apply tier 2 discount
total += max(0, amount-5000)*tier2Cost;