import java.text.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class Amortization
{private double debt;
private double rate;
private int periods;
private double payment = -1;
private double totalPayment = -1;
private double totalInterest = -1;
private double[] principalPaid;
private double[] interestPaid;
private double[] cumulativePrincipal; private double[] cumulativeInterest; private double[] cumulativePayment; private double[] principalBalance;
private boolean scheduleSet = false;public Amortization(double debt, double rate, int periods)
throws IllegalArgumentException { if (debt <= 0 || rate <= 0 || periods <= 0) throw new IllegalArgumentException(); setDebt(debt); setRate(rate); setPeriods(periods); setPayment(); setTotalPayment();
}
public double getDebt()
{return debt;
}public double getRate()
{return rate;
}public int getPeriods()
{return periods;
}public double getPayment()
{ if (payment == -1) setPayment(); return payment;}
public double getTotalPayment()
{ if (totalPayment == -1) setTotalPayment(); return totalPayment;}
public double getTotalInterest()
{ if (totalInterest == -1) setTotalInterest(); return totalInterest;}
public double[] getPrincipalPaid()
{ if (!scheduleSet) setSchedule(); return principalPaid;}
public double[] getInterestPaid()
{ if (!scheduleSet) setSchedule(); return interestPaid;}
public double[] getCumulativePrincipal() { if (!scheduleSet) setSchedule(); return cumulativePrincipal;
}
public double[] getCumulativeInterest() { if (!scheduleSet) setSchedule(); return cumulativeInterest;
}
public double[] getCumulativePayment() { if (!scheduleSet) setSchedule(); return cumulativePayment;
}
public double[] getPrincipalBalance() { if (!scheduleSet) setSchedule(); return principalBalance;
}
public String toString()
{ if (!scheduleSet) setSchedule(); NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat)NumberFormat.getCurrencyInstance(); FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD); // int i = fp.getEndIndex(); // df.applyPattern("########.00"); StringBuffer sb = new StringBuffer(); sb.append("\n"); sb.append("AMORTIZATION SCHEDULE"); sb.append("\n"); sb.append("\n"); sb.append("Initial debt: "); sb.append(nf.format(debt)); sb.append("\n"); sb.append("Periodic rate: "); sb.append(rate); sb.append("\n"); sb.append("Number of periods: "); sb.append(periods); sb.append("\n"); sb.append("Periodic Payment: "); sb.append(nf.format(getPayment())); sb.append("\n"); sb.append("Total Payment: "); sb.append(nf.format(getTotalPayment())); sb.append("\n"); sb.append("\n"); sb.append("Total Interest: "); sb.append(nf.format(getTotalInterest())); sb.append("\n"); sb.append("Period Principal Paid Interest Paid Cumulative Principal Cumulative interest Cumulative Payment Principal Balance\n"); sb.append("------ -------------- ------------- -------------------- ------------------- ------------------ -----------------\n"); for (int period = 0; period <= periods; period++) { sb.append(nf.format(period)); sb.append(" "); sb.append(df.format(principalPaid[period])); sb.append(" "); sb.append(df.format(interestPaid[period])); sb.append(" "); sb.append(df.format(cumulativePrincipal[period])); sb.append(" "); sb.append(df.format(cumulativeInterest[period])); sb.append(" "); sb.append(df.format(cumulativePayment[period])); sb.append(" "); sb.append(df.format(principalBalance[period])); sb.append("\n"); } return sb.toString();}
public static void main(String[] args) { Amortization amortization = null; amortization = AmortizationFactory.create(212000, 7.25, 30, AmortizationFactory.YEARLY); System.out.println(amortization); amortization = AmortizationFactory.create(212000, 7.25, 30, AmortizationFactory.MONTHLY); System.out.println(amortization);
}
private void setDebt(double debt)
{this.debt = debt;
}private void setRate(double rate)
{this.rate = rate;
}private void setPeriods(int periods) {
this.periods = periods;
}private void setPayment()
{ double amortizationFactor = (1 - Math.pow(1 + rate, -1 * periods)) / rate; payment = debt / amortizationFactor;}
public void setTotalPayment()
{totalPayment = payment * periods; }
public void setTotalInterest()
{ if (totalPayment == -1) setTotalPayment(); totalInterest = totalPayment-debt;}
private void setSchedule()
{ principalPaid = new double[periods+1]; interestPaid = new double[periods+1]; cumulativePrincipal = new double[periods+1]; cumulativeInterest = new double[periods+1]; cumulativePayment = new double[periods+1]; principalBalance = new double[periods+1]; if (payment == -1) setPayment(); principalPaid[0] = 0; interestPaid[0] = 0; cumulativePrincipal[0] = 0; cumulativeInterest[0] = 0; cumulativePayment[0] = 0; principalBalance[0] = debt; for (int period = 1; period <= periods; period++) { interestPaid[period] = principalBalance[period-1] * rate; principalPaid[period] = payment - interestPaid[period]; cumulativePrincipal[period] = cumulativePrincipal[period-1] + principalPaid[period]; cumulativeInterest[period] = cumulativeInterest[period-1] + interestPaid[period]; cumulativePayment[period] = cumulativePayment[period-1] + payment; principalBalance[period] = principalBalance[period-1] - principalPaid[period]; } scheduleSet = true;}
}