implement the class Loan with the following requirements:
•
A member variable that will hold the annual interest rate of the loan. Its default value will be 2.5.
•
A member variable that will hold the number of years for the loan. Its default value will be 1.
•
A member variable that will hold the loan amount. Its default variable will be 1000.
•
A default constructor.
•
Another constructor that has interest rate, years, and loan amount as its parameters.
•
A member function that returns the annual interest rate of this loan.
•
A member function that returns the number of the years of this loan.
•
A member function that returns the amount of this loan.
•
A member function that sets a new annual interest rate to this loan.
•
A member function that sets a new number of years to this loan.
•
A member function that sets a new amount to this loan.
•
A member function that returns the monthly payment of this loan.
•
A member function that returns the total payment of this loan.
#include <iostream>
using namespace std;
class Loan
{
double rate = 2.5;
int years = 1;
double amount = 1000;
public:
Loan()
{
}
Loan(double rate, int years, double amount)
{
this->rate = rate;
this->years = years;
this->amount = amount;
}
double getRate()
{
return rate;
}
int getYears()
{
return years;
}
double getAmount()
{
return amount;
}
void setRate(double rate)
{
this->rate = rate;
}
void setYears(int years)
{
this->years = years;
}
void setAmount(double amount)
{
this->amount = amount;
}
double calculateMonthlyPayment()
{
double coefficient = 1;
for (int i = 0; i < 12 * years; i++)
coefficient *= 1 + rate / 12 / 100;
return amount * rate / 12 / 100 * coefficient / (coefficient - 1);
}
double calculateTotalPayment()
{
return calculateMonthlyPayment() * 12 * years;
}
};
int main()
{
Loan loan(7.5, 5, 10000);
cout << "Annual interest rate = " << loan.getRate() << endl;
cout << "Number of years = " << loan.getYears() << endl;
cout << "Loan amount = " << loan.getAmount() << endl;
cout << "Monthly payment = " << loan.calculateMonthlyPayment() << endl;
cout << "Total payment = " << loan.calculateTotalPayment() << endl;
return 0;
}
Comments
Leave a comment