Tuesday, November 22, 2016

Programming Challenge 4.15 - Shipping Charges

/* Shipping Charges - The Fast Freight Shipping Company charges the following
   rates:
  
   Weight of Package (in kilograms)            Rate per 500 Miles Shipped
   * 2kg or less                            $1.10
   * Over 2kg but not more than 6kg            $2.20
   * Over 6kg but not more than 10kg        $3.70
   * Over 10kg but not more than 20kg        $4.80

   This program asks for the weight of the package and the distance it is to
   be shipped, and then displays the charges.

   Input Validation: Values of 0 kg or less or more than 20kg for the weight of
   the package are not accepted. Distances of less than 10 miles or more than
   3.000 miles are accepted. */

#include "Utility.h"

int main()
{
    /* Constants for minimum and maximum weight */
    const int WEIGHT_MIN = 1;
    const int WEIGHT_MAX = 20;

    /* Constants for minimum and maximum shipping distance */
    const double SHIPPING_DISTANCE_MIN = 10;
    const double SHIPPING_DISTANCE_MAX = 3000;

    /* Hold shipping charge and total shipping cost */
    double shippingCharge = 0,
           totalShippingCost;

    /* Hold package weight */
    int packageWeight;

    /* Hold shipping distance */
    double shippingDistance;

    /* Ask the user for package weight and shipping distance */
    cout << "Enter the weight of your package (in kg): ";
    cin >> packageWeight;
    cout << "Enter the shipping distance: ";
    cin >> shippingDistance;

    /* Determine charges for shipping per 500 miles based on package weight */
    if (packageWeight > 0 && packageWeight <= 2)
    {
        shippingCharge = 1.10;
    }
    else if (packageWeight >= 2 && packageWeight < 6)
    {
        shippingCharge = 2.20;
    }
    else if (packageWeight >= 6 && packageWeight < 10)
    {
        shippingCharge = 3.70;
    }
    else if (packageWeight >= 10 && packageWeight <= 20)
    {
        shippingCharge = 4.80;
    }

    /* Format Output */
    cout << fixed << showpoint << setprecision(2);

    /* Determine whether minimum and maximum weight and distance conditions
       are met and display distances and charges */
    (packageWeight >= WEIGHT_MIN && packageWeight <= WEIGHT_MAX) ?
        cout << "Your package has a weight of " << packageWeight << "kg.\n"
             << "You have to pay an extra of $" << shippingCharge
             << " per 500 miles.\n" :
        cout << "Your package weighs less than " << WEIGHT_MIN
             << " or more than " << WEIGHT_MAX << "kg.\n"
             << "We only deliver packages with weights between 1 and 20kg.\n";

    (shippingDistance >= SHIPPING_DISTANCE_MIN && shippingDistance <= SHIPPING_DISTANCE_MAX) ?
        cout << "The shipping distance is " << shippingDistance << " miles.\n" :
        cout << "The shipping distance is below " << SHIPPING_DISTANCE_MIN << " miles.\n"
             << "We only deliver to distances between 10 and 3,000 miles.\n";

    pauseSystem();
    return 0;
}

No comments:

Post a Comment