#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#include <cmath>
#include <fstream>
#include <stdlib.h>
using namespace std;
class MortgageCal
{
private:
double intYear;
double intMonth;
double loanAmt;
double term;
double numMonths;
double paymentAmt;
double balance;
double principleAmt;
double loanBal;
public:
MortgageCal(
double intYear = 0,
double term = 0,
double loanAmt = 0,
double paymentAmt = 0,
double numMonths = 0,
double intMonth = 0,
double balance = 0,
double principleAmt = 0,
double loanBal = 0):
intYear(intYear),
term(term),
loanAmt(loanAmt),
paymentAmt(paymentAmt),
numMonths(),
intMonth(),
balance(),
principleAmt(),
loanBal(){ }
double getnumMonths()
{
return term * 12;
}
bool setnumMonths(double months)
{
if (months > 0)
{
term = months/12;
}
else
{
return false;
}
return true;
}
double getTermInYears() { return term; }
bool setTermInYears(double years)
{
if (years > 0)
{
term = years;
}
else
{
return false;
}
return true;
}
double getPrinciple()
{
return principleAmt;
}
bool setPrinciple(double principle)
{
if (principle >= 0)
{
principleAmt = principle;
}
else
{
return false;
}
return true;
}
double getLoanAmount()
{
return loanAmt;
}
bool setLoanAmount(double loan)
{
if (loan >= 0)
{
loanAmt = loan;
}
else
{
return false;
}
return true;
}
double getYearlyRate()
{
return intYear;
}
bool setYearlyRate(double rate)
{
intYear = rate;
return true;
}
double getPaymentAmount()
{
return paymentAmt;
}
bool setPaymentAmount (double payment)
{
return true;
}
double getLoanBalance()
{
return loanBal;
}
bool setLoanBalance(double loanB)
{
if (loanB >= 0)
{
loanBal = loanBal-principleAmt;
}
else
{
return false;
}
return true;
}
double getInterestMonth()
{
return intMonth = (intYear/1200);
}
bool setInterestMonth(double interest)
{
return true;
}
void Amortization();
void Schedule();
void caseSchedule();
void enterLoanAmts();
};
double getRangedInput(const string prompt, double min, double max, int tries = 5);
void pause();
int menu = 0;
double getRangedInput(const string prompt, double min, double max, int tries)
{
int userTry = 0;
double userInVal = 0;
bool isValidInput;
do
{
cout << prompt << ": ";
cin >> userInVal;
if (!cin.good())
{
cout <<endl;
cout << "INPUT ERROR: Please input a proper number!" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
isValidInput = false;
}
else
{
isValidInput = (userInVal >= min && userInVal <= max);
}
if (!isValidInput)
{
cout << "Please enter a value between " << min << " and " << max << "!" << endl;
cout << endl;
}
} while (!isValidInput && ++userTry < tries);
if (userTry == tries)
{
cout << "User input invalid, can not process!" << endl;
exit(1);
}
return userInVal;
}
void MortgageCal::Schedule()
{
int intPymtNum = 1;
int max = 0;
double intRate;
double intPayment;
double principleAmt;
double loanBal;
cout << endl << "This is a breakdown of how much of your payment is applied to interest and the balance of your loan: " << endl;
system ( "PAUSE" );
cout << endl << endl;
cout << " Payment" << setw(15) << "Interest" << endl;
cout << " Number " << setw(15) << " Paid " << setw(14) << "balance " << endl;
cout << " _______" << setw(15) << "________" << setw(14) << "________" << endl;
loanBal = loanAmt; l
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
for(int i = 1, max = 1; intPymtNum <= numMonths; i++)
{
intRate = intMonth;
intPayment = loanBal * intRate;
principleAmt = paymentAmt - (loanBal * intRate);
loanBal = loanBal - principleAmt;
if (loanBal <= 0)
{
loanBal = 0;
}
cout << endl << setw(5) << intPymtNum << setw(17) << intPayment << setw(15) << loanBal;
intPymtNum++;
max++;
if (max == 20)
{
cout << endl;
system ( "PAUSE" );
max = 1;
cout << endl << endl;
cout << " Payment" << setw(15) << "Interest" << endl;
cout << " Number " << setw(15) << " Paid " << setw(14) << "balance " << endl;
cout << " _______" << setw(15) << "________" << setw(14) << "________" << endl;
}
}
}
void pause()
{
cout << "Press enter to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.ignore();
}
void MortgageCal::Amortization()
{
numMonths = term * 12;
intMonth = intYear/1200;
paymentAmt = (loanAmt*intMonth)/(1-pow(1+intMonth,-numMonths));
}
void MortgageCal::caseSchedule()
{
int checklist = 0;
int choice=0;
int ary;
int iarray[3] = {0};
float farray[3] = {0.0F};
ifstream in("calculations.txt");
in >> iarray[0] >> iarray[1] >> iarray[2];
in >> farray[0] >> farray[1] >> farray[2];
do
{
cout<<"\n------Loan Menu Items------";
cout<< "\n1) 7 years at 5.25% \n2) 15 years at 5.5% \n3) 30 years at 5.75%\n-------------------------\nEnter (1-3)";
cin >> choice;
checklist = 0;
if(choice <= 0)
{
cout<<"\n\n\n";
cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
checklist = 1;
}
else if(choice == 1)
{
ary=0;
}
else if(choice == 2)
{
ary=1;
}
else if (choice ==3)
{
ary=2;
}
else if (choice > 3)
{
cout<<"\n\n\n";
cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
checklist = 1;
}
cout<<"Please enter the Loan Amount:$";
cin >> loanAmt;
} while (checklist == 1);
numMonths = (iarray[ary] * 12);
intMonth = (farray[ary]/1200);
paymentAmt = (loanAmt*intMonth)/(1-pow(1+intMonth,-numMonths));
cout << "After Amortinzation:\n" << endl << "Your payments will be: $" << paymentAmt;
MortgageCal::Schedule();
}
void MortgageCal::enterLoanAmts()
{
double loanAmount;
double yearlyRate;
MortgageCal morgCalc;
char indicator = ('y', 'Y')
while ((indicator =='y') || (indicator == 'Y'))
{
loanAmount = getRangedInput("Please enter the loan amount", 0, numeric_limits<double>::max());
term = getRangedInput("\nPlease enter the loan term in years", 0, 10000);
yearlyRate = getRangedInput("\nPlease enter the yearly interest rate", 0, 100);
cout << endl;
morgCalc.setLoanAmount(loanAmount);
morgCalc.setTermInYears(term);
morgCalc.setYearlyRate(yearlyRate);
morgCalc.Amortization();
cout << "After Amortinzation:\n" << endl;
cout << "Your payments will be: $" << morgCalc.getPaymentAmount();
morgCalc.Schedule();
cout << endl;
pause();
cout << endl;
cout << "Do you want to enter another value (enter y to continue or n to end)? ";
cin >> indicator;
}
cout << endl;
cout << "\"Thank you for using our mortgage calculator\"" << endl;
cout << endl;
pause();
}
int main()
{
double loanAmount;
double term;
double numMonths;
double intMonth;
double paymentAmt;
char indicator;
MortgageCal morgCalc;
do
{
system("cls");
cout << endl << "\"Welcome to our Mortgage Calculator!\"";
cout << "\n\n-------- Main Menu ---------";
cout << "\n 1. Would you like to enter rate and term ";
cout << "\n 2. Would you like to select loan from menu ";
cout << "\n----------------------------\n";
cout << "Press 1 or 2: ";
cin >>menu;
if (menu == 1)
{
morgCalc.enterLoanAmts();
}
else if (menu == 2)
{
morgCalc.caseSchedule();
}
cout<< "\nEnter another loan (y)es or (n)o):";
cin>>indicator;
} while(indicator == 'y');
return 0;
}