Sunday, September 24, 2017

Programming Challenge 13.15 - Mortgage Payment

Example Files: MortgagePayment.h
                          MortgagePayment.cpp
                          MortgagePaymentDm.cpp

MortgagePayment.h


#ifndef MORTGAGE_PAYMENT_H
#define MORTGAGE_PAYMENT_H

class Mortgage
{
    private:
        double loanAmnt;                    // The loan amount
        double monthlyPayment;            // The monthly payment
        double interestRate;                // The annual interest rate
        int    terms;                        // Number of years

    public:
    Mortgage() : loanAmnt(0.0), monthlyPayment(0.0), interestRate(0.0), terms(0)
    { }

        bool setAmount(double);           
        bool setRate(double);
        bool setTerm(int);
        void calcPayment();

        double getAmount() const
        { return loanAmnt; }

        double getRate() const
        { return interestRate; }

        int getTerms() const
        { return terms; }

        double getPayment() const
        { return monthlyPayment; }
};
#endif

MortgagePayment.cpp


/* MortgagePaymentDm.cpp - Implementation file for the Mortgage Payment class. */

#include <math.h>
#include "MorgagePayment.h"

/* **********************************************************
            Mortgage::setAmount() accepts a double as argument
    This function sets the total loan amount, and assigns this
    value to the loanAmnt member.
   ********************************************************** */

bool Mortgage::setAmount(double amnt)
{
    if (amnt > 0)
    {
        loanAmnt = amnt;
        return true;
    }

    return false;
}

/* **********************************************************
            Mortgage::setRate() accepts a double as argument
    This function sets the interest rate, and assigns the
    value passed to it to the interestRate member.
   ********************************************************** */

bool Mortgage::setRate(double iRate)
{
    if (iRate > 0.0 && iRate <= 50.0)
    {
        interestRate = iRate;
        return true;
    }

    return false;
}

/* **********************************************************
            Mortgage::setTerm() accepts an int as argument
    This function sets the term in years, and assigns the
    value passed to it to the terms member.
   ********************************************************** */

bool Mortgage::setTerm(int t)
{
    if (t > 0 && t <= 40)
    {
        terms = t;
        return true;
    }

    return false;
}

/* **********************************************************
            Mortgage::calcPayment()
    This function calculates the monthly payments and assigns
    the result to the monthlyPayment member.
   ********************************************************** */

void Mortgage::calcPayment()
{
    int    mTerms = (getTerms() * 12);
    double iRate = (getRate() / 100) / 12;

    monthlyPayment = getAmount() * (pow(iRate + 1, mTerms)) *
                      iRate / (pow(iRate + 1, mTerms) - 1);
}

MortgagePaymentDm.cpp


/* MortgagePaymentDm.cpp - Demonstration file for the Mortgage Payment class. */

#include <iomanip>
#include <iostream>
#include <string>
#include "MorgagePayment.h"

using std::cout;
using std::cin;

void introduction();
void getData(Mortgage &);
void displayData(const Mortgage);

int main()
{
    Mortgage monthlyPayment;

    introduction();

    getData(monthlyPayment);

    monthlyPayment.calcPayment();
    displayData(monthlyPayment);

    cout << "\n\n\tThe ASHIKAGA BANK Team wishes you a successful day\n"
          << "\tand thanks you for your patronage!";

    std::cin.get();
    std::cin.ignore();
    return 0;
}

/* **********************************************************
    introduction()
    This function introduces the basic features to the user.
   ********************************************************** */

void introduction()
{
    cout << "\t\tASHIKAGA BANK - MORTGAGE PAYMENT CALCULATOR\n\n";
    cout << "\tDear Customer, this application allows you to calculate the monthly\n"
          << "\tamount of money you will have to pay on your home mortgage. You are\n"
          << "\tasked to enter the Loan Amount (Ex: 500,000), the Interest Rate in\n"
          << "\t(Ex: 5.50), and the term in years (Ex: 30).\n\n"
          << "\tThe ASHIKAGA BANK hopes that this new service will benefit our valued\n"
          << "\tcustomers. We thank you for your continued patronage and interest.\n\n"
          << "\tYour ASHIKAGA BANK Team.\n\n";
}

/* **********************************************************
    getData() accepts a reference to a monthlyPayment object.
    This function asks the user to input the number of terms
    in years, the loan amount and the annual interest rate.
   ********************************************************** */

void getData(Mortgage &monthlyPayment)
{
    int    terms = 0;
    double amount = 0.0;
    double iRate = 0.0;

    cout << "\t\tASHIKAGA BANK - MORTGAGE PAYMENT - CUSTOMER FINANCIAL DATA\n\n";
    cout << "\tDear Customer,\n\n"
          << "\tPlease be utmost careful when entering the information asked of you\n"
          << "\tin the following three step process, else we cannot guarantee for the\n"
          << "\tcorrectness of the results. We must also warn you that we are not legally\n"
          << "\tliable for any financial damage that may occur.\n\n";

    cout << "\tStep 1:\n"
         << "\tLoan amount: $ ";
    cin >> amount;

    while (monthlyPayment.setAmount(amount) == false)
    {
        cout << "\n\tStep 1:\n"
             << "\tLoan amount: $ ";
        cin >> amount;
    }

    cout << "\n\tStep 2:\n"
        << "\tAnnual interest rate: % ";
    cin >> iRate;

    while (monthlyPayment.setRate(iRate) == false)
    {
        cout << "\n\tStep 2:\n"
              << "\tAnnual interest rate: % ";
        cin >> iRate;
    }

    cout << "\n\tStep 3:\n"
          << "\tNumber of terms in years: ";
    cin >> terms;

    while (monthlyPayment.setTerm(terms) == false)
    {
        cout << "\n\tStep 3:\n"
              << "\tNumber of terms in years: ";
        cin >> terms;
    }

    cout << "\n\tThank you! Your data will now be processed ...\n\n";
}

/* **********************************************************
   displayData() accepts a const monthlyPayment object
    This function outputs a detailed report, telling the user
    how high his or her monthly mortgage burden will be.
   ********************************************************** */

void displayData(const Mortgage monthlyPayment)
{
    cout << std::setprecision(2) << std::fixed << std::showpoint;
    cout << "\t\tASHIKAGA BANK - MONTHLY MORTGAGE PAYMENT PLAN\n\n";
    cout << "\tDear Customer,\n\n"
          << "\tAccording to the following data you kindly provided:\n\n"
          << "\tLoan Amount" << std::setw(25) << "$ " << monthlyPayment.getAmount() << "\n"
          << "\tYearly Interest Rate"
          << std::setw(15) << "%" << std::setw(10) << monthlyPayment.getRate() << "\n"
          << "\tLoan Term: " << std::setw(28) << monthlyPayment.getTerms() << " Years\n\n"
          << "\tOur calculation indicates that your monthly payment burden will be $ "
          << monthlyPayment.getPayment();
}

Example Output: 




No comments:

Post a Comment