1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
//Library inclusions.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <math.h>
using namespace std;
//Prototype Functions
string getName();
string getType();
double getAmount();
double getYears();
double getInterest();
string getPeriod();
int getPayPerYear(string payPeriod);
double getPayment(double interestRate, double yearsFinanced, int payPerYear, double amountFinanced);
void displayData(string clientName, string loanType, double interestRate, double yearsFinanced, string payPeriod, double amountFinanced, double payment);
int main()
{
string clientName, loanType, payPeriod;
double amountFinanced;
double yearsFinanced;
double interestRate;
int payPerYear;
double payment;
cout << "LOAN PAYMENT \n \n";
cout << "Enter loan information as prompted below or Q to quit: ";
while(clientName != "Q" || clientName != "q")
{
clientName = getName();
if(clientName == "Q" || clientName =="q")
{
break;
}
loanType = getType();
amountFinanced = getAmount();
yearsFinanced = getYears();
interestRate = getInterest();
payPeriod = getPeriod();
payPerYear = getPayPerYear(payPeriod);
payment = getPayment(interestRate, yearsFinanced, payPerYear, amountFinanced);
displayData(clientName, loanType, interestRate, yearsFinanced, payPeriod, amountFinanced, payment);
}
}
string getName()
{
string clientName;
cout << endl << "Enter the name of Borrower: ";
getline(cin, clientName, '\n'); //Obtaining client's name.
return clientName;
}
string getType()
{
string loanType;
cout << "Type of Loan: ";
getline(cin, loanType, '\n'); //Obtaining what type of loan is being lent.
return loanType;
}
double getAmount()
{
double amountFinanced;
cout << "Amount Financed: ";
cin >> amountFinanced; //Obtaining the amount owed.
return amountFinanced;
}
double getYears()
{
double yearsFinanced;
cout << "Years financed (Enter as a decimal): ";
cin >> yearsFinanced; //Obtaining years financed.
return yearsFinanced;
}
double getInterest()
{
double interestRate;
cout << "Annual Interest Rate (Entered as a decimal): "; //Obtaining interest rate.
cin >> interestRate;
return interestRate;
}
string getPeriod()
{
string payPeriod;
cout << "Payment Period (Weekly, Monthly, etc): ";
getline(cin, payPeriod, '\n'); //Obtaining frequency of payment.
if (payPeriod == "Monthly")
{
payPeriod = "Monthly";
}
else if (payPeriod == "Quarterly")
{
payPeriod = "Quarterly";
}
else if (payPeriod == "Semiannually")
{
payPeriod = "Semiannually";
}
else if (payPeriod == "Annually")
{
payPeriod = "Annually";
}
else
{
cout << "Invalid selection. Please enter Monthly, Semiannually, Quarterly, or Annually.";
}
return payPeriod;
}
int getPayPerYear(string payPeriod) //Determines how many payments are made per year.
{
int payPerYear;
if(payPeriod == "Monthly")
{
payPerYear = 12;
}
else if(payPeriod =="Quarterly")
{
payPerYear = 4;
}
else if(payPeriod =="Semiannually")
{
payPerYear = 2;
}
else if(payPeriod == "Annually")
{
payPerYear = 1;
}
return payPerYear;
}
/***********************************getPayment****************************
Description: Calculates a loan payment amount.
interestRate: The annual interest rate of the loan(percentage).
yearsFinanced: The number of years financed.
payPerYear: the amount of pay periods per year.
loanAmount: the amount of the loan in US currency.
precondition:
interestRate is defined within the range 0% to 18%.
yearsFinanced is defined within the range of 1 to 30.
payPerYear is defined as 1, 2, 4, or 12.
loanAmount is defined within the range $500 to $1,000,000.
uses header file: cmath.
postcondition:
A payment amount has been calculated and returned.
*/
double getPayment(double interestRate, double yearsFinanced, int payPerYear, double amountFinanced)
{
double payment = (interestRate*(pow((1+interestRate), payPerYear))/(pow((1+interestRate), payPerYear))-1)*amountFinanced;
return payment;
}
void displayData(string clientName, string loanType, double interestRate, double yearsFinanced, string payPeriod, double amountFinanced, double payment)
{
cout << fixed << setprecision(2);
cout << endl << left << setw(26) << "Loan Payment Report For: " << right << setw(41) << clientName << endl;
cout << left << setw(26) << "Type of Loan: " << right << setw(37) << loanType << endl;
cout << left << setw(26) << "Amount Financed: " << setw(30) << "$" << right << setw(8) << amountFinanced << endl;
cout << left << setw(26) << "Years Financed: " << right << setw(38) << yearsFinanced << endl;
cout << left << setw(26) << "Payment Period: " << right << setw(40) << payPeriod << endl;
cout << left << setw(26) << "Annual Interest Rate: " << right << setw(38) << interestRate << endl;
cout << left << setw(26) << "Payment Amount: " << setw(30) << "$" << right << setw(8) << payment << endl;
system("pause");
}
| |