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.