Programming code
Create an application that will allow a loan amount, interest rate, and number of finance years to be entered for a given loan.
Determine the monthly payment amount. Calculate how much interest will be paid over the life of the loan. Display an amortization schedule showing the new balance after each payment is made.
Design an object-oriented solution. Use two classes.
Loan class: characteristics such as the amount to be financed, rate of interest, period of time for the loan, and total interest paid will identify the current state of a loan object. Include methods to determine the monthly payment amount, return the total interest paid over the life of the loan, and Loantable function to display an amortization schedule that include: number of the cycle, payment amount, principal paid, interest paid, total interest paid, balance
LoanTest class:
In the main class: instantiate an object of the loan class. Allow the user to input data about the loan. Then use Loantable function to display the Loan information.
Sample Answer
Loan Class
class Loan:
def __init__(self, amount, interest_rate, years):
self.amount = amount
self.interest_rate = interest_rate
self.years = years
self.total_interest_paid = 0
def get_monthly_payment(self):
monthly_interest_rate = self.interest_rate / 12
number_of_payments = self.years * 12
monthly_payment = (
self.amount
* (monthly_interest_rate / (1 - (1 + monthly_interest_rate) ** -number_of_payments))
)
return monthly_payment
def get_total_interest_paid(self):
total_interest_paid = 0
for i in range(self.years * 12):
monthly_interest_rate = self.interest_rate / 12
principal_payment = self.amount / (self.years * 12)
interest_payment = self.amount - principal_payment
self.amount -= principal_payment
total_interest_paid += interest_payment
self.total_interest_paid = total_interest_paid
return total_interest_paid
def loan_table(self):
table = []
for i in range(self.years * 12):
monthly_interest_rate = self.interest_rate / 12
principal_payment = self.amount / (self.years * 12)
interest_payment = self.amount - principal_payment
self.amount -= principal_payment
self.total_interest_paid += interest_payment