|
| 1 | +import java.text.NumberFormat; |
| 2 | +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); |
| 24 | + |
| 25 | + //format and display the result |
| 26 | + |
| 27 | + NumberFormat currency = NumberFormat.getCurrencyInstance(); |
| 28 | + System.out.println("Future value: "+ |
| 29 | + currency.format(futureValue)); |
| 30 | + System.out.println(); |
| 31 | + |
| 32 | + //see if the user wants to continue |
| 33 | + System.out.print("Continue? (y/n): "); |
| 34 | + choice=sc.next(); |
| 35 | + |
| 36 | + 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 | + } |
| 50 | + return futureValue; |
| 51 | + } |
| 52 | +} |
0 commit comments