import java.io.*; import java.math.*; import java.text.DecimalFormat; public class Amortization { private double loanAmount; private double interestRate; private double loanBalance; private double term; private double payment; private int loanYears; public Amortization(double loan, double rate, int years) { this.loanAmount = loan; this.loanBalance = loan; this.interestRate = rate; this.loanYears = years; calcPayment(); } private void calcPayment() { this.term = Math.pow(interestRate / 12 + 1, loanYears * 12); this.payment = (loanAmount * (interestRate / 12.0) * term) / (term - 1); } public int getNumberOfPayments() { return (loanYears * 12); } public double getLoanAmount() { return loanAmount; } public double getInterestRate() { return interestRate; } public int getLoanYears() { return loanYears; } public void saveReport(String filename) throws FileNotFoundException { DecimalFormat df = new DecimalFormat("0.00"); File file = new File(filename); PrintWriter pw = new PrintWriter(file); pw.println("Monthly Payment: $" + df.format(payment)); pw.println("Month\tInterest\tPrincipal\tBalance"); pw.println("-----------------------------------------------"); for (int i = 0; i < getNumberOfPayments(); i++) { double interest = interestRate / 12.0 * loanBalance; double principal = payment - interest; loanBalance -= principal; pw.println((i + 1) + "\t" + df.format(interest) + "\t\t" + df.format(principal) + "\t\t" + df.format(loanBalance)); } pw.close(); } }