Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Joshua Ferrell
- // Date: 3/25/2017
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.geometry.HPos;
- 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;
- A program that caculates the return on an investment.
- public class Exercise15_5 extends Application {
- // globals
- private TextField tfInvestmentAmount = new TextField();
- private TextField tfNumYears = new TextField();
- private TextField tfAnnualInterestRate = new TextField();
- private TextField tfFutureValue = new TextField();
- private Button btCalc = new Button("Calculate");
- @Override
- public void start(Stage primaryStage) {
- // Create UI
- GridPane gridPane = new GridPane();
- gridPane.setHgap(5);
- gridPane.setVgap(5);
- gridPane.add(new Label("Investment Amount:"), 0, 0);
- gridPane.add(tfInvestmentAmount, 1, 0);
- gridPane.add(new Label("Number of Years:"), 0, 1);
- gridPane.add(tfNumYears, 1, 1);
- gridPane.add(new Label("Annual Interest Rate:"), 0, 2);
- gridPane.add(tfAnnualInterestRate, 1, 2);
- gridPane.add(new Label("Future Value:"), 0, 3);
- gridPane.add(tfFutureValue, 1, 3);
- gridPane.add(btCalc, 1, 4);
- // Set Properties for UI
- gridPane.setAlignment(Pos.CENTER);
- tfInvestmentAmount.setAlignment(Pos.BOTTOM_RIGHT);
- tfNumYears.setAlignment(Pos.BOTTOM_RIGHT);
- tfAnnualInterestRate.setAlignment(Pos.BOTTOM_RIGHT);
- tfFutureValue.setAlignment(Pos.BOTTOM_RIGHT);
- tfFutureValue.setEditable(false);
- gridPane.setHalignment(btCalc, HPos.RIGHT);
- // Process event (caculate future value)
- btCalc.setOnAction(e -> calculateFutureValue());
- // Create a scene and place it in the stage
- primaryStage.setScene(new Scene(gridPane, 300, 200));
- primaryStage.setTitle("Exercise15_05"); // Set Title
- primaryStage.show(); // Display the Stage
- }
- private void calculateFutureValue() {
- // Get Values from text fields
- double amount = Double.parseDouble(tfInvestmentAmount.getText());
- double interest = Double.parseDouble(tfAnnualInterestRate.getText());
- int years = Integer.parseInt(tfNumYears.getText());
- // Get Monthly interest rate
- double monthlyInterestRate = interest / 12 / 100;
- // Get Future Value
- double futureValue = amount * Math.pow(1 + monthlyInterestRate, years * 12);
- tfFutureValue.setText(String.format("$%.2f", futureValue));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement