diff --git a/amort.py b/amort.py index eb0fae0..31827db 100644 --- a/amort.py +++ b/amort.py @@ -53,13 +53,12 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, extra_pa amortization_schedule.append(payment_details) return amortization_schedule -def get_totals(amortization_schedule, display=False, export=False): +def get_totals(amortization_schedule, func=None): total_paid = 0 total_interest_paid = 0 total_principal_paid = 0 # Display the amortization schedule - if display: - print("id, paid, interest payment, principal payment, remaining") + messages = [] for payment in amortization_schedule: # print(payment) total_paid += payment["Payment Amount"] @@ -68,8 +67,10 @@ def get_totals(amortization_schedule, display=False, export=False): if payment["Remaining Balance"] < 0: break attrs = [payment[key] for key in payment] - if display: - print("%s" % ", ".join([str(attr) for attr in attrs])) + if func is not None: + messages.append("%s" % ", ".join([str(attr) for attr in attrs])) + if func is not None: + func(messages) return total_paid, total_interest_paid, total_principal_paid if __name__ == "__main__": @@ -104,9 +105,21 @@ if __name__ == "__main__": def compare(with_extra_payments, without): x,y,z = get_totals(with_extra_payments) a,b,c = get_totals(without) - print(chr(916), "paid: ", round(a-x,2)) - print(chr(916), "interest paid: ", round(b-y,2)) - print(chr(916), "principal paid: ", round(c-z,2)) + print(chr(916), "paid: ", round(x-a,2)) + print(chr(916), "interest paid: ", round(y-b,2)) + print(chr(916), "principal paid: ", round(abs(z-c),2)) + + def display(table): + print("id, paid, interest payment, principal payment, remaining") + for row in table: + print(row) + + def export(table, filename="schedule.csv"): + with open(filename, 'w') as f: + print("id, paid, interest payment, principal payment, remaining", file=f) + for row in table: + print(row, file=f) + print("wrote to file", filename) principal, interest_rate, loan_term, extra_payments = get_arguments() @@ -116,7 +129,7 @@ if __name__ == "__main__": # } schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments) - paid, interest_paid, principal_paid = get_totals(schedule, True) + paid, interest_paid, principal_paid = get_totals(schedule,export) print("total paid: ", round(paid,2)) print("total interest paid: ", round(interest_paid,2))