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
|
#include <iostream>
#include <cmath>
using namespace std;
class Loan
{
public:
Loan();
Loan(double newAnnualInterestRate, int newNumberOfYears, double newLoanAmount);
double getAnnualInterestRate() const;
int getNumberOfYears() const;
double getLoanAmount() const;
void setAnnualInterestRate(double newAnnualInterestRate);
void setNumberOfYears(int newNumberOfYears);
void setLoanAmount(double newLoanAmount);
double getMonthlyPayment() const;
double getTotalPayment() const;
static double getMonthlyPayment(double annualInterestRate, int numberOfYears, double loanAmount);
static double getTotalPayment(double annualInterestRate, int numberOfYears, double loanAmount);
private:
double annualInterestRate;
int numberOfYears;
double loanAmount;
};
Loan::Loan()
{
annualInterestRate = 5.5;
numberOfYears = 15;
loanAmount = 40000;
}
Loan::Loan(double newAnnualInterestRate, int newNumberOfYears, double newLoanAmount)
{
annualInterestRate = newAnnualInterestRate;
numberOfYears = newNumberOfYears;
loanAmount = newLoanAmount;
}
double Loan::getAnnualInterestRate() const
{
return annualInterestRate;
}
int Loan::getNumberOfYears() const
{
return numberOfYears;
}
double Loan::getLoanAmount() const
{
return loanAmount;
}
void Loan::setAnnualInterestRate(double newAnnualInterestRate)
{
annualInterestRate = newAnnualInterestRate;
}
void Loan::setNumberOfYears(int newNumberOfYears)
{
numberOfYears = newNumberOfYears;
}
void Loan::setLoanAmount(double newLoanAmount)
{
loanAmount = newLoanAmount;
}
double Loan::getMonthlyPayment() const
{
double monthlyInterestRate = annualInterestRate / 1200;
return loanAmount * monthlyInterestRate / (1 - (pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
}
double Loan::getTotalPayment() const
{
double monthlyInterestRate=annualInterestRate/1200;
return (loanAmount * monthlyInterestRate / (1 - (pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)))*180);
}
int main(){
Loan newLoan;
newLoan.getMonthlyPayment(5.5, 15, 40000);
newLoan.getTotalPayment(5.5, 15, 40000);
cout<<" your monthly payment is "<<newLoan.getMonthlyPayment()<<".";
cout<< "Your total payment is"<< newLoan.getTotalPayment()<<endl;
}
| |