I feel that my app could be more efficient but I am not sure what else I can do. I feel like I could be reusing more. If you could give me some tips on how I could improve this, I would really appreciate it.
public class Main {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
//creating a new instance of my Calc class
Calc calc = new Calc();
// setting the variables
calc.term = 30;
calc.interestRate = (float) 0.0575;
calc.principal = (float) 200000.00;
calc.monthlyPayment = 0;
//format currency correctly
DecimalFormat df = new java.text.DecimalFormat("$,###.00");
//Method call to calc class
calc.DoWork();
//out put
System.out.println("\n__________________________________________________________________________________________________________");
System.out.println("\nPrincipal Amount: " + df.format (calc.principal ));
System.out.println("interest rate: " + calc.interestRate);
System.out.println("Term of loan(number of years): " + calc.term);
System.out.println("Monthly payment is: " + df.format (calc.monthlyPayment));
System.out.println("__________________________________________________________________________________________________________\n");
Thread.sleep(600);
calc.DoWorkAmortization();
}
}
package com.mortgagecalc;
import java.text.DecimalFormat;
public class Calc {
int term; //how long the loan is
float interestRate; // loan interest rate
float principal; // loan amount
float monthlyPayment; // monthly payment
private int period = 12; // 12 months in a year
//Amortization Variables
int n = 360;
double i = 0.0575;
double a = 200000.00;
double r = (1+i/12);
DecimalFormat df = new java.text.DecimalFormat("$,###.00");
//Method to calculate the payment.
void DoWork() throws InterruptedException
{
//Payment calculation
monthlyPayment = (float) (principal * Math.pow(1 + interestRate / period, term * period) * (interestRate / period) / (Math.pow(1 + interestRate / period, term * period) - 1));
}//Method to calculate Amortization
void DoWorkAmortization() throws InterruptedException
{
int number = 1;
double monthlyPayments = a * ( r - 1 ) / ( 1 - Math.pow(r,-n));
System.out.println ("Month \t\t Payment \t\0\0\0\0 Interest Paid \t\0\0\0\0 Principal Paid \t\0\0\0\0 Remaining Balance");
System.out.println("__________________________________________________________________________________________________________");
for ( number = 1; number <= 360; number++)
{
double interest = a * ( i/12 );
double principal = monthlyPayments - interest ;
double balance = a - principal;
a = balance;
System.out.println ("\0\0"+ number + "\t\t" + df.format(monthlyPayments)+"\t\t" + df.format(interest)+"\t\t\t"+ df.format(principal)+ "\t\t\t" +df.format(balance));
if (number % 12 == 0){
Thread.sleep(600);
}
}
}
}
(float) 0.0575
, just0.0575F
. \$\endgroup\$