4
4
import java .util .Scanner ;
5
5
6
6
public class Main {
7
+ final static byte MONTHS_IN_YEAR = 12 ;
8
+ final static byte PERCENT = 100 ;
7
9
8
10
public static void main (String [] args ) {
9
11
int principal = (int ) readNumber ("Principal ($1K - $1M): " , 1000 , 1_000_000 );
10
12
float annualInterest = (float ) readNumber ("Annual Interest Rate (1% - 30%): " , 1 , 30 );
11
13
byte years = (byte ) readNumber ("Period (1 Years- 30 Years): " , 1 , 30 );
12
14
15
+ printMortgage (principal , annualInterest , years );
16
+
17
+ printPaymentSchedule (principal , annualInterest , years );
18
+ }
19
+
20
+ private static void printPaymentSchedule (int principal , float annualInterest , byte years ) {
21
+ System .out .println ();
22
+ System .out .println ("PAYMENTS SCHEDULE:" );
23
+ System .out .println ("------------------" );
24
+ for (short month = 1 ; month <= years * MONTHS_IN_YEAR ; month ++) {
25
+ double balance = calculateBalance (principal , annualInterest , years , month );
26
+ System .out .println (NumberFormat .getCurrencyInstance ().format (balance ));
27
+ }
28
+ }
29
+
30
+ private static void printMortgage (int principal , float annualInterest , byte years ) {
31
+ System .out .println ();
32
+ System .out .println ("MORTGAGE" );
33
+ System .out .println ("--------" );
13
34
double mortgage = calculateMortgage (principal , annualInterest , years );
14
- System .out .println ("Mortgage : " + NumberFormat .getCurrencyInstance ().format (mortgage ));
35
+ System .out .println ("Monthly Payments : " + NumberFormat .getCurrencyInstance ().format (mortgage ));
15
36
}
16
37
17
38
public static double readNumber (String prompt , double min , double max ) {
@@ -28,10 +49,16 @@ public static double readNumber(String prompt, double min, double max) {
28
49
return value ;
29
50
}
30
51
31
- public static double calculateMortgage (int principal , float annualInterest , byte years ) {
32
- final byte MONTHS_IN_YEAR = 12 ;
33
- final byte PERCENT = 100 ;
52
+ public static double calculateBalance (int principal , float annualInterest , byte years , short numberOfPaymentsMade ) {
53
+ float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR ;
54
+ short numberOfPayments = (short ) (years * MONTHS_IN_YEAR );
55
+
56
+ double balance = principal * (Math .pow (1 + monthlyInterest , numberOfPayments ) - Math .pow (1 + monthlyInterest , numberOfPaymentsMade )) / (Math .pow (1 + monthlyInterest , numberOfPayments ) - 1 );
34
57
58
+ return balance ;
59
+ }
60
+
61
+ public static double calculateMortgage (int principal , float annualInterest , byte years ) {
35
62
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR ;
36
63
short numberOfPayments = (short ) (years * MONTHS_IN_YEAR );
37
64
0 commit comments