任务描述
基于上一关实现的 Mortgage 类,利用继承机制,分别补全相关函数,实现用三种贷款方式建模的要求。
相关知识
基于上一关介绍的三种贷款方式:
固定利息率,不支付任何点;
固定利息率,支付一定的点;
可变利息率,低引诱利率,随后高利息率。
以 Mortgage 为父类,定义三个子类 Fixed 、 FixedWithPts 和 TwoRate ,这三个子类都利用基类 Mortgage 的 init 函数进行初始化,然后在 legend 属性上,填上自己的贷款类型描述。首先,TwoRate 类有两个利率,新增了 teaserRate 和 nextRate 两个属性,在 teaserRate 到期后,按 nextRate 利率支付利息。此外,FixedWithPts 在还贷之前,已经支付了一定比例的首付,其每月还贷金额是不同的。FixedWithPts 中的 pts 即为首次支付的比例,例如 20% ,则 pts 初始化为 20 。 TwoRate 的每月还贷金额分为两个时期,请注意这两个时期的利率是不一样的,分别为初始化函数中的 teaserRate 和 r 。
编程要求
本关的编程任务是,补全 7-2.py文件中 Begin-End 区间的代码,实现用三种贷款方式建模的要求。具体要求如下:
本关要求补全7-2.py文件中,3 个子类定义的 init 函数和 makePayment 函数,以实现三种贷款方式建模的功能;
具体请参见后续测试样例。
本关涉及的代码文件 7-2.py 的代码框架如下:
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):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
#********** End *********#
self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'
class FixedWithPoints(Mortgage):
def __init__(self, loan, r, months, pts):
# 请在此添加代码,补全函数__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):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
#********** End *********#
self.legend = str(teaserRate)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'
def makePayment(self):
# 请在此添加代码,补全函数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))
测试说明
平台会对你编写的代码进行测试:
测试输入:
无输入
预期输出:
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
开始你的任务吧,祝你成功!
只要我们能梦想的,我们就能实现。
代码示例
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):
# 请在此添加代码,补全函数__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):
# 请在此添加代码,补全函数__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):
# 请在此添加代码,补全函数__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):
# 请在此添加代码,补全函数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))