-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoanRepaymentCalculator.java
More file actions
76 lines (42 loc) · 2.36 KB
/
LoanRepaymentCalculator.java
File metadata and controls
76 lines (42 loc) · 2.36 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
69
70
71
72
73
74
75
76
/* SELF ASSESSMENT of whether my code is easy to understand.
1. Did I use appropriate CONSTANTS instead of numbers within the code? yes
Mark out of 5:
Comment:
2. Did I use easy-to-understand, meaningful CONSTANT names? yes
Mark out of 5:
Comment:
3. Did I format the CONSTANT names properly (in UPPERCASE)?yes
Mark out of 5:
Comment:
4. Did I use easy-to-understand meaningful variable names? yes
Mark out of 10:
Comment:
5. Did I format the variable names properly (in lowerCamelCase)?yes
Mark out of 5:
Comment:
6. Did I indent the code appropriately? yes
Mark out of 10:
Comment:
Total Mark out of 40 (Add all the previous marks):
*/
import java.util.Scanner;
public class LoanRepaymentCalculator {
public static void main(String[] args) {
System.out.print("Enter loan amount? "); // loan amount
Scanner input = new Scanner( System.in ) ;
double loanAmount = input.nextDouble();
System.out.print("Enter annual interest rate? (e.g. 0.04) "); // interest rate
double annualInterestRate = input.nextDouble();
System.out.print("Enter the term of the loan in years? "); // loan in years
final int MONTHS_IN_YEAR =12; // making a CONSTANT
int loanInYears = input.nextInt();
input.close(); // closing the input
int loanInMonths = loanInYears*MONTHS_IN_YEAR ; // converting formula years to months
double monthlyInterestRate = (annualInterestRate/MONTHS_IN_YEAR); // interest rate conversion
double monthlyRepayment = loanAmount * ((monthlyInterestRate * (Math.pow(1 + monthlyInterestRate , loanInMonths)))
/ ((Math.pow(1 + monthlyInterestRate , loanInMonths)) -1 )); // formula for the loan
System.out.printf("The monthly repayment for a %d year loan of %.2f at an annual interest rate of %.2f would be %.2f "
, loanInYears, loanAmount, annualInterestRate, monthlyRepayment); // the output
}
}
// finally done :)