Thursday, January 19, 2017

Programming Challenge 6.25 - Travel Expenses

/* Travel Expenses - This program calculates and displays the total travel
   expenses of a businessperson on a trip. The program has functions that
   ask for and return the following:
  
      * The total number of days spent on a trip
      * The time of departure on the first day of the trip, and the time of
        arrival back home on the last day of the trip
      * The amount of any round-trip airfare
      * The amount of any car rentals
      * Miles driven, if a private vehicle was used
       
        * The vehicle expense is calculated as $0.27 per mile driven

      * Parking fees

         * The company allows up to $6.00 per day. Anything in excess of
           this must be paid by the employee.

      * Taxi fees if a taxi was used anytime during the trip.

         * The company allows up to $10.00 per day, for each day a taxi
           was used. Anything in excess must be paid by the employee.

      * Conference and seminar registration fees
      * Hotel expenses

         * The company allows up to $90.00 per night for lodging. Anything
           in excess must be paid by the employee.

      * The amount of each meal eaten.

         * On the first day of the trip, breakfast is allowed as an expense
           if the time of departure is before 7 a.m.

         * Lunch is allowed if the time of departure is before 12 noon.

         * Dinner is allowed on the first day if the departure is before
           6 p.m.

         * On the last day of the trip, breakfast is allowed if the time of
           arrival is after 8 a.m.

         * Lunch is allowed if the time of arrival is after 1 p.m.

         * Dinner is allowed on the last day if the time of arrival is after
           7 p.m.

      * The program only asks for the amounts of allowable meals
     
         * The company allows up to $9.00 for breakfast
         * The company allows up to $12.00 for lunch
         * The company allows up to $16.00 for dinner

        Anything in excess must be paid by the employee.

   Input Validation: No negative numbers for any dollar amounts, or for miles
   driven in a private vehicle are allowed. No numbers less than 1 for the
   number of days is allowed. Only valid times for the time of departure and
   the time of arrival are accepted. */

#include "Utility.h"

/* Prototypes: Number of days, */
int numDays(int);
double getDepartureTime(double);
double getArrivalTime(double);
double getRoundTripFares(double);
double getCarRentalCost(double);
double getMilesDriven(double);
double getParkingFees(double);
double getTaxiFees(double);
double getRegistrationFees(double);
double getHotelExpenses(double);

double getAllowableMeals(double, double, double &, double &, double &,
                         double &, double &, double &);

void calcTotalCost(int, double, double, double, double, double, double,
                     double, double, double, double, double, double,
                     double, double, double, double miles = 0.27,
                     double parking = 6.0, double taxi = 10.0,
                     double hotel = 90.0, double breakfast = 9.0,
                     double lunch = 12.0, double dinner = 16.0);

