//Natalie Zarate CIS 5 Chapter 4, P. 222, #13
/*******************************************************************************
*
* COMPUTE SHIPPING CHARGES
* _____________________________________________________________________________
* This program accepts and as input the weight of the user's package and the
* distance the package is being shipped, validates it, then calculates and
* displays the shipping charges.
*
* Computation will be based on the following input restrictions:
* - The package cannot weight less than 0 kg or more than 20 kg
* - The package cannot be shipped a distance less than 10 mi or more than 3,000
* mi
* _____________________________________________________________________________
* INPUT
* packageW : Weight of the package
* shipDist : Shipping distance
* distMult : Times greater than 500
* rate_1 : Rate for packages that weight 0-2 kg
* rate_2 : Rate for packages that weight 2-6 kg
* rate_3 : Rate for packages that weight 6-10 kg
* rate_4 : Rate for packages that weight 10-20 kg
* rate_1Max : Max weight for rate_1
* rate_2Max : Max weight for rate_2
* rate_3Max : Max weight for rate_3
* rate_4Max : Max weight for rate_4
* lowerW : Min package weight
* upperW : Max package weight
* distRate : Miles rates are based on, (rate per 500 mi)
* lowerD : Min shipping distance
* upperD : Max shipping distance
*
* OUTPUT
* shipCharge : Cost of shipping
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
/**********************************************************************
* CONSTANTS
* --------------------------------------------------------------------
* rate_1 : Rate for packages that weight 0-2 kg
* rate_2 : Rate for packages that weight 2-6 kg
* rate_3 : Rate for packages that weight 6-10 kg
* rate_4 : Rate for packages that weight 10-20 kg
* rate_1Max : Max weight for rate_1
* rate_2Max : Max weight for rate_2
* rate_3Max : Max weight for rate_3
* rate_4Max : Max weight for rate_4
* lowerW : Min package weight
* upperW : Max package weight
* distRate : Miles rates are based on, (rate per 500 mi)
* lowerD : Min shipping distance
* upperD : Max shipping distance
* *******************************************************************/
const float rate_1 = 1.10;
const float rate_2 = 2.20;
const float rate_3 = 3.70;
const float rate_4 = 4.80;
const float rate_1Max = 2;
const float rate_2Max = 6;
const float rate_3Max = 10;
const float rate_4Max = 20;
const float lowerW = 0;
const float upperW = 20;
const int distRate = 500;
const float lowerD = 10;
const float upperD = 3000;
float packageW;
float shipDist;
float shipCharge;
int distMult;
// Prompt user for package weight
cout << "Enter package weight in kilograms: " << endl;
cin >> packageW;
// Validate input
if (packageW <= lowerW || packageW > upperW)
{
cout << "Package weight must be greater than 0 kg and less than 20 kg.";
cout << endl;
cout << "Enter package weight in kilograms: " << endl;
cin >> packageW;
}
// Prompt user for shipping distance
cout << "Enter shipping distance in miles:" << endl;
cin >> shipDist;
// Validate Inout
if (shipDist < lowerD || shipDist > upperD)
{
cout << "Shipping distance must be between 10 and 3000 miles." << endl;
cout << "Enter shipping distance in miles: " << endl;
cin >> shipDist;
}
// Compute shipping charges
if ((shipDist / distRate) < 1)
{
if (0 < packageW && packageW <= rate_1Max)
shipCharge = rate_1;
else if (packageW > rate_1Max && packageW <= rate_2Max)
shipCharge = rate_2;
else if (packageW > rate_2Max && packageW <= rate_3Max)
shipCharge = rate_3;
else
shipCharge = rate_4;
}
if ((shipDist / distRate) >= 1 && (shipDist / distRate) < 2)
{
if (0 < packageW && packageW <= rate_1Max)
shipCharge = rate_1 * 2;
else if (packageW > rate_1Max && packageW <= rate_2Max)
shipCharge = rate_2 * 2;
else if (packageW > rate_2Max && packageW <= rate_3Max)
shipCharge = rate_3 * 2;
else
shipCharge = rate_4 * 2;
}
if ((shipDist / distRate) >= 2 && (shipDist / distRate) < 3)
{
if (0 < packageW && packageW <= rate_1Max)
shipCharge = rate_1 * 3;
else if (packageW > rate_1Max && packageW <= rate_2Max)
shipCharge = rate_2 * 3;
else if (packageW > rate_2Max && packageW <= rate_3Max)
shipCharge = rate_3 * 3;
else
shipCharge = rate_4 * 3;
}
if ((shipDist / distRate) >= 3 && (shipDist / distRate) < 4)
{
if (0 < packageW && packageW <= rate_1Max)
shipCharge = rate_1 * 4;
else if (packageW > rate_1Max && packageW <= rate_2Max)
shipCharge = rate_2 * 4;
else if (packageW > rate_2Max && packageW <= rate_3Max)
shipCharge = rate_3 * 4;
else
shipCharge = rate_4 * 4;
}
if ((shipDist / distRate) >= 4 && (shipDist / distRate) < 5)
{
if (0 < packageW && packageW <= rate_1Max)
shipCharge = rate_1 * 5;
else if (packageW > rate_1Max && packageW <= rate_2Max)
shipCharge = rate_2 * 5;
else if (packageW > rate_3Max && packageW <= rate_3Max)
shipCharge = rate_3 * 5;
else
shipCharge = rate_4 * 5;
}
if ((shipDist / distRate) >= 5 && (shipDist / distRate) < 6)
{
if (0 < packageW && packageW <= rate_1Max)
shipCharge = rate_1 * 6;
else if (packageW > rate_1Max && packageW <= rate_2Max)
shipCharge = rate_2 * 6;
else if (packageW > rate_2Max && packageW <= rate_3Max)
shipCharge = rate_3 * 6;
else
shipCharge = rate_4 * 6;
}
// Output shipping charges
cout << "Shipping: $";
cout << setprecision(3) << showpoint << shipCharge << endl;
return 0;
}