From fdf01baf6a7d2da74913a79e65484c889dd10d48 Mon Sep 17 00:00:00 2001 From: - <-> Date: Fri, 9 Feb 2024 20:31:04 -0800 Subject: [PATCH] handle multiple one-time payments --- amort.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/amort.py b/amort.py index 71aae2c..24d4e11 100644 --- a/amort.py +++ b/amort.py @@ -1,7 +1,7 @@ import argparse, json -def generate_amortization_schedule(principal, interest_rate, loan_term, one_time_payment=None): +def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]): # Convert interest rate to decimal and calculate periodic interest rate monthly_interest_rate = interest_rate / 12 / 100 @@ -16,6 +16,9 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, one_time remaining_balance = principal amortization_schedule = [] + one_time_payment = None + if extra_payments != []: + one_time_payment = extra_payments.pop(0) for payment_number in range(1, num_payments + 1): # Calculate interest for the current period interest_payment = round(remaining_balance * monthly_interest_rate, 2) @@ -27,6 +30,8 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, one_time if one_time_payment and payment_number == one_time_payment['payment_number']: principal_payment += one_time_payment['amount'] remaining_balance -= one_time_payment['amount'] + if extra_payments != []: + one_time_payment = extra_payments.pop(0) # Update remaining balance remaining_balance -= principal_payment @@ -63,19 +68,28 @@ if __name__ == "__main__": help="sets the term (years)") p.add_argument("--one-time", "-ot", type=str,\ help="factors in a one-time payment (json, example: {\"payment_number\":13,\"amount\":5000}") + p.add_argument("--extra-payments", "-ep", type=str,\ + help="facts in multiple one time payments (json file name)") args = p.parse_args() - return args.principal, args.interest_rate, args.term, args.one_time + l = [] + if args.extra_payments is not None: + with open(args.extra_payments) as f: + l = json.loads(f.read()) + # print(extra["extra-payments"]) + if args.one_time is not None: + l.append(json.loads(args.one_time)) + extra = l["extra-payments"] + extra.sort(key=lambda k: k["payment_number"]) + return args.principal, args.interest_rate, args.term, extra + + principal, interest_rate, loan_term, extra_payments = get_arguments() - principal, interest_rate, loan_term, one_time = get_arguments() - if one_time is not None: - one_time_payment = json.loads(one_time) - else: one_time_payment = None # one_time_payment = { # 'payment_number': 13, # 'amount': 5000 # } - schedule = generate_amortization_schedule(principal, interest_rate, loan_term, one_time_payment) + schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments) total_paid = 0 total_interest_paid = 0