//Sam Partovi CS1A Chapter 4, P. 221, #10
//
/*******************************************************************************
*
* COMPUTE QUANTITY-BASED DISCOUNT
* ____________________________________________________________
* This program computes discount percentage and total cost based on a given
* item quantity, with varying discounts for multiple quantity ranges.
*
* Discounts are determined by the following table:
* 0 - 9 units = 0% discount 10 - 19 units = 20% discount
* 20 - 49 units = 30% discount 50 - 99 units = 40% discount
* 100+ units = 50% discount
*
* The total purchase price is calculated with the following formula:
* totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal)
*
* The total savings are calulated with the following formula:
* totalSavings = (unitsSold * itemPrice * discountDecimal)
* ____________________________________________________________
* INPUT
* unitsSold : Quantity of units to be sold
* discountDecimal : Discount expressed as a decimal
* itemPrice : Price per item
*
* OUTPUT
* discountPercentage : Discount expressed as a percentage
* totalCost : Total purchase price after realizing discount
* totalSavings : Total savings after realizing discount
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int unitsSold; //INPUT - Quantity of units to be sold
float discountDecimal; //INPUT - Discount expressed as a decimal
float discountPercentage; //OUTPUT - Discount expressed as a percentage
float totalCost; //OUTPUT - Total purchase price after realizing discount
float totalSavings; //OUTPUT - Total savings after realizing discount
//
//Initialize program variables
const float itemPrice = 99.00;
discountDecimal = 0.00; //Before receiving user input, discount is 0%.
//
//Prompt user to input unitsSold
cout << "Enter the quantity of software packages to purchase: \n";
cin >> unitsSold;
//
//Compute discount, savings, and total for 0-9 units
if (unitsSold > 0 and unitsSold < 10) {
totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
totalSavings = (unitsSold * itemPrice * discountDecimal);
cout << "Your purchase of " << unitsSold <<
" unit(s) does not qualify for a discount.\n";
cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".\n";
}
//
//Compute discount, savings, and total for 10-19 units
else if (unitsSold >= 10 and unitsSold <= 19) {
discountDecimal += 0.20;
discountPercentage = discountDecimal * 100;
totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
totalSavings = (unitsSold * itemPrice * discountDecimal);
cout << "You are receiving a " << discountPercentage << "% discount, saving $"
<< fixed << setprecision(2) << totalSavings << ".\n";
cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
}
//
//Compute discount, savings, and total for 20-49 units
else if (unitsSold > 19 and unitsSold <= 49) {
discountDecimal += 0.30;
discountPercentage = discountDecimal * 100;
totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
totalSavings = (unitsSold * itemPrice * discountDecimal);
cout << "You are receiving a " << discountPercentage << "% discount, saving $"
<< fixed << setprecision(2) << totalSavings << ".\n";
cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
}
//
//Compute discount, savings, and total for 49-99 units
else if (unitsSold > 49 and unitsSold <= 99) {
discountDecimal += 0.40;
discountPercentage = discountDecimal * 100;
totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
totalSavings = (unitsSold * itemPrice * discountDecimal);
cout << "You are receiving a " << discountPercentage << "% discount, saving $"
<< fixed << setprecision(2) << totalSavings << ".\n";
cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
}
//
//Compute discount, savings, and total for 99+ units
else if (unitsSold > 99) {
discountDecimal += 0.50;
discountPercentage = discountDecimal * 100;
totalCost = (unitsSold*itemPrice) - (unitsSold * itemPrice * discountDecimal);
totalSavings = (unitsSold * itemPrice * discountDecimal);
cout << "You are receiving a " << discountPercentage << "% discount, saving $"
<< fixed << setprecision(2) << totalSavings << ".\n";
cout << "Your total is $" << fixed << setprecision(2) << totalCost << ".";
}
//
//Input validation: Display error if quantity < 0
else if (unitsSold <= 0) {
cout << "\nError! Your purchase must be greater than 0 units.";
}
return 0;
}