int main()
{
   /* Variables: Days on trip */
   static int daysOnTrip = 0;

   /* Variables: Time of departure, Time of arrival, Round trip fares,
                 Car rental cost, Miles driven in private car, parking
                 fees, Taxi fees, Registration fees, Hotel expenses,
                 Breakfast on the first day (if applicable), Lunch on
                 first day (if applicable), Dinner on first day (if
                 applicable), Breakfast last day (if applicable),
                 Lunch last day (if applicable), Dinner on last day
                 (if applicable) */
   static double timeOfDeparture = 0.0,
                 timeOfArrival = 0.0,
                 roundTripFares = 0.0,
                 carRentalCost = 0.0,
                 milesDriven = 0.0,
                 parkingFees = 0.0,
                 taxiFees = 0.0,
                 registrationFees = 0.0,
                 hotelExpenses = 0.0,
                 breakfastFirstDay = 0.0,
                 lunchFirstDay = 0.0,
                 dinnerFirstDay = 0.0,
                 breakfastLastDay = 0.0,
                 lunchLastDay = 0.0,
                 dinnerLastDay = 0.0,
                 allowableMeals = 0.0;

   /* Display: Table header */
   cout << "\t\tAsahaka Chemical Corp. Business Trip Calculator\n\n";

   /* Call: numDays, getDepartureTime, getArrivalTime, getRoundTripFares,
            getCarRentalCost, getMilesDriven, getParkingFees, getTaxiFees,
            getRegistrationFees, getHotelExpenses, getAllowableMeals,
            calcTotalCost */
   daysOnTrip = numDays(daysOnTrip);
   timeOfDeparture = getDepartureTime(timeOfDeparture);
   timeOfArrival = getArrivalTime(timeOfArrival);
   roundTripFares = getRoundTripFares(roundTripFares);
   carRentalCost = getCarRentalCost(carRentalCost);
   milesDriven = getMilesDriven(milesDriven);
   parkingFees = getParkingFees(parkingFees);
   taxiFees = getTaxiFees(taxiFees);
   registrationFees = getRegistrationFees(registrationFees);
   hotelExpenses = getHotelExpenses(hotelExpenses);

   allowableMeals = getAllowableMeals(timeOfDeparture, timeOfArrival,
                                      breakfastFirstDay, lunchFirstDay,
                                      dinnerFirstDay, breakfastLastDay,
                                      lunchLastDay, dinnerLastDay);

   calcTotalCost(daysOnTrip, timeOfDeparture, timeOfArrival, roundTripFares,
                 carRentalCost, milesDriven, parkingFees, taxiFees,
                 registrationFees, hotelExpenses, breakfastFirstDay,
                 lunchFirstDay, dinnerFirstDay, breakfastLastDay,
                 lunchLastDay, dinnerLastDay);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: numDays

   Gets and returns the number of days spent on the business
   trip.
   ********************************************************** */

int numDays(int daysOnTrip)
{
   /* Get: Number of days on trip */
   cout << "How many days did you spend on this business trip? ";
   cin >> daysOnTrip;

   /* Validate: Input */
   while (daysOnTrip < 1)
   {
      cout << "\nInvalid input. The number of days spent on a trip\n"
           << "has to be greater than or equal to 1.\n"
           << "How many days have you spent on this business trip? ";
      cin >> daysOnTrip;
   }

   /* Return: daysOnTrip */
   return daysOnTrip;
}

/* **********************************************************
   Definition: getDepartureTime

   Gets and returns the departure time.
   ********************************************************** */

double getDepartureTime(double departureTime)
{
   /* Get: Departure time */
   cout << "\nAt what time was your departure on the first day? ";
   cin >> departureTime;
  
   /* Validate: Input */
   while (departureTime < 0 || departureTime > 24.00)
   {
      cout << "\nInvalid input. The departure time can not be\n"
           << "negative. Example Input: 00.00 - 24.00\n"
           << "At what time was your departure on the first day? ";
      cin >> departureTime;
   }
  
   /* Return: departureTime */
   return departureTime;
}

/* **********************************************************
   Definition: getDepartureTime

   Gets and returns the time of arrival.
   ********************************************************** */

double getArrivalTime(double arrivalTime)
{
   /* Get: Arrival time */
   cout << "\nAt what time did you arrive back home? ";
   cin >> arrivalTime;

   /* Validate: Input */
   while (arrivalTime < 0 || arrivalTime > 24.00)
   {
      cout << "\nInvalid input. The arrival time can not be\n"
           << "negative or greater than 24.00.\n"
           << "At what time did you arrive back home? ";
      cin >> arrivalTime;
   }

   /* Return: Arrival time */
   return arrivalTime;
}

/* **********************************************************
   Definition: roundTripFare

   Gets and returns the round trip airfares (if any).
   ********************************************************** */

double getRoundTripFares(double roundTripFares)
{
   /* Get: Round trip fares */
   cout << "\nWere there any round trip airfares? $ ";
   cin >> roundTripFares;

   /* Validate: Input */
   while (roundTripFares < 0)
   {
      cout << "\nInvalid input. The amount of airfares can not\n"
           << "be negative.\n"
           << "Were there any round trip airfares? $ ";
      cin >> roundTripFares;
   }

   /* Return: roundTripFares */
   return roundTripFares;
}

/* **********************************************************
   Definition: getCarRentalCost

   Gets and returns the car rental cost (if any).
   ********************************************************** */

double getCarRentalCost(double carRentalCost)
{
   /* Get: Car rental cost */
   cout << "\nHave you had any car rental cost? $ ";
   cin >> carRentalCost;

   /* Validate: Input */
   while (carRentalCost < 0)
   {
      cout << "\nInvalid input. The amount of car rental cost can not\n"
           << "be negative.\n"
           << "\nHave you had any car rental cost? $ ";
      cin >> carRentalCost;
   }

   /* Return: carRentalCost */
   return carRentalCost;
}

/* **********************************************************
   Definition: getMilesDriven

   Gets and returns the miles driven (if any) in a private
   car.
   ********************************************************** */

double getMilesDriven(double milesDriven)
{
   /* Get: Miles driven in private car */
   cout << "\nHow many miles have you driven in your own car? ";
   cin >> milesDriven;

   /* Validate: Input */
   while (milesDriven < 0)
   {
      cout << "\nInvalid input. The number of miles driven can not\n"
           << "be negative.\n\n"
           << "\nHow many miles have you driven in your own car? ";
      cin >> milesDriven;
   }

   /* Return: milesDriven */
   return milesDriven;
}

/* **********************************************************
   Definition: getParkingFees

   Gets and returns the parking fees (if any).
   ********************************************************** */

double getParkingFees (double parkingFees)
{
   /* Get: Parking fees */
   cout << "\nHave you had to pay any parking fees? $ ";
   cin >> parkingFees;

   /* Validate: Input */
   while (parkingFees < 0)
   {
      cout << "\nInvalid input. The parking fees can not be negative.\n\n"
           << "Have you had to pay any parking fees? $ ";
      cin >> parkingFees;
   }

   /* Return: parkingFees */
   return parkingFees;
}

/* **********************************************************
   Definition: getTaxiFees

   Gets and returns the taxi fees (if any).
   ********************************************************** */

double getTaxiFees (double taxiFees)
{
   /* Get: Taxi fees */
   cout << "\nWere there any taxi fees? $ ";
   cin >> taxiFees;

   /* Validate: Input */
   while (taxiFees < 0)
   {
      cout << "\nInvalid input. The taxi fees can not be negative.\n\n"
           << "Were there any taxi fees? $ ";
      cin >> taxiFees;
   }

   /* Return: taxiFees */
   return taxiFees;
}

/* **********************************************************
   Definition: getConferenceFees

   Gets and returns the conference fees (if any).
   ********************************************************** */

double getRegistrationFees (double registrationFees)
{
   /* Get: Conference and/or seminar registration fees */
   cout << "\nWere there any conference or seminar registration fees? $ ";
   cin >> registrationFees;

   /* Validate: Input */
   while (registrationFees < 0)
   {
      cout << "\nInvalid input. The registration fee can not be negative.\n"
           << "Were there any conference or seminar registration fees? $ ";
      cin >> registrationFees;
   }

   /* Return: registrationFees */
   return registrationFees;
}

/* **********************************************************
   Definition: getHotelExpenses

   Gets and returns the hotel expenses (if any).
   ********************************************************** */

double getHotelExpenses(double hotelExpenses)
{
   /* Get: Hotel expenses */
   cout << "\nHave you had any hotel expenses? $ ";
   cin >> hotelExpenses;

   /* Validate: Input */
   while (hotelExpenses < 0)
   {
      cout << "\nInvalid input. The hotel expenses can not be negative.\n"
           << "Have you had any hotel expenses? $ ";
      cin >> hotelExpenses;
   }

   /* Return: hotelExpenses */
   return hotelExpenses;
}

/* **********************************************************
   Definition: getAllowableMeals

   Gets and returns the amount spent for each allowable meal.
   ********************************************************** */

double getAllowableMeals(double timeOfDeparture, double timeOfArrival,
                         double &breakfastFirstDay, double &lunchFirstDay,
                         double &dinnerFirstDay, double &breakfastLastDay,
                         double &lunchLastDay, double &dinnerLastDay)
{
   /* Variable: Amount of allowable meals */
   double amountAllowableMeals = 0.0;

   /* Determine the departure and arrival times, so as to only get the
      necessary input for the applicable meals */
   if (timeOfDeparture > 07.00)
   {
      cout << "\nHow much did you pay for breakfast on your first day? $ ";
      cin >> breakfastFirstDay;
   }

   /* Validate: Input */
   while (breakfastFirstDay < 0.0)
   {
      cout << "\nInvalid input. The cost for breakfast can not be negative.\n"
           << "How much did you pay for breakfast on your first day? $ ";
      cin >> breakfastFirstDay;
   }

   if (timeOfDeparture < 12.00)
   {
      cout << "\nHow much did you pay for lunch on your first day? $ ";
      cin >> lunchFirstDay;
   }

   /* Validate: Input */
   while (lunchFirstDay < 0.0)
   {
      cout << "\nInvalid input. The cost for lunch can not be negative.\n"
           << "How much did you pay for lunch on your first day? $ ";
      cin >> lunchFirstDay;
   }

   if (timeOfDeparture < 18.00)
   {
      cout << "\nHow much did you pay for dinner on your first day? $ ";
      cin >> dinnerFirstDay;
   }

   /* Validate: Input */
   while (dinnerFirstDay < 0.0)
   {
      cout << "\nInvalid input. The cost for dinner can not be negative.\n"
           << "How much did you pay for dinner on your first day? $ ";
      cin >> dinnerFirstDay;
   }

   if (timeOfArrival < 08.00)
   {
      cout << "\nHow much did you pay for breakfast on your last day? $ ";
      cin >> breakfastLastDay;
   }

   /* Validate: Input */
   while (breakfastLastDay < 0.0)
   {
      cout << "\nInvalid input. The cost for breakfast can not be negative.\n"
           << "How much did you pay for breakfast on your last day? $ ";
      cin >> breakfastLastDay;
   }

   if (timeOfArrival > 13.00)
   {
      cout << "\nHow much did you pay for lunch on your last day? $ ";
      cin >> lunchLastDay;
   }

   /* Validate: Input */
   while (lunchLastDay < 0.0)
   {
      cout << "\nInvalid input. The cost for lunch can not be negative.\n"
           << "How much did you pay for lunch on your last day? $ ";
      cin >> lunchLastDay;
   }

   if (timeOfArrival > 19.00)
   {
      cout << "\nHow much did you pay for dinner on your last day? $ ";
      cin >> dinnerLastDay;
   }

   /* Validate: Input */
   while (dinnerLastDay < 0.0)
   {
      cout << "\nInvalid input. The cost for breakfast can not be negative.\n"
         << "How much did you pay for dinner on your last day? $ ";
      cin >> dinnerLastDay;
   }

   /* Return: amountAllowableMeals */
   return amountAllowableMeals;
}

/* **********************************************************
   Definition: calcTotalCost

   Accepts all input provided as arguments, calculates and
   displays a summary of the total cost of the business trip.
   ********************************************************** */

void calcTotalCost(int numDays, double timeDeparture, double timeArrival,
                   double costAirFare, double costCarRental, double costPerMile,
                   double costParking, double costTaxi, double costRegistration,
                   double costHotel, double costBreakfastFirst, double costLunchFirst,
                   double costDinnerFirst, double costBreakfastLast, double costLunchLast,
                   double costDinnerLast, double costMilesCovered, double costTaxiCovered,
                   double costParkingCovered, double costHotelCovered,
                   double costBreakfastCovered, double costLunchCovered,
                   double costDinnerCovered)
{
   /* Variables: Cost not covered for: Miles driven, Hotel expenses, Taxi,
                 Breakfast on first and last day, Lunch on first and last
                 day, Dinner on first and last day, Subtotal cost of the
                 trip, Total cost of the trip */
   double privateCostCar = 0.0,
          privateCostHotel = 0.0,
          privateCostTaxi = 0.0,
          privateCostBreakfast = 0.0,
          privateCostLunch = 0.0,
          privateCostDinner = 0.0,
          privateCostTotal = 0.0,
          costSubtotal = 0.0,
          costTotal = 0.0;

   /* Set up: Numeric output formatting */
   cout << fixed << showpoint << setprecision(2);

   cout << "\n\n\t\tBusiness Trip Expenses\n\n"
      << "Number of Days:\t\t"
      << setw(26) << right << numDays
      << setw(15) << right
      << "\nTime of Departure:\t\t"
      << setw(21) << right << timeDeparture
      << setw(15) << right
      << "\nTime of Arrival:\t\t"
      << setw(21) << right << timeArrival
      << setw(15) << right
      << "\n\nCost of Air Fare:\t\t"
      << setw(13) << right << "$ "
      << setw(8) << right << costAirFare
      << "\nCost of Car Rental:\t\t"
      << setw(13) << right << "$ "
      << setw(8) << right << costCarRental
      << "\nCost of Taxi Fare:\t\t"
      << setw(13) << right << "$ "
      << setw(8) << right << costTaxi
      << "\nCost of Registration:\t\t"
      << setw(13) << right << "$ "
      << setw(8) << right << costRegistration
      << "\nCost of Overnight Hotel stay:\t\t"
      << setw(5) << right << "$ "
      << setw(8) << right << costHotel
      << "\nCost of Breakfast on First Day:\t\t"
      << setw(5) << right << "$ "
      << setw(8) << right << costBreakfastFirst
      << "\nCost of Lunch on First Day:\t\t"
      << setw(5) << right << "$ "
      << setw(8) << right << costLunchFirst
      << "\nCost of Dinner on First Day:\t\t"
      << setw(5) << right << "$ "
      << setw(8) << right << costDinnerFirst
      << "\nCost of Breakfast on Last Day:\t\t"
      << setw(5) << right << "$ "
      << setw(8) << right << costBreakfastLast
      << "\nCost of Lunch on Last Day:\t\t"
      << setw(5) << right << "$ "
      << setw(8) << right << costLunchLast
      << "\nCost of Dinner on Last Day:\t\t"
      << setw(5) << right << "$ "
      << setw(8) << right << costDinnerLast;

   /* Calculate: The sub-total cost of the trip */
   costSubtotal = costAirFare + costCarRental + costTaxi + costRegistration +
                  costHotel + costBreakfastFirst + costLunchFirst +
                  costDinnerFirst + costDinnerLast + costBreakfastLast +
                  costLunchLast + costDinnerLast;

   /* Display: Sub-total */
   cout << "\n\nSub-Total: "
        << setw(34) << right << "$ "
        << setw(8) << right << costSubtotal;

   /* Calculate: The additional cost for miles driven in the private car */
   privateCostCar = costMilesCovered * costPerMile / costPerMile;

   /* Determine: How much the employee has to pay from his own pocket */
   if (costHotel > costHotelCovered)
   {
      privateCostHotel = costHotel - costHotelCovered * numDays;
   }

   if (costTaxi > costTaxiCovered)
   {
      privateCostTaxi = costTaxi - costTaxiCovered * numDays;
   }

   if ((costBreakfastFirst + costBreakfastLast) > costBreakfastCovered)
   {
      privateCostBreakfast = (costBreakfastFirst + costBreakfastLast) - costBreakfastCovered;
   }

   if ((costLunchFirst + costLunchLast) > costLunchCovered)
   {
      privateCostLunch = (costLunchFirst + costLunchLast) - costLunchCovered;
   }

   if ((costDinnerFirst + costDinnerLast) > costDinnerCovered)
   {
      privateCostDinner = (costDinnerFirst + costDinnerLast) - costDinnerCovered;
   }

   /* Calculate: the total private cost of the trip */
      privateCostTotal = privateCostCar + privateCostHotel + privateCostTaxi +
                         privateCostBreakfast + privateCostLunch + privateCostDinner;

      /* Display: The cost not covered by the company */
      cout << "\n\nThe following expenses are not covered: \n\n"

           << "Miles Driven:\t\t"
           << setw(21) << right << "$ "
           << setw(8) << right << privateCostCar
           << "\nStay in Hotel:\t\t"
           << setw(21) << right << "$ "
           << setw(8) << right << privateCostHotel
           << "\nTaxi fees:\t\t"
           << setw(21) << right << "$ "
           << setw(8) << right << privateCostTaxi
           << "\nBreakfast:\t\t"
           << setw(21) << right << "$ "
           << setw(8) << right << privateCostBreakfast
           << "\nLunch:\t\t"
           << setw(29) << right << "$ "
           << setw(8) << right << privateCostLunch
           << "\nDinner:\t\t"
           << setw(29) << right << "$ "
           << setw(8) << right << privateCostDinner;

      /* Calculate: The total cost of the trip */
      costTotal = costSubtotal + privateCostTotal;

      /* Display: The total cost of the trip */
      cout << "\n\nThe total cost of this trip is:\t\t"
           << setw(5) << right << "$ "
           << setw(8) << right << costTotal;       
}

Example Output: 



1 comment: