Thursday, June 8, 2017

Programming Challenge 11.11 - Monthly Budget

/* Monthly Budget - A student has established the following monthly budget:

        * Housing:                      500.00            Utilities:                  150.00
        * Household Expenses:       65.00            Transportation:           50.00
        * Food:                          250.00           Medical:                        30.00
        * Insurance:                  100.00       Entertainment:              150.00
        * Clothing:                       75.00            Miscellaneous:                50.00

    This program has a MonthlyBudget structure designed to hold each of these
    expense categories. The program passes the structure to a function that
    asks the user to enter the amounts spent in each budget category during a
    month. The program then passes the structure to a function that displays a
    report indicating the amount over or under in each category, as well as the
    amount over or under for the entire monthly budget. */

#include "Utility.h"

struct MonthlyBudget
{
    double housing;            /* Amount spent for housing                 */
    double utilities;            /* Amount spent for utilities                 */
    double householdExp;        /* Amount spent for household expenses  */
    double transport;            /* Amount spent for transportation         */
    double food;                /* Amount spent for food                     */
    double medical;            /* Amount spent for medical treatment   */
    double insurance;            /* Amount spent for insurance                 */
    double entertainment;    /* Amount spent for entertainment         */   
    double clothing;            /* Amount spent for clothing                 */
    double miscellaneous;    /* Amount spent for miscellaneous items */
    double total;                /* Total amount spent                         */           
};

void getBudget(MonthlyBudget &);
void calcTotal(MonthlyBudget &);
void calcBudget(const double, const double);
void displayBudget(const MonthlyBudget, const MonthlyBudget);

