Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Nickelis Jarvis
- Date: 9.24.14
- Program Name: Shipping Calculator
- Description: Calculate shipping charges or flag if too heavy or shipped too close.
- */
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main() {
- //Declare integers for weight and distance, no need to initialize; will be done later.
- int packageWeight, shipDistance;
- //Declare constant integer variables for minimum distance, maximum distance, modulus
- //(length of distance), maximum package weight, and output formatting (width of fill line and column widths)
- const int
- shipDistance_MIN = 10,
- shipDistance_MOD = 500,
- shipDistance_MAX = 3000,
- packageWeight_MAX = 50,
- fillWidth = 66,
- columnWidth[2] = { 40, 4 };
- //Declare constant double precision floating-point array, initialize with set of shipping rates; declare pointer to array afterwards for
- //stepping through array (used in if-else statement later)
- const double shipRate[4] = {
- 1.10,
- 2.20,
- 3.70,
- 4.80
- }, *p_shipRate = shipRate;
- //Output UI, format with fill lines, set column widths for clarity.
- cout << "Fast Freight Shipping Company Rates" << endl;
- cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
- cout << setw(columnWidth[0]) << left << "Weight of Package" << "Rate per 500 miles shipped" << endl;
- cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
- cout << fixed << setprecision(2);
- cout << setw(columnWidth[0]) << left << "5 pounds or less" << "$" << setw(columnWidth[1]) << shipRate[0] << endl;
- cout << setw(columnWidth[0]) << left << "Over 5 pounds but not more than 15" << "$" << setw(columnWidth[1]) << shipRate[1] << endl;
- cout << setw(columnWidth[0]) << left << "Over 15 pounds but not more than 30" << "$" << setw(columnWidth[1]) << shipRate[2] << endl;
- cout << setw(columnWidth[0]) << left << "Over 30 pounds but not more than 50" << "$" << setw(columnWidth[1]) << shipRate[3] << endl;
- cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
- cout << setw(8) << "NOTICE: We do not ship packages over 50 lbs" << endl;
- cout << setw(8) << "" << "We do not ship less than 10 miles or more than 3,000" << endl;
- cout << setfill('_') << setw(fillWidth) << "" << setfill(' ') << endl;
- //Get package weight.
- cout << "Enter package weight: ";
- cin >> packageWeight;
- //Get shipping distance.
- cout << "Enter shipping distance: ";
- cin >> shipDistance;
- //if-else statement, check each expression and increment pointer location in shipRate[]; this sets the proper ship rate for weight.
- //else executes if weight is below 0 (negative weight) or over 50lbs (too heavy)
- //if too heavy, wait for keyboard input then exit.
- //NOTE: Would have liked to check for overweight package first, but not allowed as per assignment instructions.
- if (packageWeight >= 0 && packageWeight <= 5)
- p_shipRate = 0;
- if (packageWeight > 5)
- p_shipRate++;
- if (packageWeight > 15)
- p_shipRate++;
- if (packageWeight > 30 && packageWeight < 50)
- p_shipRate++;
- else {
- cout << "Cannot ship, too heavy." << endl;
- system("pause");
- return(0);
- }
- //Check if the shipping distance is too close or too far.\
- //If true, wait for keyboard input then exit.
- if (shipDistance < shipDistance_MIN || shipDistance > shipDistance_MAX){
- cout << "Cannot ship, too close / far.";
- system("pause");
- return(0);
- }
- //Divide shipping distance into lengths of 500 miles, if there is a remainder, increment shipLengths by 1 (round up).
- int shipLengths = shipDistance;
- shipLengths /= shipDistance_MOD;
- if (shipDistance % shipDistance_MOD)
- shipLengths++;
- //output shipping rate (selected by if-else statement above), number of 500-mile segments, and total cost to ship.
- cout << fixed << setprecision(2) << "Shipping rate: $" << *p_shipRate << endl;
- cout << "Number " << shipDistance_MOD << " mile segments: " << shipLengths << endl;
- cout << "Cost to ship " << packageWeight << " pound package: " << shipLengths * *p_shipRate << endl;
- system("pause");
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement