Thursday, December 22, 2016

Programming Challenge 5.16 - Savings Account Balance

/* Savings Account Balance - This program calculates the balance of a savings
   account at the end of a period of time. It asks the user for the annual
   interest rate, the starting balance, and the number of months that have
   passed since the account was established. A loop iterates once for every
   month, performing the following:

   * The user is asked for the amount deposited into the account during the
      month. This amount is added to the balance.

      Input Validation: No negative numbers are accepted.

   * The user is asked for the amount withdrawn from the account during the
     month. This amount is subtracted from the balance.

     Input Validation: No negative numbers are accepted.

   * Calculating the monthly interest:

     The monthly interest rate is the annual interest rate divided by twelve.
     The monhtly interest rate is multiplied by the balance, and the result is
     added to the balance.

   After the last iteration, the program displays the ending balance, the total
   amount of deposits, the total amount of withdrawals, and the total interest
   earned.

   Notice: If, at any point, a negative balance is calculated, a message will
   be displayed, indicating that the account has been closed and the loop will
   terminate. */

#include "Utility.h"

int main()
{
    /* Constant: Maximum annual interest rate */
    const double ANNUAL_INTEREST_MAX = 15;

    /* Variable: Months (counter variable), Months passed */
    int months = 1,
        monthsPassed = 0;

    /* Variables: Starting Balance, Annual interest rate, Monthly interest
       rate, Account Balance (accumulator variable), Account balance total,
       Monthly deposits, Monthly deposits total, Monthly withdrawals total
       (accumulator variable), Monthly balance total,
       (accumulator variable), Monthly interest earned, Deposits total,
       Withdrawals total, Interest earned total */
    double startingBalance = 0,
           annualInterestRate = 0,
           accountBalance = 0,
           accountBalanceTotal = 0,
           monthlyInterest = 0,
           interestRate = 0,
           monthlyInterestRate = 0,
           monthlyDeposits = 0,
           monthlyDepositsTotal = 0,
           monthlyWithdrawals = 0,
           monthlyWithdrawalsTotal = 0,
           monthlyInterestEarned = 0,
           monthlyBalanceTotal = 0,
           depositsTotal = 0,
           withdrawalsTotal = 0,
           interestEarnedTotal = 0;  

    /* Display: Information */
    cout << "\t\tAshikaga Bank: Personal Account Balance Manager\n\n";

    /* Input::Ask the user for: The starting balance of the users bank
    account, the annual interest rate, the number of months have passed
    since the account was established */
    cout << "What was the starting balance of your account?\n"
        << "Starting Balance: $ ";
    cin >> accountBalance;

    /* While loop::Input Validation: As long as the starting balance entered
    by the user is below or equal to 0, this loop will iterate */
    while (accountBalance <= 0)
    {
        /* Display: Error message */
        cout << "Error: The Starting Balance has to be a positive value.\n";

        /* Ask again: */
        cout << "Starting Balance: $ ";
        cin >> accountBalance;
    }

    cout << "What is the annual interest rate?\n"
        << "Annual Interest Rate: % ";
    cin >> annualInterestRate;

    /* While loop::Input Validation: While the annual interest rate entered
    by the user is below or equal to 0, or above ANNUAL_INTEREST_MAX (4.25),
    this loop will iterate */
    while (annualInterestRate <= 0 || annualInterestRate > ANNUAL_INTEREST_MAX)
    {
        /* Display: Error message */
        cout << "Error: The Annual Interest Rate has to be a positive value\n"
            << "above 0 and below or equal to %15.00.\n";

        /* Ask again: */
        cout << "Starting Balance: $ ";
        cin >> annualInterestRate;
    }

    cout << "How much time has passed since your account was established?\n"
         << "Months Passed: ";
    cin >> monthsPassed;

    /* While loop::Input Validation: While months passed entered by the user
       is below or equal to 0, this loop will iterate */
    while (monthsPassed <= 0)
    {
        /* Display: Error message */
        cout << "Error: The amount of months passed can not be negative!\n";

        /* Ask again: */
        cout << "Months Passed: ";
        cin >> monthsPassed;
    }

    /* For loop: As long as months is lower than or equal to months passed,
       this loop will iterate */
    for (months; months <= monthsPassed; months++)
    {
        /* Nested While loop: While months is smaller than or equal to
           months passed, and the account balance is not negative,
           this loop will iterate */
        while (months <= monthsPassed && (!(accountBalance < 0)))
        {
            /* Input::Ask the user for: His or her monthly deposits, his or
            her monthly withdrawals */
            cout << "\nHow much money have you deposited in month " << months
                << " in total?\n"
                << "Monthly Deposit: $ ";
            cin >> monthlyDeposits;

            /* Nested While loop: While the customer enters a negative
               number for his or her monthly deposits, this loop will
               iterate */
            while (monthlyDeposits < 0)
            {
                /* Display: Error message */
                cout << "\nError: Monthly deposits have to be positive,\n"
                     << "or above, or equal to 0, if no deposits were\n"
                     << "made in the month you wish to enter data for.\n";

                /* Ask again: */
                cout << "\nHow much money have you deposited in month " << months
                    << " in total?\n"
                    << "Monthly Deposit: $ ";
                cin >> monthlyDeposits;
            }

            cout << "\nHow much money have you withdrawn in month " << months
                << " in total?\n"
                << "Monthly Withdrawals: $ ";
            cin >> monthlyWithdrawals;
   
            /* Nested While loop: While the customer enters a negative
            number for his or her monthly withdrawals, this loop will
            iterate */
            while (monthlyWithdrawals < 0)
            {
                /* Display: Error message */
                cout << "\nError: Monthly withdrawals have to be positive,\n"
                     << "or above, or equal to 0, if no withdrawals have\n"
                     << "occured in the month you wish to enter data for.\n";

                /* Ask again: */
                cout << "\nHow much money have you withdrawn in month "
                     << months << " in total?\n"
                     << "Monthly Withdrawals: $ ";
                cin >> monthlyWithdrawals;
            }

            /* Accumulating: Monthly deposits (add to the account balance),
               Monthly withdrawals (deduct from the account balance) */
            accountBalance += monthlyDeposits;
            accountBalance -= monthlyWithdrawals;

            /* Calculations: Monthly interest rate:
               To get the fraction of the total interest rate, 4.25 = 0.0425,
               the annual interest rate is divided by 100 and by the amount
               of months in a year */
               monthlyInterestRate = (annualInterestRate / 12.0) / 100.0;

            /* Interest rate formula: Monthly interest rate = account balance *
               monthly interest rate * (months passed / 12.0) / 100.0 */       
            interestRate = accountBalance * monthlyInterestRate *
                          (monthsPassed / 12.0);
            monthlyInterest = accountBalance * monthlyInterestRate *
                          (monthsPassed / 12.0);

            /* Accumulating: Total monthly deposits, Total monthly withdrawals,
               Total monthly interest earned, Total monthly balance */
            monthlyDepositsTotal += monthlyDeposits;
            monthlyWithdrawalsTotal -= monthlyWithdrawals;
            monthlyInterestEarned += monthlyInterest;
            monthlyBalanceTotal = accountBalance + monthlyInterestEarned;

            /* Accumulating: Total Deposits, Total withdrawals, Total
               Interest Earned, Total account balance */
            depositsTotal += monthlyDeposits;
            withdrawalsTotal -= monthlyWithdrawals;
            interestEarnedTotal += interestRate;
            accountBalanceTotal = accountBalance + interestEarnedTotal;

            /* If Statement: If the account balance is negative, or the
            total monthly balance is negative, or the monthly withdrawals
            are greater than the monthly deposits, and the monthly withdrawals
            are greater than the monthly account balance the loop will
            terminate */
            if (accountBalance < 0 || monthlyBalanceTotal < 0 ||
                monthlyWithdrawals > monthlyDeposits &&
                monthlyWithdrawals > monthlyBalanceTotal)
            {
                cout << "\nValued customer,\n\n"
                     << "We are utmost sorry to have to inform you about the sad\n"
                     << "event, that your bank account was for some unknown reason\n"
                     << "overdrawn, and had to be terminated.\n"
                     << "If you wish to aquire further information about this event,\n"
                     << "or you feel that this was a failure on our side or a third\n"
                     << "party we have no control over, please do not hesitate to call\n"
                     << "us. Our hotlines are open for you 24/7.\n"
                     << "It would also be a pleasure for us, should you give us the\n"
                     << "opportunity to clarify this sad situation with you personally,\n"
                     << "to visit us during our regular service hours. We will see to it,\n"
                     << "that this problem will be resolved to your utmost satisfaction.\n"
                     << "Thank you for your understanding,\n\n"
                     << "Your Ashikaga Bank Team\n";

                pauseSystem();
                return 0;
            }

                /* Display: Formatted monthly output of the account balance sheet */
                cout << fixed << showpoint << setprecision(2);
                cout << "\n\t\tAshikaga Bank - Monthly Account Balance Sheet\n";
                cout << "--------------------------------------------------------------------\n";
                cout << "Month: " << months << "\n";
                cout << "\nMonthly Deposits Total:\t\t\t\t\t $ " << setw(9) << right
                    << monthlyDepositsTotal << endl;
                cout << "Monthly Withdrawals Total:\t\t\t\t $ " << setw(9) << right
                    << monthlyWithdrawalsTotal << endl;
                cout << "Monthly Interest Earned:\t\t\t\t $ " << setw(9) << right
                    << monthlyInterestEarned << endl;
                cout << "--------------------------------------------------------------------\n";
                cout << "Monthly Balance Total:\t\t\t\t\t $ " << setw(9) << right
                    << monthlyBalanceTotal << endl;
                cout << "--------------------------------------------------------------------\n";

            months++;
        }
   
        /* Display: Formatted  final output of the account balance sheet */
        cout << fixed << showpoint << setprecision(2);
        cout << "\n\n\t\tAshikaga Bank - Final Account Balance Sheet\n";
        cout << "--------------------------------------------------------------------\n";
        cout << "Account History: " << monthsPassed << " months\n";
        cout << "\nDeposits Total:\t\t\t\t\t $ " << setw(9) << right
            << depositsTotal << endl;
        cout << "Withdrawals Total:\t\t\t\t $ " << setw(9) << right
            << withdrawalsTotal << endl;
        cout << "Interest Earned Total:\t\t\t\t $ " << setw(9) << right
            << interestEarnedTotal << endl;
        cout << "--------------------------------------------------------------------\n";
        cout << "Account Balance Total:\t\t\t\t\t $ " << setw(9) << right
            << accountBalanceTotal << endl;
        cout << "--------------------------------------------------------------------\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment