943,743 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 14180
  • Java RSS
Mar 23rd, 2004
0

Java Loan Application

Expand Post »
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);
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jjskywlker is offline Offline
4 posts
since Mar 2004
Mar 23rd, 2004
0

Re: Java Loan Application

Got the calculate Principal covered.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jjskywlker is offline Offline
4 posts
since Mar 2004
Mar 23rd, 2004
0

Re: Java Loan Application

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jjskywlker is offline Offline
4 posts
since Mar 2004
Mar 24th, 2004
0

Re: Java Loan Application

Here's some info on the binary search.

http://www.tbray.org/ongoing/When/20...3/03/22/Binary
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Mar 24th, 2004
0

Re: Java Loan Application

Got it all figured out. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jjskywlker is offline Offline
4 posts
since Mar 2004
Oct 25th, 2008
0

Re: Java Loan Application

Thank you, for the coding..it really help me
to see how it should be.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boyjustlearn26 is offline Offline
1 posts
since Oct 2008
Oct 26th, 2008
0

Re: Java Loan Application

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???
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Please Explain!
Next Thread in Java Forum Timeline: Simple 2D Arrays problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC