| | |
Java Loan Application
![]() |
•
•
Join Date: Mar 2004
Posts: 4
Reputation:
Solved Threads: 0
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);
}
}
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
- How to create an installable file of java web application (JSP)
- Starting Java web Application from exe (JSP)
- Need to build JAva Web Application (JSP)
- need help for java chat application (Java)
- XML in Java Mobile application (Java)
- help to set up "Sun Java™ System Application Server PE 8" for web application (Java)
Other Threads in the Java Forum
- Previous Thread: Please Explain!
- Next Thread: Simple 2D Arrays problem
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows working





