-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWisdomCompoundCalculator.java
More file actions
57 lines (36 loc) · 2.21 KB
/
WisdomCompoundCalculator.java
File metadata and controls
57 lines (36 loc) · 2.21 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
import java.util.Scanner;
public class CompoundInterestCalculator2 {
public static void main(String[] args) {
Scanner wisdom = new Scanner(System.in);
System.out.println("Enter the initial investment amount:");
double initialInvestment = wisdom.nextDouble();
System.out.println("Enter the monthly contribution:");
double monthlyContribution = wisdom.nextDouble();
System.out.println("Enter the number of years:");
int years = wisdom.nextInt();
System.out.println("Enter the annual interest rate (in %):");
double interestRate = wisdom.nextDouble();
System.out.println("Enter the interest rate variance (in %):");
double rateVariance = wisdom.nextDouble();
System.out.println("Enter the number of times interest is compounded per year:");
int compoundFrequency = wisdom.nextInt();
double averageResult = calculateCompoundInterest(initialInvestment, monthlyContribution, years, interestRate, compoundFrequency);
double bestCaseResult = calculateCompoundInterest(initialInvestment, monthlyContribution, years, interestRate + rateVariance, compoundFrequency);
double worstCaseResult = calculateCompoundInterest(initialInvestment, monthlyContribution, years, interestRate - rateVariance, compoundFrequency);
System.out.println("\nResults:");
System.out.println("Total Investment Without Interest: $" + (initialInvestment + monthlyContribution * years * 12));
System.out.println("Average Scenario: $" + averageResult);
System.out.println("Best Case Scenario: $" + bestCaseResult);
System.out.println("Worst Case Scenario: $" + worstCaseResult);
}
public static double calculateCompoundInterest(double startAmount, double monthlyAdd, int totalYears, double annualRate, int frequency) {
double rate = annualRate / 100;
double totalAmount = startAmount;
int months = totalYears * 12;
for (int i = 0; i < months; i++) {
totalAmount += monthlyAdd;
totalAmount += (totalAmount * (rate / frequency));
}
return Math.round(totalAmount * 100.0) / 100.0;
}
}