Created
June 15, 2013 11:32
-
-
Save anonymous/5787834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* sets up the calculator | |
*/ | |
public class LoanCalculator { | |
public static void main(String[] args) { | |
Scanner console=new Scanner(System.in); | |
System.out.print("What is the annual percentage rate (APR)? "); | |
double apr=console.nextDouble(); | |
System.out.print("What is the loan term in years? "); | |
double loanTerm=console.nextDouble(); | |
System.out.print("What is the loan amount in whole dollars? "); | |
double amount=console.nextDouble(); | |
double monthlyPayment= calculatePayment( apr, loanTerm, amount); | |
System.out.printf("the monthly payment is : %.2f\n", monthlyPayment); | |
producePaymentSchedule(monthlyPayment, apr, loanTerm, amount); | |
} | |
/** | |
* Calculates the monthly payment. | |
* @return monthly payment | |
*/ | |
public static double calculatePayment(double apr, double loanTerm, double amount) { | |
double n = 12 * loanTerm; | |
double i= apr/1200; | |
double monthlyPayment=(amount * i * Math.pow(1+i, n))/(Math.pow(1+i, n)-1); | |
return monthlyPayment; | |
} | |
/** | |
* Create Payment Schedule | |
* @param monthly payment, apr, loanTerm, and amount | |
*/ | |
public static void producePaymentSchedule(double monthlyPayment, double apr, double loanTerm, double amount) { | |
System.out.println(" Payment Schedule "); | |
System.out.printf(" num Payment Interest Principal Balance \n"); | |
double Balance=amount; | |
System.out.printf(" $%8.2f\n\n", Balance); | |
for(int i=1; i<=(loanTerm*12); i++) { | |
double payment=monthlyPayment; | |
double interest= (apr/1200)*Balance; | |
double principal=payment-interest; | |
Balance=Balance-principal; | |
System.out.printf("%4d $%6.2f $%6.2f $%6.2f $%8.2f\n", i, payment, interest, principal, Balance); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saw this code on the LinusTechTips forum for the challenge of a useful program under 100 lines of code: you could really nail it if you indented more efficiently