Python面向对象编程实训

按揭贷款——定义抽象类

def findPayment(loan, r, m):
    #********** Begin *********#
    # 请在下面编写代码
    up = r*(1+r)**m
    dn = (1+r)**m-1
    return loan*(up/dn)
    # 请不要修改下面的代码
    #********** End *********#
class Mortgage(object):
    def __init__(self, loan, annRate, months):
        #********** Begin *********#
        # 请在下面编写代码
        self.loan = loan
        self.annRate = annRate
        self.months = months
        self.rate = self.annRate/12/100
        self.paid = [0.0]
        self.owed = [loan]
        self.payment = findPayment(loan,self.rate,months)
        # 请不要修改下面的代码
        #********** End *********#
        self.legend = None

    def makePayment(self):
        #********** Begin *********#
        # 请在下面编写代码
        self.paid.append(self.payment)
        reduction = self.payment - self.owed[-1] * self.rate
        self.owed.append(self.owed[-1] - reduction)
        # 请不要修改下面的代码
        #********** End *********#

    def getTotalPaid(self):
        #********** Begin *********#
        # 请在下面编写代码
        return sum(self.paid)
        # 请不要修改下面的代码
        #********** End *********#

    def __str__(self):
        return 'The Mortgage is {self.legend}, Loan is {self.loan}, Months is {self.months}, Rate is {self.rate:.2f}, Monthly payment is {self.payment:.2f}'.format(self=self)

if __name__=="__main__":
    print(Mortgage(100000, 6.5, 36))
    print(Mortgage(100000, 6.5, 120))


三种贷款方式建模

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))

比较各种贷款的利弊

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):
        Mortgage.__init__(self, loan, r, months)
        self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'


class FixedWithPoints(Mortgage):
    def __init__(self, loan, r, months, pts):
        Mortgage.__init__(self, loan, r, months)
        self.pts = pts
        self.paid = [loan * (pts / 100.0)]
        self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'


class TwoRate(Mortgage):
    def __init__(self, loan, r, months, teaserRate, teaserMonths):
        Mortgage.__init__(self, loan, teaserRate, months)
        self.teaserMonths = teaserMonths
        self.teaserRate = teaserRate/1200
        self.nextRate = r / 1200.0
        self.legend = str(teaserRate)\
                      + '% for ' + str(self.teaserMonths)\
                      + ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'

    def makePayment(self):
        if len(self.paid) == self.teaserMonths + 1:
            self.rate = self.nextRate
            self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths)
        Mortgage.makePayment(self)


def compareMortgages(amt, years, fixedRate, pts, ptsRate, varRate1, varRate2, varMonths):
    # 请在此添加代码,补全函数compareMortgages
        #********** Begin *********#
    totMonths = years * 12
    fixed1 = Fixed(amt, fixedRate, totMonths)
    fixed2 = FixedWithPoints(amt, ptsRate, totMonths, pts)
    twoRate = TwoRate(amt, varRate2, totMonths, varRate1, varMonths)
    morts = [fixed1, fixed2, twoRate]
        #********** End *********#
    for m in range(totMonths):
        # 请在此添加代码,补全函数compareMortgages
        #********** Begin *********#
        for mort in morts:
            mort.makePayment()
        #********** End *********#
    for m in morts:
        print(m)
        print('Loan ' + str(amt) + ' Total payments = ' + str(int(m.getTotalPaid())))

if __name__=="__main__":
    compareMortgages(200000, 30, 7, 3.25, 5, 4.5, 9.5, 48)
    print('*'*40)
    compareMortgages(1000000, 30, 7, 20, 5, 4.5, 9.5, 48)
    print('*' * 40)
    compareMortgages(500000, 10, 7, 20, 5, 4.5, 9.5, 48)
  • 12
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值