Advertisement
Nick-O-Rama

Amortization

Mar 27th, 2015
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.text.DecimalFormat;
  4.  
  5. public class Amortization {
  6.  
  7.     private double loanAmount;
  8.     private double interestRate;
  9.     private double loanBalance;
  10.     private double term;
  11.     private double payment;
  12.     private int loanYears;
  13.  
  14.     public Amortization(double loan, double rate, int years) {
  15.         this.loanAmount = loan;
  16.         this.loanBalance = loan;
  17.         this.interestRate = rate;
  18.         this.loanYears = years;
  19.  
  20.         calcPayment();
  21.     }
  22.  
  23.     private void calcPayment() {
  24.         this.term = Math.pow(interestRate / 12 + 1, loanYears * 12);
  25.         this.payment = (loanAmount * (interestRate / 12.0) * term) / (term - 1);
  26.     }
  27.  
  28.     public int getNumberOfPayments() {
  29.         return (loanYears * 12);
  30.     }
  31.  
  32.     public double getLoanAmount() {
  33.         return loanAmount;
  34.     }
  35.  
  36.     public double getInterestRate() {
  37.         return interestRate;
  38.     }
  39.  
  40.     public int getLoanYears() {
  41.         return loanYears;
  42.     }
  43.  
  44.     public void saveReport(String filename) throws FileNotFoundException {
  45.         DecimalFormat df = new DecimalFormat("0.00");
  46.         File file = new File(filename);
  47.         PrintWriter pw = new PrintWriter(file);
  48.         pw.println("Monthly Payment: $" + df.format(payment));
  49.         pw.println("Month\tInterest\tPrincipal\tBalance");
  50.         pw.println("-----------------------------------------------");
  51.         for (int i = 0; i < getNumberOfPayments(); i++) {
  52.             double interest = interestRate / 12.0
  53.                     * loanBalance;
  54.             double principal = payment - interest;
  55.             loanBalance -= principal;
  56.             pw.println((i + 1) + "\t" + df.format(interest) + "\t\t"
  57.                     + df.format(principal) + "\t\t" + df.format(loanBalance));
  58.         }
  59.  
  60.         pw.close();
  61.  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement