Task description
Based on the Mortgage class implemented in the previous level, the inheritance mechanism is used to complete the correlation functions respectively to meet the requirements of modeling with three loan methods.
Relevant knowledge
Based on the three loan methods introduced in the previous chapter:
Fixed interest rate, no payment of any point;
Fixed interest rate, pay a certain point;
Variable interest rate, low interest rate, followed by high interest rate.
Taking Mortgage as the parent class, define three subclasses Fixed, FixedWithPts and TwoRate. These three subclasses are initialized by the init function of the base class Mortgage, and then fill in their own loan type description on the legend attribute. First, the TwoRate class has two interest rates. Two attributes, teaserRate and nextRate, are added. After the teaserRate expires, the interest will be paid according to the nextRate interest rate. In addition, Fixed withpts has paid a certain proportion of the down payment before repayment, and its monthly repayment amount is different. pts in FixedWithPts is the proportion of the first payment, for example, 20%, then pts is initialized to 20. The monthly loan repayment amount of TwoRate is divided into two periods. Please note that the interest rates in these two periods are different, which are teaserRate and r in the initialization function.
Programming requirements
The programming task of this pass is to complete the code of begin end interval in 7-2.py file and realize the requirements of modeling with three loan methods. Specific requirements are as follows:
It is required to complete the init function and makePayment function defined in 3 subclasses in 7-2.py file to realize the modeling function of three loan modes;
See the following test examples for details.
The code framework of the code file 7-2.py involved in this level is as follows:
def findPayment(loan, r, m): return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1)) class Mortgage(object): def __init__(self, loan, annRate, months): self.loan = loan self.rate = annRate / 1200.0 self.months = months self.paid = [0.0] self.owed = [loan] self.payment = findPayment(loan, self.rate, self.months) self.legend = None def makePayment(self): self.paid.append(self.payment) reduction = self.payment - self.owed[-1] * self.rate self.owed.append(self.owed[-1] - reduction) def getTotalPaid(self): return sum(self.paid) def __str__(self): return str(self.legend) class Fixed(Mortgage): def __init__(self, loan, r, months): # Please add code here to complete the function__ init__ #********** Begin *********# #********** End *********# self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months' class FixedWithPoints(Mortgage): def __init__(self, loan, r, months, pts): # Please add code here to complete the function__ init__ #********** Begin *********# #********** End *********# self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months' class TwoRate(Mortgage): def __init__(self, loan, r, months, teaserRate, teaserMonths): # Please add code here to complete the function__ init__ #********** Begin *********# #********** End *********# self.legend = str(teaserRate)\ + '% for ' + str(self.teaserMonths)\ + ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months' def makePayment(self): # Please add code here to complete the function makePayment #********** Begin *********# #********** End *********# Mortgage.makePayment(self) if __name__=="__main__": print(Fixed(100000, 6.5, 36)) print(Fixed(100000, 6.5, 120)) print(FixedWithPoints(100000, 6.5, 36, 20)) print(FixedWithPoints(100000, 6.5, 120, 20)) print(TwoRate(100000, 9.0, 36, 4.8, 12)) print(TwoRate(100000, 7.0, 120, 4.8, 36))
Test description
The platform will test the code you write:
Test input:
No input
Expected output:
Fixed, 6.5%, for 36 months
Fixed, 6.5%, for 120 months
Fixed, 6.5%, 20 points, for 36 months
Fixed, 6.5%, 20 points, for 120 months
4.8% for 12 months,
then 9.0%, for 36 months
4.8% for 36 months,
then 7.0%, for 120 months
Start your mission. I wish you success!
As long as we can dream, we can realize it.
Code example
def findPayment(loan, r, m): return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1)) class Mortgage(object): def __init__(self, loan, annRate, months): self.loan = loan self.rate = annRate / 1200.0 self.months = months self.paid = [0.0] self.owed = [loan] self.payment = findPayment(loan, self.rate, self.months) self.legend = None def makePayment(self): self.paid.append(self.payment) reduction = self.payment - self.owed[-1] * self.rate self.owed.append(self.owed[-1] - reduction) def getTotalPaid(self): return sum(self.paid) def __str__(self): return str(self.legend) class Fixed(Mortgage): def __init__(self, loan, r, months): # Please add code here to complete the function__ init__ #********** Begin *********# self.loan = loan self.r = r self.months = months #********** End *********# self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months' class FixedWithPoints(Mortgage): def __init__(self, loan, r, months, pts): # Please add code here to complete the function__ init__ #********** Begin *********# self.loan = loan self.pts = pts self.months = months #********** End *********# self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months' class TwoRate(Mortgage): def __init__(self, loan, r, months, teaserRate, teaserMonths): # Please add code here to complete the function__ init__ #********** Begin *********# self.loan = loan self.r = r self.months = months self.teaserRate = teaserRate self.teaserMonths = teaserMonths #********** End *********# self.legend = str(teaserRate)\ + '% for ' + str(self.teaserMonths)\ + ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months' def makePayment(self): # Please add code here to complete the function makePayment #********** Begin *********# if len(self.paid) == self.teaserMonths + 1: self.rate = self.nextRate self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths) #********** End *********# Mortgage.makePayment(self) if __name__=="__main__": print(Fixed(100000, 6.5, 36)) print(Fixed(100000, 6.5, 120)) print(FixedWithPoints(100000, 6.5, 36, 20)) print(FixedWithPoints(100000, 6.5, 120, 20)) print(TwoRate(100000, 9.0, 36, 4.8, 12)) print(TwoRate(100000, 7.0, 120, 4.8, 36))