*15.5 (Create an investment-value calculator) Write a program that calculates the future value of an investment at a given interest rate for a specified number of years. The formula for the calculation is:futureValue = investmentAmount * (1 + monthlyInterestRate)^(years*12)
Use text fields for the investment amount, number of years, and annual interest rate. Display the future amount in a text field when the user clicks the Calculate button, as shown in Figure 15.25b
package section_15;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class _05 extends Application {
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
Label labelInvestmentAmount = new Label("Investment Amount:");
Label labelNumberOfYears = new Label("Number of Years:");
Label labelAnnualInterestRate = new Label("Annual Interest Rate:");
Label labelFutureValue = new Label("Future Value:");
TextField tfInvestmentAmount = new TextField();
tfInvestmentAmount.setAlignment(Pos.BASELINE_RIGHT);
TextField tfNumberOfYears = new TextField();
tfNumberOfYears.setAlignment(Pos.BASELINE_RIGHT);
TextField tfAnnualInterestRate = new TextField();
tfAnnualInterestRate.setAlignment(Pos.BASELINE_RIGHT);
TextField tfFutureValue = new TextField();
tfFutureValue.setAlignment(Pos.BASELINE_RIGHT);
Button btCalculate = new Button("Calculate");
GridPane.setHalignment(btCalculate, HPos.RIGHT);
btCalculate.setOnAction(event -> {
double investmentAmount = Double.parseDouble(tfInvestmentAmount.getText());
double numberOfYears = Double.parseDouble(tfNumberOfYears.getText());
double annualInterestRate = Double.parseDouble(tfAnnualInterestRate.getText());
double futureValue = investmentAmount * Math.pow(1 + (annualInterestRate / 1200), numberOfYears * 12);
futureValue = Math.round(futureValue * 100) / 100.0;
tfFutureValue.setText("$" + futureValue);
});
pane.add(labelInvestmentAmount, 0, 0);
pane.add(labelNumberOfYears, 0, 1);
pane.add(labelAnnualInterestRate, 0, 2);
pane.add(labelFutureValue, 0, 3);
pane.add(tfInvestmentAmount, 1, 0);
pane.add(tfNumberOfYears, 1, 1);
pane.add(tfAnnualInterestRate, 1, 2);
pane.add(tfFutureValue, 1, 3);
pane.add(btCalculate, 1, 4);
pane.setHgap(10);
pane.setVgap(5);
pane.setPadding(new Insets(20));
Scene scene = new Scene(pane);
primaryStage.setTitle("_15_05");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}