-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureInvestmentCalculator.java
More file actions
78 lines (64 loc) · 2.59 KB
/
FutureInvestmentCalculator.java
File metadata and controls
78 lines (64 loc) · 2.59 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
77
78
import java.text.DecimalFormat;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FutureInvestmentCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.00");
double investmentAmount = 0.0;
double annualInterestRate = 0.0;
int numberOfYears = 0;
// Input: Investment Amount
while (true) {
try {
System.out.print("Enter investment amount: ");
investmentAmount = input.nextDouble();
if (investmentAmount <= 0) {
System.out.println("Please enter a positive number.");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
input.next();
}
}
// Input: Annual Interest Rate
while (true) {
try {
System.out.print("Enter annual interest rate: ");
annualInterestRate = input.nextDouble();
if (annualInterestRate <= 0) {
System.out.println("Please enter a positive number.");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
input.next();
}
}
// Input: Number of Years
while (true) {
try {
System.out.print("Enter number of years: ");
numberOfYears = input.nextInt();
if (numberOfYears <= 0) {
System.out.println("Please enter a positive number.");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
input.next();
}
}
// Calculating Future Investment Value
double monthlyInterestRate = annualInterestRate / 100 / 12;
double futureInvestmentValue = investmentAmount * Math.pow(1 + monthlyInterestRate, numberOfYears * 12);
// Output
System.out.println("Accumulated value is " + df.format(futureInvestmentValue));
// Close the Scanner
input.close();
}
}