Monday, December 26, 2016

Programming Challenge 5.19 - Budget Analysis

/* Budget Analysis - This program asks the user to enter the amount that
   he or she has budgeted for a month. A loop then prompts the user to
   enter each of his or her expenses for the month and keep a running
   total. When the loop finishes, the program displays the amount that
   the user is over or under budget. */

#include "Utility.h"

int main()
{
    /* Variables: Monthly budget, Expenses, Total expenses (accumulator),
       Over budget */
    double monthlyBudget = 0.0,
           expenses = 0.0,
           totalExpenses = 0.0,
           overBudget = 0.0;

    /* Variable: Running expenses (counter variable) */
    int runningExpenses = 1;

    /* Display: Information */
    cout << "\t\tPersonal Budget Butler\n\n"
        << "This program helps you in the task of keeping track of\n"
        << "your monthly budget and expenses. Please enter your\n"
        << "alloted budget for the month, and proceed to enter all\n"
        << "of your expenses. Once you finished entering data,\n"
        << "enter '-1'. The program will then tell you, whether or\n"
        << "not you are still within your monthly budget limit.\n";

    /* Ask the user for his or her budgeted amount of money for the
       current month */
    cout << "\nPlease enter your budget limit for this month\n";
    cout << "Monthly budget: $";
    cin >> monthlyBudget;

    /* While loop::Input validation: While the user enters a negative
       number for his or her monthly budget, this loop will iterate */
    while (monthlyBudget < 1)
    {
        /* Display: Error message */
        cout << "\nYour monthly budget can not be 0 or negative. Please\n"
             << "enter an amount greater than at least $1 and above.\n";

        /* Ask again */
        cout << "\nPlease enter the amount of money you budgeted for this"
             << " month.\n";
        cout << "Monthly budget: $";
        cin >> monthlyBudget;
    }

    /* Ask the user for his or her current expenses */
    cout << "\nEnter your monhtly expenses, then enter -1 to finish.\n";
    cout << "Expenses: $";
    cin >> expenses;

    /* While loop::Input Validation: If the user enters a value equal to
       0 or lower than -1, this loop will iterate */
    while (expenses < -1 || expenses == 0)
    {
        /* Display: Error message */
        cout << "\nYou seem to have entered a value of 0, or lower\n"
             << "than -1. Please remember, that the only\n"
             << "value below 0 that is allowed is '-1' to quit.\n";
           
        /* Ask again */
        cout << "Expenses: $";
        cin >> expenses;
     }

    /* For loop: As long as the user enters his or her budget, and doesn't
       enter -1 to quit entering data, this loop will iterate */
    while (expenses != -1)
    {
        /* Accumulating: Total monthly expenses */
        totalExpenses += expenses;
        runningExpenses++;

        /* Keep asking the user for his or her current expenses, until
           he or she enters -1 to exit the loop */
        cout << "Expenses: $";
        cin >> expenses;

        /* Nested While loop::Input Validation: If the user enters a value
           equal to 0 or lower than -1, this loop will iterate */
        while (expenses < -1 || expenses == 0)
        {
            /* Display: Error message */
            cout << "\nYou seem to have entered a value of 0, or lower\n"
                 << "than -1. Please remember, that the only\n"
                 << "value below 0 that is allowed is '-1' to quit.\n";

            /* Ask again */
            cout << "Expenses: $";
            cin >> expenses;
        }
    }

    /* Calculating: The amount exceeding the monthly budget limit */
    overBudget = totalExpenses - monthlyBudget;

    /* Formating the ouput */
    cout << fixed << showpoint << setprecision(2);

    /* Display: Table header */
    cout << "\nPersonal Budget Butler Final Report\n";
    cout << "-------------------------------------------------\n";

    /* If Statement: If the budget limit reached after all
    expenses have been entered? If so, the monhtly budget limit,
    the total expenses, and an information is displayed, warning
    the user that he has reached his budget limit */
    if (totalExpenses == monthlyBudget)
    {
        cout << "\nThis month you have budgeted:\t$ "
            << monthlyBudget << setw(6) << right
            << "\n-------------------------------------------------\n"
            << "\nYou spent:\t\t\t$ " << setw(6) << right << totalExpenses
            << "\n-------------------------------------------------\n"
            << "Warning! You hit your monthly budget limit!\n";
    }
    else
    {
        /* Conditional Statement: Are the total expenses greater than the
           monthly budget limit? If so, the monthly budget limit and total
           expenses are displayed, along with a message informing the user
           about the amount exceeding the budget limit. Else the monthly
           budget limit is displayed, and the user is told that he or she
           is still within budget for the current month. */
        totalExpenses > monthlyBudget ?
            cout << "\nThis month you have budgeted:\t\t$ "
            << setw(7) << right << monthlyBudget
            << "\nYou spent:\t\t\t\t$ "
            << setw(7) << right << totalExpenses
            << "\n-------------------------------------------------\n"
            << "This month you exceeded your budget by:\t$ "
            << setw(7) << right << overBudget << endl :
        cout << "\nThis month you have budgeted:\t\t$ "
            << setw(6) << right << monthlyBudget
            << "\nYou spent:\t\t\t\t$ "
            << setw(7) << right << totalExpenses
            << "\n-------------------------------------------------\n"
            << "Great! You are still within your budget limit.\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment