|
1 | | -import java.text.NumberFormat; |
2 | 1 | import java.util.Scanner; |
3 | | -public class FutureValueApp { |
4 | | - public static void main(String[] args) { |
5 | | - System.out.println("Welcome to the Future Value App\n"); |
6 | | - Scanner sc=new Scanner(System.in); |
7 | | - String choice="y"; |
8 | | - while (choice.equalsIgnoreCase("y")){ |
9 | | - //get the input from the user |
10 | | - System.out.print("Enter monthly investment: "); |
11 | | - double monthlyInvestment =sc.nextDouble(); |
12 | | - System.out.print("Enter yearly investment rate: "); |
13 | | - double interestRate =sc.nextDouble(); |
14 | | - System.out.print("Enter number of years: "); |
15 | | - int years=sc.nextInt(); |
16 | | - |
17 | | - //convert yearly values to monthly values |
18 | | - double monthlyInterestRate=interestRate/12/100; |
19 | | - int months=years/12; |
20 | | - |
21 | | - //call future value method |
22 | | - |
23 | | - double futureValue =calaulateFutureValue(monthlyInvestment,monthlyInterestRate,months); |
| 2 | +import java.text.NumberFormat; |
24 | 3 |
|
25 | | - //format and display the result |
| 4 | +public class FutureValueApp { |
26 | 5 |
|
| 6 | + public static void main(String[] args) { |
| 7 | + System.out.println("The Future Value Calculator\n"); |
| 8 | + |
| 9 | + @SuppressWarnings("resource") |
| 10 | + Scanner sc = new Scanner(System.in); |
| 11 | + String choice = "y"; |
| 12 | + while (choice.equalsIgnoreCase("y")) { |
| 13 | + // get the input from the user |
| 14 | + System.out.print("Enter monthly investment: "); |
| 15 | + double monthlyInvestment = sc.nextDouble(); |
| 16 | + System.out.print("Enter yearly interest rate: "); |
| 17 | + double interestRate = sc.nextDouble(); |
| 18 | + System.out.print("Enter number of years: "); |
| 19 | + int years = sc.nextInt(); |
| 20 | + |
| 21 | + // convert yearly values to monthly values |
| 22 | + double monthlyInterestRate = interestRate / 12 / 100; |
| 23 | + int months = years * 12; |
| 24 | + |
| 25 | + // use a for loop to calculate the future value |
| 26 | + double futureValue = 0.0; |
| 27 | + for (int i = 1; i <= months; i++) { |
| 28 | + futureValue = (futureValue + monthlyInvestment) * |
| 29 | + (1 + monthlyInterestRate); |
| 30 | + System.out.println("Month " + i + ": " + futureValue); |
| 31 | + } |
| 32 | + |
| 33 | + // format the result and display it to the user |
27 | 34 | NumberFormat currency = NumberFormat.getCurrencyInstance(); |
28 | | - System.out.println("Future value: "+ |
29 | | - currency.format(futureValue)); |
| 35 | + System.out.println("Future value: " |
| 36 | + + currency.format(futureValue)); |
30 | 37 | System.out.println(); |
31 | 38 |
|
32 | | - //see if the user wants to continue |
| 39 | + // see if the user wants to continue |
33 | 40 | System.out.print("Continue? (y/n): "); |
34 | | - choice=sc.next(); |
35 | | - |
| 41 | + choice = sc.next(); |
36 | 42 | System.out.println(); |
37 | | - |
38 | | - |
39 | | - } |
40 | | - } |
41 | | - |
42 | | - //static method that requires three arguments and return a double |
43 | | - public static double calaulateFutureValue(double monthlyInvestment, |
44 | | - double monthlyInterestRate, int months){ |
45 | | - double futureValue=0.0; |
46 | | - for (int i = 0; i <months ; i++) { |
47 | | - futureValue=(futureValue+monthlyInvestment)*(1+monthlyInterestRate); |
48 | | - |
49 | 43 | } |
50 | | - return futureValue; |
| 44 | + System.out.println("Bye!"); |
51 | 45 | } |
52 | 46 | } |
0 commit comments