@@ -26,14 +26,17 @@ class AffordabilityCalculator:
26
26
interest_rate : float
27
27
loan_term : float
28
28
home_affordability_price : int
29
+ hoa_monthly_fee : float
29
30
30
31
# Constructor
31
- def __init__ (self , monthly_payment : str , down_payment : str , interest_rate : str , loan_term : str ):
32
+ def __init__ (self , monthly_payment : str = "0" , down_payment : str = "0" ,
33
+ interest_rate : str = "0" , loan_term : str = "0" , hoa_monthly_fee : str = "0" ):
32
34
"""Initializes class variables."""
33
35
self .monthly_payment = self .convert_string_number_into_float (monthly_payment )
34
36
self .down_payment = self .convert_string_number_into_float (down_payment )
35
37
self .interest_rate = self .convert_string_number_into_float (interest_rate )
36
38
self .loan_term = self .convert_string_number_into_float (loan_term )
39
+ self .hoa_monthly_fee = self .convert_string_number_into_float (hoa_monthly_fee )
37
40
38
41
# Variable Checking Functions
39
42
def _user_inputs_are_valid (self ) -> bool :
@@ -42,7 +45,8 @@ def _user_inputs_are_valid(self) -> bool:
42
45
self .monthly_payment ,
43
46
self .down_payment ,
44
47
self .interest_rate ,
45
- self .loan_term
48
+ self .loan_term ,
49
+ self .hoa_monthly_fee
46
50
]
47
51
if any (var == - 1.0 for var in class_variables ):
48
52
return False
@@ -60,23 +64,13 @@ def convert_string_number_into_float(number) -> float:
60
64
return - 1.0
61
65
62
66
# Calculation Functions
63
- def test_calculate_home_affordability_price_with_zero_interest (self , zero_interest_calculator ):
64
- """Test."""
65
- result = zero_interest_calculator .calculate_home_affordability_price ()
66
- assert result != "Invalid User Inputs"
67
- assert result == "560000"
68
-
69
67
def calculate_home_affordability_price (self ) -> str :
70
68
"""Calculates the maximum home price that a user can afford."""
71
69
if not self ._user_inputs_are_valid ():
72
70
return "Invalid User Inputs"
73
- if self .interest_rate == 0 :
74
- loan_term_months = self ._convert_loan_term_length_into_months ()
75
- loan_affordability_price = self .monthly_payment * loan_term_months
76
- else :
77
- numerator = self ._calculate_numerator ()
78
- denominator = self ._calculate_denominator ()
79
- loan_affordability_price = self ._calculate_loan_affordability (numerator , denominator )
71
+ numerator = self ._calculate_numerator ()
72
+ denominator = self ._calculate_denominator ()
73
+ loan_affordability_price = self ._calculate_loan_affordability (numerator , denominator )
80
74
home_affordability_price = loan_affordability_price + self .down_payment
81
75
self .home_affordability_price = round (home_affordability_price )
82
76
return str (round (home_affordability_price ))
@@ -122,7 +116,13 @@ def _calculate_denominator(self) -> float:
122
116
123
117
def _calculate_loan_affordability (self , numerator , denominator ) -> float :
124
118
"""Helper function for the calculate_home_affordability_price() function."""
125
- return (self .monthly_payment * denominator ) / numerator
119
+ monthly_payment = self .monthly_payment - self .hoa_monthly_fee
120
+ if monthly_payment < 0 : # Desired monthly payment can't be less than monthly HOA fees
121
+ return - 1
122
+ loan_term = self ._convert_loan_term_length_into_months ()
123
+ if numerator == 0 : # Numerator is 0 if interest rate inputted was 0%
124
+ return monthly_payment * loan_term
125
+ return (monthly_payment * denominator ) / numerator # If interest rate > 0%, return this
126
126
127
127
def _calculate_monthly_payment (self ) -> float :
128
128
"""Helper function for the calculate_total_home_loan_price() function."""
0 commit comments