Skip to content

Commit 9b7a06c

Browse files
authored
Merge pull request #88 from Josh-Voyles/hoa-fees-backend
Add HOA fee calculator logic for issue #72
2 parents d540b5e + 28063e0 commit 9b7a06c

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

home-choice-pro/models/affordability_calculator.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ class AffordabilityCalculator:
2626
interest_rate: float
2727
loan_term: float
2828
home_affordability_price: int
29+
hoa_monthly_fee: float
2930

3031
# 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"):
3234
"""Initializes class variables."""
3335
self.monthly_payment = self.convert_string_number_into_float(monthly_payment)
3436
self.down_payment = self.convert_string_number_into_float(down_payment)
3537
self.interest_rate = self.convert_string_number_into_float(interest_rate)
3638
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)
3740

3841
# Variable Checking Functions
3942
def _user_inputs_are_valid(self) -> bool:
@@ -42,7 +45,8 @@ def _user_inputs_are_valid(self) -> bool:
4245
self.monthly_payment,
4346
self.down_payment,
4447
self.interest_rate,
45-
self.loan_term
48+
self.loan_term,
49+
self.hoa_monthly_fee
4650
]
4751
if any(var == -1.0 for var in class_variables):
4852
return False
@@ -60,23 +64,13 @@ def convert_string_number_into_float(number) -> float:
6064
return -1.0
6165

6266
# 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-
6967
def calculate_home_affordability_price(self) -> str:
7068
"""Calculates the maximum home price that a user can afford."""
7169
if not self._user_inputs_are_valid():
7270
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)
8074
home_affordability_price = loan_affordability_price + self.down_payment
8175
self.home_affordability_price = round(home_affordability_price)
8276
return str(round(home_affordability_price))
@@ -122,7 +116,13 @@ def _calculate_denominator(self) -> float:
122116

123117
def _calculate_loan_affordability(self, numerator, denominator) -> float:
124118
"""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
126126

127127
def _calculate_monthly_payment(self) -> float:
128128
"""Helper function for the calculate_total_home_loan_price() function."""

home-choice-pro/tests/test_models/test_affordability_calculator.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
@pytest.fixture
66
def calculator():
7-
return AffordabilityCalculator("1500.0", "20000.0", "5", "30")
7+
return AffordabilityCalculator("1500.0", "20000.0", "5", "30", "50")
88

99
@pytest.fixture
1010
def zero_interest_calculator():
11-
return AffordabilityCalculator("1500.0", "20000.0", "0", "30")
11+
return AffordabilityCalculator("1500.0", "20000.0", "0", "30", "50")
12+
13+
@pytest.fixture
14+
def empty_calculator_fields():
15+
return AffordabilityCalculator()
1216

1317

1418
def test_user_inputs_are_valid(calculator):
@@ -27,32 +31,37 @@ def test_convert_string_number_into_float():
2731
def test_calculate_home_affordability_price(calculator):
2832
result = calculator.calculate_home_affordability_price()
2933
assert result != "Invalid User Inputs"
30-
assert calculator.calculate_home_affordability_price() == "299422"
34+
assert calculator.calculate_home_affordability_price() == "290108"
3135

3236

3337
def test_calculate_home_affordability_price_with_zero_interest(zero_interest_calculator):
3438
result = zero_interest_calculator.calculate_home_affordability_price()
3539
assert result != "Invalid User Inputs"
36-
assert result == "560000"
40+
assert result == "542000"
41+
42+
def test_empty_calculator_fields(empty_calculator_fields):
43+
result = empty_calculator_fields.calculate_home_affordability_price()
44+
assert result != "Invalid User Inputs"
45+
assert result == "0"
3746

3847

3948
def test_calculate_total_home_loan_price(calculator):
4049
calculator.calculate_home_affordability_price()
4150
result = calculator.calculate_total_home_loan_price()
4251
assert result != "Invalid Calculation From _calculate_monthly_payment() Function"
43-
assert calculator.calculate_total_home_loan_price() == "539999"
52+
assert calculator.calculate_total_home_loan_price() == "521999"
4453

4554

4655
def test_calculate_loan_principal(calculator):
4756
calculator.calculate_home_affordability_price()
4857
result = calculator.calculate_loan_principal()
49-
assert calculator.calculate_loan_principal() == "279422"
58+
assert calculator.calculate_loan_principal() == "270108"
5059

5160

5261
def test_calculate_loan_interest(calculator):
5362
calculator.calculate_home_affordability_price()
5463
result = calculator.calculate_loan_interest()
55-
assert calculator.calculate_loan_interest() == "260577"
64+
assert calculator.calculate_loan_interest() == "251891"
5665

5766

5867
def test_calculate_numerator(calculator):

0 commit comments

Comments
 (0)