Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.text.NumberFormat;
- import java.util.Locale;
- public class Amortization {
- private double interestRate, loanBalance;
- private int loanYears;
- private static NumberFormat nf =
- NumberFormat.getCurrencyInstance(new Locale("en", "US"));
- public Amortization(double loanBalance, double interestRate, int loanYears) {
- this.loanBalance = loanBalance;
- this.interestRate = interestRate;
- this.loanYears = loanYears;
- // DO NOT CALL CALCPAYMENT HERE!!!!!!!
- }
- private double getMonthlyPayment() {
- double loanBalance = 20000;
- double calcRate = (7.5/100) / 12;
- int calcYears = 5 * 12;
- double numerator = calcRate * Math.pow((1+calcRate), calcYears);
- double denominator = Math.pow((1+calcRate),calcYears) - 1;
- return loanBalance * (numerator/denominator);
- }
- public int getNumberOfPayments() {
- return 12 * loanYears;
- }
- private void saveReport(String filename) throws IOException {
- double payment = this.getMonthlyPayment();
- double monthlyInterest, principal;
- FileWriter writer = new FileWriter("Amoritization.txt");
- PrintWriter report = new PrintWriter(writer);
- report.println("Monthy Payment: $" + nf.format(payment));
- report.println("Month\tInterest\tPrincipal\tBalance");
- report.println("-------------------------------------------------");
- for (int month = 1; month <= getNumberOfPayments(); month++) {
- monthlyInterest = interestRate / 12.0 * loanBalance;
- if (month != getNumberOfPayments()) {
- principal = payment - monthlyInterest;
- } else {
- principal = loanBalance;
- payment = loanBalance + monthlyInterest;
- }
- loanBalance -= principal;
- report.println(month + "\t" + nf.format(monthlyInterest) + "\t\t"
- + nf.format(principal) + "\t\t" + nf.format(loanBalance));
- }
- report.close();
- }
- public double getLoanBalance() {
- return loanBalance;
- }
- public double getInterestRate() {
- return interestRate;
- }
- public int getLoanYears() {
- return loanYears;
- }
- @Override
- public String toString() {
- return "Loan Amount: " + nf.format(loanBalance) +
- "\nInterest Rate: " + interestRate +
- "\nLoan Years: " + loanYears +
- "\nMonthly Payments: " + nf.format(getMonthlyPayment());
- }
- public static void main(String[] args) {
- double loanAmount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount of " +
- "the Loan:"));
- double interestRate = Double.parseDouble(JOptionPane.showInputDialog("Enter the annual " +
- "interest rate of the loan:"));
- int loanYears = Integer.parseInt(JOptionPane.showInputDialog("Enter the length of the " +
- "loan, in years:"));
- Amortization a = new Amortization(loanAmount, interestRate, loanYears);
- JOptionPane.showMessageDialog(null,a,"Your Results",1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment