/****************************************************************************** * This program... * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import org.pureprogrammer.Utils; public class AmortizationTable2 { public static void main(String[] args) { if (args.length != 3) { System.out.println(Utils.format("Syntax: {0:s} loan_amount rate_per_annum term_in_years", "AmortizationTable2")); System.exit(1); } long balance = Utils.stolWithDefault(args[0], 0) * 100; double interestRate = Utils.stodWithDefault(args[1], 0.05); interestRate /= 12.0; int periods = Utils.stoiWithDefault(args[2], 30); periods *= 12; long payment = (long)(interestRate * balance / (1.0 - Math.pow(1.0 + interestRate, -periods)) + 0.5); System.out.println("Month\tPayment\tPrinciple\tInterest\tBalance"); System.out.println(Utils.format("0\t\t\t\t{0:.2f}", balance / 100.0)); for (int i = 0; i < periods; ++i) { final long INTEREST = (long)(balance * interestRate + 0.5); long principle = payment - INTEREST; balance -= principle; if (i == periods - 1) { payment += balance; principle += balance; balance = 0; } System.out.println(Utils.format("{0:d}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}", i + 1, payment / 100.0, principle / 100.0, INTEREST / 100.0, balance / 100.0)); } } }