Soulpieced
03-24-2004, 04:15 PM
Did the expansion to this project in < 2 hours. Take that java!
.
import java.text.DecimalFormat;
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, 552.4956616579792); //Principal, APR, Payment. Calculate: Term
Loan c = new Loan(0, .04, 60, 552.4956616579792); //APR, Payment, Term. Calculate: Principal
Loan d = new Loan(30000, 0, 60, 552.4956616579792); //Principal, Term, Payment. Calculate: APR
Loan e = new Loan(30000, .05, 60, 552.4956616579792); //All Available. Calculate: Validate
Loan f = new Loan(30000, .04, 60, 552.4956616579792); //All Available. Calculate: Validate
System.out.println(df.format(a.getMonthlyPayment() ));
System.out.println(b.getTerm());
System.out.println(c.getPrincipal());
System.out.println(df.format(d.getAPR()));
}
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
setPrincipal(calculatePrincipal(b, c, d));
setAPR(b);
setTerm(c);
setMonthlyPayment(d);
}
else if (b == 0) {
//Calculate: APR
setPrincipal(a);
setAPR(calculateAPR(a, c, d));
setTerm(c);
setMonthlyPayment(d);
}
else if (c == 0) {
//Calculate: Term
setPrincipal(a);
setAPR(b);
setTerm(calculateTerm(a, b, d));
setMonthlyPayment(d);
}
else if (d == 0) {
//Calculate: Payment
setPrincipal(a);
setAPR(b);
setTerm(c);
setMonthlyPayment(calculatePayment(a, b, c));
}
else {
//Verify input
setPrincipal(a);
setAPR(b);
setTerm(c);
setMonthlyPayment(d);
if (calculatePayment(a, b, c) != getMonthlyPayment()) System.out.println("ERROR, your input numbers do not validate.");
else System.out.println("Your loan application was processed correctly!");
}
}
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;
}
//Calculate the APR with all other values known
public static double calculateAPR(double principal, int term, double monthlyPayment) {
double first = .00001;
double last = .25;
double middle;
while (last > first) {
middle = ((first + last) / 2);
if (calculatePayment(principal, middle, term) == monthlyPayment) return middle;
if (calculatePayment(principal, middle, term) < monthlyPayment) first = middle;
else last = middle;
}
return 0;
}
//Calculate the Payment with all other values known
public static double calculatePayment(double principal, double APR, int term) {
double monthlyIR = APR / 12;
double monthlyPayment = (monthlyIR * principal) * (1 + (1 / (Math.pow(1 + monthlyIR, term) - 1)));
return monthlyPayment;
}
//Calculate the Principal with all other values known
public static double calculatePrincipal(double APR, int term, double monthlyPayment) {
double monthlyIR = APR / 12;
double newPrincipal = monthlyPayment / (monthlyIR * (1 + (1 / (Math.pow(1 + monthlyIR, term) - 1))));
return newPrincipal;
}
//Calculate the Term with all other values known
public static int calculateTerm(double principal, double APR, double monthlyPayment) {
double monthlyIR = APR / 12;
String term = Math.round(Math.log(1 / (monthlyPayment / (monthlyIR * principal) - 1) + 1) /
Math.log(1 + monthlyIR)) + "";
int newTerm = Integer.parseInt(term);
return newTerm;
}
//Print out an amortization schedule based on the loan
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 on 3-24-2004 by Soulpieced]
.
import java.text.DecimalFormat;
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, 552.4956616579792); //Principal, APR, Payment. Calculate: Term
Loan c = new Loan(0, .04, 60, 552.4956616579792); //APR, Payment, Term. Calculate: Principal
Loan d = new Loan(30000, 0, 60, 552.4956616579792); //Principal, Term, Payment. Calculate: APR
Loan e = new Loan(30000, .05, 60, 552.4956616579792); //All Available. Calculate: Validate
Loan f = new Loan(30000, .04, 60, 552.4956616579792); //All Available. Calculate: Validate
System.out.println(df.format(a.getMonthlyPayment() ));
System.out.println(b.getTerm());
System.out.println(c.getPrincipal());
System.out.println(df.format(d.getAPR()));
}
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
setPrincipal(calculatePrincipal(b, c, d));
setAPR(b);
setTerm(c);
setMonthlyPayment(d);
}
else if (b == 0) {
//Calculate: APR
setPrincipal(a);
setAPR(calculateAPR(a, c, d));
setTerm(c);
setMonthlyPayment(d);
}
else if (c == 0) {
//Calculate: Term
setPrincipal(a);
setAPR(b);
setTerm(calculateTerm(a, b, d));
setMonthlyPayment(d);
}
else if (d == 0) {
//Calculate: Payment
setPrincipal(a);
setAPR(b);
setTerm(c);
setMonthlyPayment(calculatePayment(a, b, c));
}
else {
//Verify input
setPrincipal(a);
setAPR(b);
setTerm(c);
setMonthlyPayment(d);
if (calculatePayment(a, b, c) != getMonthlyPayment()) System.out.println("ERROR, your input numbers do not validate.");
else System.out.println("Your loan application was processed correctly!");
}
}
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;
}
//Calculate the APR with all other values known
public static double calculateAPR(double principal, int term, double monthlyPayment) {
double first = .00001;
double last = .25;
double middle;
while (last > first) {
middle = ((first + last) / 2);
if (calculatePayment(principal, middle, term) == monthlyPayment) return middle;
if (calculatePayment(principal, middle, term) < monthlyPayment) first = middle;
else last = middle;
}
return 0;
}
//Calculate the Payment with all other values known
public static double calculatePayment(double principal, double APR, int term) {
double monthlyIR = APR / 12;
double monthlyPayment = (monthlyIR * principal) * (1 + (1 / (Math.pow(1 + monthlyIR, term) - 1)));
return monthlyPayment;
}
//Calculate the Principal with all other values known
public static double calculatePrincipal(double APR, int term, double monthlyPayment) {
double monthlyIR = APR / 12;
double newPrincipal = monthlyPayment / (monthlyIR * (1 + (1 / (Math.pow(1 + monthlyIR, term) - 1))));
return newPrincipal;
}
//Calculate the Term with all other values known
public static int calculateTerm(double principal, double APR, double monthlyPayment) {
double monthlyIR = APR / 12;
String term = Math.round(Math.log(1 / (monthlyPayment / (monthlyIR * principal) - 1) + 1) /
Math.log(1 + monthlyIR)) + "";
int newTerm = Integer.parseInt(term);
return newTerm;
}
//Print out an amortization schedule based on the loan
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 on 3-24-2004 by Soulpieced]