Advertisement
hfxdesign

Shipping Calculator

Sep 25th, 2014
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. /*
  2. Name: Nickelis Jarvis
  3. Date: 9.24.14
  4. Program Name: Shipping Calculator
  5. Description: Calculate shipping charges or flag if too heavy or shipped too close.
  6. */
  7.  
  8. #include <iostream>
  9. #include <iomanip>
  10.  
  11. using namespace std;
  12.  
  13. int main() {
  14.     //Declare integers for weight and distance, no need to initialize; will be done later.
  15.     int packageWeight, shipDistance;
  16.  
  17.     //Declare constant integer variables for minimum distance, maximum distance, modulus
  18.     //(length of distance), maximum package weight, and output formatting (width of fill line and column widths)
  19.     const int
  20.         shipDistance_MIN = 10,
  21.         shipDistance_MOD = 500,
  22.         shipDistance_MAX = 3000,
  23.         packageWeight_MAX = 50,
  24.         fillWidth = 66,
  25.         columnWidth[2] = { 40, 4 };
  26.  
  27.     //Declare constant double precision floating-point array, initialize with set of shipping rates; declare pointer to array afterwards for
  28.     //stepping through array (used in if-else statement later)
  29.     const double shipRate[4] = {
  30.         1.10,
  31.         2.20,
  32.         3.70,
  33.         4.80
  34.     }, *p_shipRate = shipRate;
  35.  
  36.     //Output UI, format with fill lines, set column widths for clarity.
  37.     cout << "Fast Freight Shipping Company Rates" << endl;
  38.     cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
  39.     cout << setw(columnWidth[0]) << left << "Weight of Package" << "Rate per 500 miles shipped" << endl;
  40.     cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
  41.     cout << fixed << setprecision(2);
  42.     cout << setw(columnWidth[0]) << left << "5 pounds or less" << "$" << setw(columnWidth[1]) << shipRate[0] << endl;
  43.     cout << setw(columnWidth[0]) << left << "Over 5 pounds but not more than 15" << "$" << setw(columnWidth[1]) << shipRate[1] << endl;
  44.     cout << setw(columnWidth[0]) << left << "Over 15 pounds but not more than 30" << "$" << setw(columnWidth[1]) << shipRate[2] << endl;
  45.     cout << setw(columnWidth[0]) << left << "Over 30 pounds but not more than 50" << "$" << setw(columnWidth[1]) << shipRate[3] << endl;
  46.     cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
  47.     cout << setw(8) << "NOTICE: We do not ship packages over 50 lbs" << endl;
  48.     cout << setw(8) << "" << "We do not ship less than 10 miles or more than 3,000" << endl;
  49.     cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
  50.  
  51.     //Get package weight.
  52.     cout << "Enter package weight: ";
  53.     cin >> packageWeight;
  54.  
  55.     //Get shipping distance.
  56.     cout << "Enter shipping distance: ";
  57.     cin >> shipDistance;
  58.  
  59.     //if-else statement, check each expression and increment pointer location in shipRate[]; this sets the proper ship rate for weight.
  60.     //else executes if weight is below 0 (negative weight) or over 50lbs (too heavy)
  61.     //if too heavy, wait for keyboard input then exit.
  62.     //NOTE: Would have liked to check for overweight package first, but not allowed as per assignment instructions.
  63.     if (packageWeight >= 0 && packageWeight <= 5)
  64.         p_shipRate = 0;
  65.     if (packageWeight > 5)
  66.         p_shipRate++;
  67.     if (packageWeight > 15)
  68.         p_shipRate++;
  69.     if (packageWeight > 30 && packageWeight < 50)
  70.         p_shipRate++;
  71.     else {
  72.         cout << "Cannot ship, too heavy." << endl;
  73.         system("pause");
  74.         return(0);
  75.     }
  76.  
  77.     //Check if the shipping distance is too close or too far.\
  78.     //If true, wait for keyboard input then exit.
  79.     if (shipDistance < shipDistance_MIN || shipDistance > shipDistance_MAX){
  80.         cout << "Cannot ship, too close / far.";
  81.         system("pause");
  82.         return(0);
  83.     }
  84.  
  85.     //Divide shipping distance into lengths of 500 miles, if there is a remainder, increment shipLengths by 1 (round up).
  86.     int shipLengths = shipDistance;
  87.     shipLengths /= shipDistance_MOD;
  88.     if (shipDistance % shipDistance_MOD)
  89.             shipLengths++;
  90.  
  91.     //output shipping rate (selected by if-else statement above), number of 500-mile segments, and total cost to ship.
  92.     cout << fixed << setprecision(2) << "Shipping rate: $" << *p_shipRate << endl;
  93.     cout << "Number " << shipDistance_MOD << " mile segments: " << shipLengths << endl;
  94.     cout << "Cost to ship " << packageWeight << " pound package: " << shipLengths * *p_shipRate << endl;
  95.  
  96.     system("pause");
  97.  
  98.     return(0);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement