Ok, so I need to enhance the functionality of my existing Loan class to be able to calculate any of the 4 missing elements in a Loan, whether it is the Principal, APR, Term, or Payment. I already have the necessary calculations to get the monthly Payment, but I need help with the other 3.

I know finding APR requires a binary search.

The non-java formula for principal is something like this

Principal =           Payment
                   APR/12 * ( 1 + 1/(1 + APR/12)exponent Term - 1)


Term =      log ( 1 / (Payment/(APR/12 * Principal - 1)) + 1 
               log ( 1 + APR/12) 

They numbers on top are divided by the bottom. Here is my current code.

public class Loan {
  private double principal;
  private double APR;
  private int term;
  private double monthlyPayment;
  static DecimalFormat df = new DecimalFormat("0.##");

  public static void main(String[] args) throws Exception {
    Loan a = new Loan(30000, .04, 60, 0);    //Principal, APR, Term.      Calculate: Payment
    Loan b = new Loan(30000, .04, 0, 522.5); //Principal, APR, Payment.   Calculate: Term
    Loan c = new Loan(0, .04, 60, 522.5);    //APR, Payment, Term.        Calculate: Principal
    Loan d = new Loan(30000, 0, 60, 522.5);   //Principal, Term, Payment. Calculate: APR
    System.out.println(a.getMonthlyPayment());
  }     

  private Loan() { }
/*  
  public Loan(double a, double b, int c) {
    setPrincipal(a);
    setAPR(b);
    setTerm(c); 
    setMonthlyPayment(calculatePayment(a, b, c));
  }
*/

//            Principal    APR       Term   Payment   
  public Loan(double a,    double b, int c, double d) {
    if (a == 0) {
      //Calculate: Principal
    }
    else if (b == 0) {
      //Calculate: APR   
    }
    else if (c == 0) {
      //Calculate: Term   
    }
    else if (d == 0) {
      //Calculate: Payment   
      setPrincipal(a);
      setAPR(b);
      setTerm(c); 
      setMonthlyPayment(calculatePayment(a, b, c));
    }
    else {
      //Verify input   
    }
  }

  public void setPrincipal(double a) {
    principal = a;
  }

  public void setAPR(double b) {
    APR = b;
  }

  public void setTerm(int c) {
    term = c;
  }

  public void setMonthlyPayment(double mp) {
    monthlyPayment = mp;   
  }

  public double getPrincipal() {
    return principal;
  }

  public double getAPR() {
    return APR;
  }

  public int getTerm() {
    return term;
  }

  public double getMonthlyPayment() {
    return monthlyPayment;   
  }

  public static double calculatePayment(double principal, double APR, int term) {
    double monthlyInterestRate = APR / 12;
    double monthlyPayment = (monthlyInterestRate * principal) * (1 + (1 / (Math.pow(1 + monthlyInterestRate, term) - 1)));
    return monthlyPayment;
  }

  public static void amortize(double principal, double APR, int term, double monthlyPayment) {
    double monthlyInterestRate = APR / 12;
    double totalPayment = monthlyPayment * term;
    int payment = 1;
    double balance = principal;

    //print information
    System.out.println("Loan Amount: $" + principal);
    System.out.println("Number of Payments: " + term);
    System.out.println("Interest Rate: " + APR + "%");
    System.out.println("Monthly Payment: $" + df.format(monthlyPayment));
    System.out.println("Total Payment: $" + df.format(totalPayment));
    System.out.println();
    System.out.println("Payment#\tInterest\tPrincipal\tBalance"); 

     do {
       double monthlyInterest = monthlyInterestRate * balance;
       principal = monthlyPayment - monthlyInterest;
       balance -= principal;

       //Show Amortization Schedule
       System.out.print(df.format(payment));
       System.out.print("\t        ");
       System.out.print(df.format(monthlyInterest));
       System.out.print("\t        ");
       System.out.print(df.format(principal));
       System.out.print("\t        ");
       System.out.print(df.format(balance));
       System.out.println();
       payment++;  
    }    
    while (payment <= term);
  }
}

Edited 3 Years Ago by pyTony: fixed formating

Ok, I got all of the algorithms in, so now I just need to figure out how to do the binary search if APR is not known. Any ideas?

Thank you, for the coding..it really help me
to see how it should be.

you have posted one time on this forum, and that is just to re-open a thread that was solved over four years ago???

Comments
Don't try to understand it, just enjoy the ride.
This article has been dead for over six months. Start a new discussion instead.