Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Created March 27, 2017 21:46
Show Gist options
  • Save snarkbait/2353c42a8f1c0c137470804a86a45143 to your computer and use it in GitHub Desktop.
Save snarkbait/2353c42a8f1c0c137470804a86a45143 to your computer and use it in GitHub Desktop.
JavaFX Example - Future Value
package fvcalc;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FutureValue extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("fv_calc.fxml"));
primaryStage.setTitle("Future Value of Investment Calculator");
primaryStage.setScene(new Scene(root, 400, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.lang.String?>
<?import java.net.URL?>
<GridPane fx:controller="fvcalc.FVController"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="8">
<padding><Insets top="40" right="25" bottom="10" left="35"/></padding>
<Label text="Initial Investment:"
GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<TextField fx:id="textInvestment"
GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<Label text="Interest Rate:"
GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="textRate" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Years:"
GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<TextField fx:id="textYears"
GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Label text="Compounded:"
GridPane.columnIndex="0" GridPane.rowIndex="3"/>
<ComboBox fx:id="comboCompound"
GridPane.columnIndex="1" GridPane.rowIndex="3"
onAction="#getSelected">
<items><FXCollections fx:factory="observableArrayList">
<String fx:value="Yearly"/>
<String fx:value="Biannually"/>
<String fx:value="Quarterly"/>
<String fx:value="Monthly"/>
<String fx:value="Weekly"/>
<String fx:value="Daily"/>
</FXCollections></items>
<value><String fx:value="Yearly"/></value>
</ComboBox>
<Button text="Calculate"
GridPane.columnIndex="1" GridPane.rowIndex="4"
onAction="#calculate"/>
<Label fx:id="result" id="result_text" GridPane.columnIndex="0" GridPane.rowIndex="5"
GridPane.columnSpan="3"/>
<stylesheets>
<URL value="@fv_style.css"/>
</stylesheets>
</GridPane>
.root {
-fx-background-image: url("yellow_lined.png");
-fx-background-size: stretch;
}
.label {
-fx-font-family: "sans-serif";
-fx-font-size: 14px;
-fx-font-weight: bold;
-fx-font-style: italic;
}
.text-field {
-fx-font-family: "Verdana";
-fx-font-size: 12px;
}
#result_text {
-fx-font-family: "Arial Black";
-fx-font-size: 24px;
-fx-text-fill: red;
-fx-effect: dropshadow( gaussian , black , 2,0.2,0,1);
}
package fvcalc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import logic.FVLogic;
import logic.FVLogic.CompoundType;
public class FVController {
@FXML private Label result; // these fields here
@FXML private TextField textInvestment; // must be declared with the
@FXML private TextField textRate; // @FXML annotation
@FXML private TextField textYears; // so they can be referenced
@FXML private ComboBox<String> comboCompound; // from the FXML document
private CompoundType comp = CompoundType.YEARLY; // current type of compounding interest
@FXML public void calculate(ActionEvent event) {
double principal = getDouble(textInvestment.getText());
double rate = getDouble(textRate.getText());
double years = getDouble(textYears.getText());
if (principal > 0 && rate > 0 && years > 0) {
double calc = FVLogic.futureValue(principal, rate, (int) years, comp);
result.setText("$ " + String.format("%,.2f", calc));
}
else {
result.setText("Invalid Entry.");
}
}
private double getDouble(String text) {
if (text.length() > 0) {
double d;
try {
d = Double.parseDouble(text);
return d;
}
catch (NumberFormatException e) {
return 0.0;
}
}
return 0.0;
}
@FXML public void getSelected(ActionEvent e) {
comp = FVLogic.CompoundType.valueOf(comboCompound.getSelectionModel().getSelectedItem().toUpperCase());
}
}
package logic;
/**
* @author /u/Philboyd_Studge on 3/27/2017.
*/
public class FVLogic {
public enum CompoundType {
YEARLY(1), BIANNUALLY(2), QUARTERLY(4), MONTHLY(12), WEEKLY(52), DAILY(356);
private int compounding;
CompoundType(int compounding) { this.compounding = compounding; }
public int getCompounding() { return compounding; }
}
public static double futureValue(double investment, double rate, int periods, CompoundType cmp) {
rate = 1 + (rate / 100) / cmp.getCompounding();
periods *= cmp.getCompounding();
// FV = PV * R^n
return investment * Math.pow(rate, periods);
}
}
@ak637
Copy link

ak637 commented Feb 21, 2018

How would you convert this to a Present Value calculator?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment