forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise15_05.java
95 lines (77 loc) · 3.77 KB
/
Exercise15_05.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ch_15;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
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.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* *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.
*/
public class Exercise15_05 extends javafx.application.Application {
@Override
public void start(Stage primaryStage) throws Exception {
SimpleStringProperty displayResult = new SimpleStringProperty();
VBox mainBox = new VBox(5);
HBox investmentAmountBox = new HBox(5);
TextField amountField = new TextField();
Label amountLabel = new Label("Investment Amount:");
HBox.setMargin(amountLabel, new Insets(10, 5, 5, 1));
HBox.setMargin(amountField, new Insets(10, 5, 5, 1));
investmentAmountBox.getChildren().addAll(amountLabel, amountField);
HBox numYearsBox = new HBox(5);
Label yearsLabel = new Label("Number Of Years:");
TextField yearsField = new TextField();
HBox.setMargin(yearsField, new Insets(1, 5, 5, 1));
HBox.setMargin(yearsLabel, new Insets(1, 5, 5, 1));
numYearsBox.getChildren().addAll(yearsLabel, yearsField);
HBox interestRateBox = new HBox(5);
Label interestLabel = new Label("Annual Interest Rate:");
TextField interestField = new TextField();
HBox.setMargin(interestLabel, new Insets(1, 5, 5, 1));
HBox.setMargin(interestField, new Insets(1, 5, 5, 1));
interestRateBox.getChildren().addAll(interestLabel, interestField);
HBox futureValueBox = new HBox(5);
Label futureValLabel = new Label("Future Value:");
TextField resultField = new TextField();
HBox.setMargin(futureValLabel, new Insets(1, 5, 5, 5));
HBox.setMargin(resultField, new Insets(1, 5, 5, 5));
resultField.textProperty().bind(displayResult);
futureValueBox.getChildren().addAll(futureValLabel, resultField);
HBox bottomBox = new HBox(5);
Button calcBtn = new Button("Calculate");
bottomBox.getChildren().add(calcBtn);
HBox.setMargin(calcBtn, new Insets(1, 10, 0, 0));
calcBtn.setOnAction(e -> {
double fv = calculateFutureValue(amountField.getText(), yearsField.getText(), interestField.getText());
displayResult.set(String.format("$%.2f", fv));
});
bottomBox.setAlignment(Pos.BOTTOM_RIGHT);
mainBox.getChildren().addAll(investmentAmountBox, numYearsBox, interestRateBox, futureValueBox, bottomBox);
Scene scene = new Scene(mainBox, 400, 225);
primaryStage.setScene(scene);
primaryStage.setTitle(getClass().getName());
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
double calculateFutureValue(String investmentAmount, String numYears, String annualInterestRate) {
double amount = Double.parseDouble(investmentAmount);
int years = Integer.parseInt(numYears);
double monthRate = Double.parseDouble(annualInterestRate) / 100 / 12;
double temp = Math.pow((1 + monthRate), (years * 12.0));
return amount * temp;
}
}