int main()
{
    MonthlyBudget spentBudget;
    MonthlyBudget fixedBudget = { 500.0, 150.0,  65.0, 50.0, 250.0,
                                            30.0, 100.0, 150.0, 75.0,  50.0, 1420.0 };

    getBudget(spentBudget);
    calcTotal(spentBudget);
    displayBudget(spentBudget, fixedBudget);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getBudget

    This function asks the user to enter the sum spent for the
    budget items. The input is stored in the appropriate
    structure members.
   ********************************************************** */

void getBudget(MonthlyBudget &spentBudget)
{
    cout << "\n\tMONTHLY BUDGET - EXPENSES\n\n"
          << setw(25) << left << "\tHousing:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.housing;

    while (spentBudget.housing <= 0.0)
    {
        cout << setw(25) << left << "\tHousing:"
             << setw(3) << right << "$ ";
        cin >> spentBudget.housing;
    }
   
    cout << setw(25) << left << "\tUtilities:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.utilities;

    while (spentBudget.utilities <= 0.0)
    {
        cout << setw(25) << left << "\tUtilities:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.utilities;
    }

    cout << setw(25) << left << "\tHousehold Expenses:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.householdExp;

    while (spentBudget.householdExp <= 0.0)
    {
        cout << setw(25) << left << "\tHousehold Expenses:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.householdExp;
    }

    cout << setw(25) << left << "\tTransportation:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.transport;

    while (spentBudget.transport <= 0.0)
    {
        cout << setw(25) << left << "\tTransportation:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.transport;
    }

    cout << setw(25) << left << "\tFood:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.food;

    while (spentBudget.food <= 0.0)
    {
        cout << setw(25) << left << "\tFood:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.food;
    }

    cout << setw(25) << left << "\tMedical:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.medical;
   
    while (spentBudget.medical <= 0.0)
    {
        cout << setw(25) << left << "\tMedical:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.medical;
    }

    cout << setw(25) << left << "\tInsurance:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.insurance;

    while (spentBudget.insurance <= 0.0)
    {
        cout << setw(25) << left << "\tInsurance:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.insurance;
    }

    cout << setw(25) << left << "\tEntertainment:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.entertainment;

    while (spentBudget.entertainment <= 0.0)
    {
        cout << setw(25) << left << "\tEntertainment: "
              << setw(3) << right << "$ ";
        cin >> spentBudget.entertainment;
    }

    cout << setw(25) << left << "\tClothing:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.clothing;

    while (spentBudget.clothing <= 0.0)
    {
        cout << setw(25) << left << "\tClothing:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.clothing;
    }

    cout << setw(25) << left << "\tMiscellaneous:"
          << setw(3) << right << "$ ";
    cin >> spentBudget.miscellaneous;

    while (spentBudget.clothing <= 0.0)
    {
        cout << setw(25) << left << "\tMiscellaneous:"
              << setw(3) << right << "$ ";
        cin >> spentBudget.clothing;
    }
}

/* **********************************************************
   Definition: calcTotal

    This function calculates the total amount spent during the
    month. The result is stored in the appropriate structure
    member.
   ********************************************************** */

void calcTotal(MonthlyBudget &spentBudget)
{
    spentBudget.total = spentBudget.housing      + spentBudget.utilities     +
                              spentBudget.householdExp + spentBudget.transport     +
                              spentBudget.food          + spentBudget.medical         +
                              spentBudget.insurance    + spentBudget.entertainment +
                              spentBudget.clothing        + spentBudget.miscellaneous;
}

/* **********************************************************
   Definition: calcBudget

    The ternary in this function determines whether the amount
    spent is greater, less, or equal to the budgeted amount.
    Based on the result, calculations are performed for each
    budget item. The result of the calculation, and whether
    the budget is over, under or equal to the budgeted amount
    is displayed.
   ********************************************************** */

void calcBudget(const double spentBudget, const double fixedBudget)
{
    const string overBudget         = "     over budget\n";
    const string underBudget    = "    under budget\n";
    const string balancedBudget = " balanced budget\n";

    cout << fixed << showpoint << setprecision(2);

    spentBudget > fixedBudget ? cout << (spentBudget - fixedBudget) << overBudget :
    spentBudget < fixedBudget ? cout << (fixedBudget - spentBudget) << underBudget :
                                         cout << (fixedBudget - spentBudget) << balancedBudget;
}

/* **********************************************************
   Definition: displayBudget

    This function displayes a detailed overview about each
    budget item.
   ********************************************************** */

void displayBudget(const MonthlyBudget spentBudget, const MonthlyBudget fixedBudget)
{
    cout << "\n\n\tMONTHLY BUDGET - EVALUATION\n\n\n";

    cout << "\tBUDGET ITEM"
          << setw(20) << right << "BUDGETED"
          << setw(20) << right << "SPENT"
          << setw(34) << right << "BUDGET STATUS";

    cout << "\n\t" << setw(97) << right << setfill('-') << "\n";

    cout << setfill(' ');
    cout << fixed << showpoint << setprecision(2);

    cout << setw(20) << left  << "\tHousing"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.housing
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.housing
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.housing, fixedBudget.housing);

    cout << setw(20) << left  << "\tUtilities"
          << setw(5)  << right << "$ " << setw(7) << right << fixedBudget.utilities
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.utilities
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.utilities, fixedBudget.utilities);

    cout << setw(20) << left  << "\tHousehold Expenses"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.householdExp
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.householdExp
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.householdExp, fixedBudget.householdExp);

    cout << setw(20) << left  << "\tTransport"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.transport
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.transport
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.transport, fixedBudget.transport);

    cout << setw(20) << left  << "\tFood"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.food
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.food
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.food, fixedBudget.food);

    cout << setw(20) << left  << "\tMedical"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.medical
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.medical
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.medical, fixedBudget.medical);

    cout << setw(20) << left  << "\tInsurance"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.insurance
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.insurance
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.insurance, fixedBudget.insurance);

    cout << setw(20) << left  << "\tEntertainment"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.entertainment
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.entertainment
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.entertainment, fixedBudget.entertainment);

    cout << setw(20) << left  << "\tClothing"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.clothing
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.clothing
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.clothing, fixedBudget.clothing);

    cout << setw(20) << left  << "\tMiscellaneous"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.miscellaneous
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.miscellaneous
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.miscellaneous, fixedBudget.miscellaneous);

    cout << "\t" << setw(97) << right << setfill('-') << "\n";
    cout << setfill(' ');

    cout << setw(20) << left  << "\tTotal"
          << setw(5) << right  << "$ " << setw(7) << right << fixedBudget.total
          << setw(16) << right << "$ " << setw(7) << right << spentBudget.total
          << setw(19) << right << "$ " << setw(7) << right;
    calcBudget(spentBudget.total, fixedBudget.total);
}

Example Output:




1 comment: