Skip to content

Commit 0175b96

Browse files
committed
Enhanced For Loop
1 parent dfb1d9c commit 0175b96

1 file changed

Lines changed: 35 additions & 41 deletions

File tree

src/FutureValueApp.java

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,46 @@
1-
import java.text.NumberFormat;
21
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;
243

25-
//format and display the result
4+
public class FutureValueApp {
265

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
2734
NumberFormat currency = NumberFormat.getCurrencyInstance();
28-
System.out.println("Future value: "+
29-
currency.format(futureValue));
35+
System.out.println("Future value: "
36+
+ currency.format(futureValue));
3037
System.out.println();
3138

32-
//see if the user wants to continue
39+
// see if the user wants to continue
3340
System.out.print("Continue? (y/n): ");
34-
choice=sc.next();
35-
41+
choice = sc.next();
3642
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-
4943
}
50-
return futureValue;
44+
System.out.println("Bye!");
5145
}
5246
}

0 commit comments

Comments
 (0)