Skip to content

Commit d64cc84

Browse files
author
Matthew Shirtliffe
committed
months to pay off credit card with tests and clean up previous
1 parent ee7334f commit d64cc84

File tree

7 files changed

+128
-38
lines changed

7 files changed

+128
-38
lines changed

password_strength_indicator/app.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,9 @@ def password_validator(password):
3535
elif len(password) >= 8 and special_characters_regex.search(password):
3636
print(f'The password \'{password}\' is a very strong password')
3737

38-
def prompt_get_strings():
39-
print('Enter two strings and I\'ll tell you if they\nare anagrams: ')
40-
41-
42-
def get_first_string():
43-
44-
while True:
45-
first_string = input('Enter the first string: ')
46-
47-
if len(first_string) >=3:
48-
return first_string
49-
else:
50-
print('A valid input is required')
51-
52-
53-
def get_second_string():
54-
55-
while True:
56-
second_string = input('Enter the second string: ')
57-
58-
if len(second_string) >=3:
59-
return second_string
60-
else:
61-
print('A valid input is required')
62-
63-
64-
def is_anagram(first_string, second_string):
65-
66-
return sorted(first_string) == sorted(second_string)
67-
68-
69-
def print_is_anagram(first_string, second_string):
70-
if(is_anagram(first_string, second_string)):
71-
print(f'{first_string} and {second_string} are anagrams.')
7238

7339
if __name__ == "__main__":
7440

7541
print_header()
76-
prompt_get_strings()
77-
first_string = get_first_string()
78-
second_string = get_second_string()
79-
print_is_anagram(first_string, second_string)
42+
password = get_password()
43+
password_validator(password)

pay_off_credit_card/app.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import math
2+
3+
def print_header():
4+
""" Print header
5+
"""
6+
header_text = ''
7+
TEXT = f' Months to Pay Off a Credit Card \n'
8+
line = '-' * len(TEXT)
9+
line += '\n'
10+
11+
header_text += line
12+
header_text += TEXT
13+
header_text += line
14+
print(header_text)
15+
16+
17+
def get_balance():
18+
19+
while True:
20+
try:
21+
balance = input('What is your balance? ')
22+
return int(balance)
23+
except ValueError:
24+
print('A valid input is required')
25+
26+
27+
def get_apr():
28+
29+
while True:
30+
try:
31+
apr = input('What is the APR on the card (as a percent)? ')
32+
return int(apr)
33+
except ValueError:
34+
print('A valid input is required')
35+
36+
37+
def get_monthly_payments():
38+
39+
while True:
40+
try:
41+
monthly_payment = input('What are the monthly payment you can make? ')
42+
return int(monthly_payment)
43+
except ValueError:
44+
print('A valid input is required')
45+
46+
47+
def calculate_months_until_paid_off(balance, apr, monthly_payments):
48+
apr = apr/100
49+
apr = apr/365
50+
months_until_paid_off = (-1/30) * math.log(1+(balance/monthly_payments)*(1-(1+apr)**30),10)/math.log(1+apr,10)
51+
return math.ceil(months_until_paid_off)
52+
53+
def print_months_take_to_pay(months):
54+
print(f'It will take you {months} months to pay off this card')
55+
56+
if __name__ == "__main__":
57+
print_header()
58+
balance = get_balance()
59+
apr = get_apr()
60+
monthly_payments = get_monthly_payments()
61+
months_until_paid_off = calculate_months_until_paid_off(balance, apr, monthly_payments)
62+
print_months_take_to_pay(months_until_paid_off)
63+

pay_off_credit_card/tests/__init__.py

Whitespace-only changes.

pay_off_credit_card/tests/system/__init__.py

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
import app
4+
5+
class AppTest(TestCase):
6+
7+
def test_print_header(self):
8+
9+
expected = '----------------------------------\n Months to Pay Off a Credit Card \n----------------------------------\n'
10+
with patch('builtins.print') as mocked_print:
11+
app.print_header()
12+
mocked_print.assert_called_with(expected)
13+
14+
def test_get_balance(self):
15+
16+
expected = 'What is your balance? '
17+
with patch('builtins.input') as mocked_input:
18+
with patch('builtins.print') as mocked_print:
19+
mocked_input.side_effect = ('' , '5000')
20+
balance = app.get_balance()
21+
mocked_print.assert_called_with('A valid input is required')
22+
mocked_input.assert_called_with(expected)
23+
self.assertEqual(balance, 5000)
24+
25+
def test_get_apr(self):
26+
expected = 'What is the APR on the card (as a percent)? '
27+
with patch('builtins.input') as mocked_input:
28+
with patch('builtins.print') as mocked_print:
29+
mocked_input.side_effect = ('' , '12')
30+
apr = app.get_apr()
31+
mocked_print.assert_called_with('A valid input is required')
32+
mocked_input.assert_called_with(expected)
33+
self.assertEqual(apr, 12)
34+
35+
def test_get_monthly_payments(self):
36+
expected = 'What are the monthly payment you can make? '
37+
with patch('builtins.input') as mocked_input:
38+
with patch('builtins.print') as mocked_print:
39+
mocked_input.side_effect = ('' , '100')
40+
monthly_payments = app.get_monthly_payments()
41+
mocked_print.assert_called_with('A valid input is required')
42+
mocked_input.assert_called_with(expected)
43+
self.assertEqual(monthly_payments, 100)
44+
45+
def test_print_months_take_to_pay(self):
46+
47+
months = 70
48+
expected = f'It will take you {months} months to pay off this card'
49+
with patch('builtins.print') as mocked_print:
50+
app.print_months_take_to_pay(months)
51+
mocked_print.assert_called_with(expected)

pay_off_credit_card/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from unittest import TestCase
2+
import app
3+
4+
class PayOffCreditCardTest(TestCase):
5+
6+
def test_calculate_months_until_paid_off(self):
7+
balance = 5000
8+
apr = 12
9+
monthly_payments = 100
10+
11+
months_until_paid_off = app.calculate_months_until_paid_off(balance, apr, monthly_payments)
12+
self.assertEqual(months_until_paid_off, 70)

0 commit comments

Comments
 (0)