Thêm một phương thức vào lớp kế thừa
Hãy thêm một phương thức vào MortgageCalculator để tính khoản thanh toán hàng tháng khi biết số tiền gốc, lãi suất hằng năm và số năm trả nợ.
Bài tập này là một phần của khóa học
Nghiên cứu tình huống: Xây dựng phần mềm bằng Python
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
class MortgageCalculator(FinancialCalculator):
def __init__(self, loan_amount, annual_interest_rate, years):
super().__init__()
self.loan_amount = loan_amount
self.monthly_interest_rate = self.monthly_interest(annual_interest_rate)
self.months = self.multiply(years, 12)
self.monthly_payment = self.calculate_monthly_payment()
def calculate_monthly_payment(self):
numerator = self.monthly_interest_rate * (1 + self.monthly_interest_rate) ** self.months
denominator = (1 + self.monthly_interest_rate) ** self.months - 1
# Compute the quotient of numerator and denominator and save the result to multiplier
multiplier = self.divide(____, ____)