-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
68 lines (56 loc) · 2.54 KB
/
Main.java
File metadata and controls
68 lines (56 loc) · 2.54 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
Scanner scanner = new Scanner(System.in);
int principal = getPrincipal(scanner);
float monthlyInterest = getMonthlyInterest(scanner, PERCENT, MONTHS_IN_YEAR);
int numberOfPayments = getNumberOfPayments(scanner, MONTHS_IN_YEAR);
double mortgage = getMortgage(monthlyInterest, numberOfPayments, principal);
double totalPayment = mortgage * numberOfPayments;
System.out.println("Mortgage: " + NumberFormat.getCurrencyInstance().format(mortgage));
System.out.println("Total Payment: " + NumberFormat.getCurrencyInstance().format(totalPayment));
}
public static int getPrincipal(Scanner scanner) {
int principal;
Currency currency = NumberFormat.getCurrencyInstance().getCurrency();
while (true) {
System.out.print("Principal ("+currency+" 1K - "+currency+" 1M): ");
principal = scanner.nextInt();
if (principal >= 1_000 && principal <= 1_000_000)
break;
System.out.println("Enter a value between 1,000 and 1,000,000.");
}
return principal;
}
public static float getMonthlyInterest(Scanner scanner, final byte PERCENT, final byte MONTHS_IN_YEAR) {
float annualInterest;
while (true) {
System.out.print("Annual Interest Rate: ");
annualInterest = scanner.nextFloat();
if (annualInterest >= 1 && annualInterest <= 30)
break;
System.out.println("Enter a value greater than 0 and less than or equal to 30.");
}
return annualInterest / PERCENT / MONTHS_IN_YEAR;
}
public static int getNumberOfPayments(Scanner scanner, final byte MONTHS_IN_YEAR) {
byte years;
while (true) {
System.out.print("Period (Years): ");
years = scanner.nextByte();
if (years >= 1 && years <= 30)
break;
System.out.println("Enter a value between 1 and 30.");
}
return years * MONTHS_IN_YEAR;
}
public static double getMortgage(float monthlyInterest, int numberOfPayments, int principal) {
double part1 = monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments);
double part2 = Math.pow(1 + monthlyInterest, numberOfPayments) - 1;
return principal * (part1 / part2);
}
}