can display schedule in different ways

This commit is contained in:
- 2024-02-10 12:15:32 -08:00
parent ccb39cfc21
commit ed2e72267c

View File

@ -53,13 +53,12 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, extra_pa
amortization_schedule.append(payment_details) amortization_schedule.append(payment_details)
return amortization_schedule return amortization_schedule
def get_totals(amortization_schedule, display=False, export=False): def get_totals(amortization_schedule, func=None):
total_paid = 0 total_paid = 0
total_interest_paid = 0 total_interest_paid = 0
total_principal_paid = 0 total_principal_paid = 0
# Display the amortization schedule # Display the amortization schedule
if display: messages = []
print("id, paid, interest payment, principal payment, remaining")
for payment in amortization_schedule: for payment in amortization_schedule:
# print(payment) # print(payment)
total_paid += payment["Payment Amount"] total_paid += payment["Payment Amount"]
@ -68,8 +67,10 @@ def get_totals(amortization_schedule, display=False, export=False):
if payment["Remaining Balance"] < 0: if payment["Remaining Balance"] < 0:
break break
attrs = [payment[key] for key in payment] attrs = [payment[key] for key in payment]
if display: if func is not None:
print("%s" % ", ".join([str(attr) for attr in attrs])) 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 return total_paid, total_interest_paid, total_principal_paid
if __name__ == "__main__": if __name__ == "__main__":
@ -104,9 +105,21 @@ if __name__ == "__main__":
def compare(with_extra_payments, without): def compare(with_extra_payments, without):
x,y,z = get_totals(with_extra_payments) x,y,z = get_totals(with_extra_payments)
a,b,c = get_totals(without) a,b,c = get_totals(without)
print(chr(916), "paid: ", round(a-x,2)) print(chr(916), "paid: ", round(x-a,2))
print(chr(916), "interest paid: ", round(b-y,2)) print(chr(916), "interest paid: ", round(y-b,2))
print(chr(916), "principal paid: ", round(c-z,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() 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) 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 paid: ", round(paid,2))
print("total interest paid: ", round(interest_paid,2)) print("total interest paid: ", round(interest_paid,2))