binibiningtinamoran

Amortization.java

Dec 4th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.text.NumberFormat;
  6. import java.util.Locale;
  7.  
  8. public class Amortization {
  9.  
  10.     private double interestRate, loanBalance;
  11.     private int loanYears;
  12.  
  13.     private static NumberFormat nf =
  14.             NumberFormat.getCurrencyInstance(new Locale("en", "US"));
  15.  
  16.     public Amortization(double loanBalance, double interestRate, int loanYears) {
  17.         this.loanBalance = loanBalance;
  18.         this.interestRate = interestRate;
  19.         this.loanYears = loanYears;
  20.         // DO NOT CALL CALCPAYMENT HERE!!!!!!!
  21.     }
  22.  
  23.     private double getMonthlyPayment() {
  24.         double loanBalance = 20000;
  25.         double calcRate = (7.5/100) / 12;
  26.         int calcYears = 5 * 12;
  27.  
  28.         double numerator = calcRate * Math.pow((1+calcRate), calcYears);
  29.         double denominator = Math.pow((1+calcRate),calcYears) - 1;
  30.         return loanBalance * (numerator/denominator);
  31.     }
  32.  
  33.     public int getNumberOfPayments() {
  34.         return 12 * loanYears;
  35.     }
  36.  
  37.     private void saveReport(String filename) throws IOException {
  38.         double payment = this.getMonthlyPayment();
  39.         double monthlyInterest, principal;
  40.         FileWriter writer = new FileWriter("Amoritization.txt");
  41.         PrintWriter report = new PrintWriter(writer);
  42.  
  43.         report.println("Monthy Payment: $" + nf.format(payment));
  44.         report.println("Month\tInterest\tPrincipal\tBalance");
  45.         report.println("-------------------------------------------------");
  46.         for (int month = 1; month <= getNumberOfPayments(); month++) {
  47.             monthlyInterest = interestRate / 12.0 * loanBalance;
  48.  
  49.             if (month != getNumberOfPayments()) {
  50.                 principal = payment - monthlyInterest;
  51.             } else {
  52.                 principal = loanBalance;
  53.                 payment = loanBalance + monthlyInterest;
  54.             }
  55.             loanBalance -= principal;
  56.  
  57.             report.println(month + "\t" + nf.format(monthlyInterest) + "\t\t"
  58.                     + nf.format(principal) + "\t\t" + nf.format(loanBalance));
  59.         }
  60.         report.close();
  61.     }
  62.  
  63.     public double getLoanBalance() {
  64.         return loanBalance;
  65.     }
  66.  
  67.     public double getInterestRate() {
  68.         return interestRate;
  69.     }
  70.  
  71.     public int getLoanYears() {
  72.         return loanYears;
  73.     }
  74.  
  75.     @Override
  76.     public String toString() {
  77.         return "Loan Amount: " + nf.format(loanBalance) +
  78.                 "\nInterest Rate: " + interestRate +
  79.                 "\nLoan Years: " + loanYears +
  80.                 "\nMonthly Payments: " + nf.format(getMonthlyPayment());
  81.     }
  82.  
  83.     public static void main(String[] args) {
  84.  
  85.         double loanAmount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount of " +
  86.                 "the Loan:"));
  87.         double interestRate = Double.parseDouble(JOptionPane.showInputDialog("Enter the annual " +
  88.                 "interest rate of the loan:"));
  89.         int loanYears = Integer.parseInt(JOptionPane.showInputDialog("Enter the length of the " +
  90.                 "loan, in years:"));
  91.         Amortization a = new Amortization(loanAmount, interestRate, loanYears);
  92.         JOptionPane.showMessageDialog(null,a,"Your Results",1);
  93.  
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment