-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mortgage_Calculator.java
51 lines (43 loc) · 1.74 KB
/
Mortgage_Calculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.text.NumberFormat;
import java.util.Scanner;
public class Mortgage_Calculator {
public static void main(String[] args) {
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
int principal = 0;
float annualInterestRate = 0;
int numberOfPayments = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Principal: ");
principal = scanner.nextInt();
if (principal >= 1000 && principal <= 1_000_000_000) {
break;
} else {
System.out.println("Enter a value between 1000 and 1000000000");
}
}
while (true) {
System.out.print("Annual Interest Rate: ");
annualInterestRate = scanner.nextFloat();
if (annualInterestRate >= 1 && annualInterestRate <= 30) {
break;
}
System.out.println("Enter a value between 1 and 30.");
}
float monthlyInterestRate = annualInterestRate / PERCENT / MONTHS_IN_YEAR;
while (true) {
System.out.print("Period (Years): ");
byte years = scanner.nextByte();
if (years >= 1 && years <= 30) {
numberOfPayments = years * MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 and 30.");
}
double mortgage = principal * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments))
/ (Math.pow(1 + monthlyInterestRate, numberOfPayments) - 1);
String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.println("Mortgage: " + mortgageFormatted);
}
}