Java Loan Application

Reply

Join Date: Mar 2004
Posts: 4
Reputation: jjskywlker is an unknown quantity at this point 
Solved Threads: 0
jjskywlker jjskywlker is offline Offline
Newbie Poster

Java Loan Application

 
0
  #1
Mar 23rd, 2004
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);
}
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 4
Reputation: jjskywlker is an unknown quantity at this point 
Solved Threads: 0
jjskywlker jjskywlker is offline Offline
Newbie Poster

Re: Java Loan Application

 
0
  #2
Mar 23rd, 2004
Got the calculate Principal covered.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 4
Reputation: jjskywlker is an unknown quantity at this point 
Solved Threads: 0
jjskywlker jjskywlker is offline Offline
Newbie Poster

Re: Java Loan Application

 
0
  #3
Mar 23rd, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Java Loan Application

 
0
  #4
Mar 24th, 2004
Here's some info on the binary search.

http://www.tbray.org/ongoing/When/20...3/03/22/Binary
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 4
Reputation: jjskywlker is an unknown quantity at this point 
Solved Threads: 0
jjskywlker jjskywlker is offline Offline
Newbie Poster

Re: Java Loan Application

 
0
  #5
Mar 24th, 2004
Got it all figured out. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: boyjustlearn26 is an unknown quantity at this point 
Solved Threads: 0
boyjustlearn26 boyjustlearn26 is offline Offline
Newbie Poster

Re: Java Loan Application

 
0
  #6
Oct 25th, 2008
Thank you, for the coding..it really help me
to see how it should be.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Java Loan Application

 
1
  #7
Oct 26th, 2008
Originally Posted by boyjustlearn26 View Post
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???
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC