Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Murach Java 6 th Edition FutureValueApplication.java import java.text.NumberFormat; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField;

In Murach Java 6th Edition
FutureValueApplication.java
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class FutureValueApplication extends Application {
private TextField investmentField;
private TextField interestRateField;
private TextField yearsField;
private TextField futureValueField;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("Future Value Calculator");
GridPane grid = new GridPane();
grid.setAlignment(Pos.TOP_CENTER);
grid.setPadding(new Insets(25,25,25,25));
grid.setHgap(10);
grid.setVgap(10);
Scene scene = new Scene(grid);
grid.add(new Label("Monthly Investment:"),0,0);
investmentField = new TextField();
grid.add(investmentField,1,0);
grid.add(new Label("Yearly Interest Rate:"),0,1);
interestRateField = new TextField();
grid.add(interestRateField,1,1);
grid.add(new Label("Years:"),0,2);
yearsField = new TextField();
grid.add(yearsField,1,2);
grid.add(new Label("Future Value:"),0,3);
futureValueField = new TextField();
futureValueField.setEditable(false);
grid.add(futureValueField,1,3);
Button calculateButton = new Button("Calculate");
calculateButton.setOnAction(event -> calculateButtonClicked());
Button exitButton = new Button("Exit");
exitButton.setOnAction(event -> exitButtonClicked());
HBox buttonBox = new HBox(10);
buttonBox.getChildren().add(calculateButton);
buttonBox.getChildren().add(exitButton);
buttonBox.setAlignment(Pos.BOTTOM_RIGHT);
grid.add(buttonBox,0,4,2,1);
primaryStage.setScene(scene);
primaryStage.show();
}
private void calculateButtonClicked(){
Validation v = new Validation();
String errorMsg ="";
errorMsg += v.isDouble(investmentField.getText(),
"Monthly Investment");
errorMsg += v.isDouble(interestRateField.getText(),
"Yearly Interest Rate");
errorMsg += v.isInteger(yearsField.getText(),
"Years");
if (errorMsg.isEmpty()){
double investment = Double.parseDouble(
investmentField.getText());
double rate = Double.parseDouble(
interestRateField.getText());
int years = Integer.parseInt(
yearsField.getText());
double futureValue = calculateFutureValue(
investment, rate, years);
NumberFormat currency = NumberFormat.getCurrencyInstance();
futureValueField.setText(currency.format(futureValue));
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("Invalid Data");
alert.setContentText(errorMsg);
alert.showAndWait();
}
}
public double calculateFutureValue(double investment, double rate, int years){
double futureValue =0.0;
// convert yearly values to monthly values
double monthlyInterestRate = rate /12/100;
int months = years *12;
for (int i =1; i = months; i++){
futureValue =(futureValue + investment)
*(1+ monthlyInterestRate);
}
return futureValue;
}
private void exitButtonClicked(){
System.exit(0); //0 indicates a normal exit
}
}
Validation.java:
//package com.murach.future_value;
public class Validation {
private final String lineEnd;
public Validation(){
this.lineEnd ="
";
}
public Validation(String lineEnd){
this.lineEnd = lineEnd;
}
public String isPresent(String value, String name){
String msg ="";
if (value.isEmpty()){
msg = name +" is required." + lineEnd;
}
return msg;
}
public String isDouble(String value, String name){
String msg ="";
try {
Double.parseDouble(value);
} catch (NumberFormatException e){
msg = name +" must be a valid number." + lineEnd;
}
return msg;
}
public String isInteger(String value, String name){
String msg ="";
try {
Integer.parseInt(value);
} catch (NumberFormatException e){
msg = name +" must be an integer." + lineEnd;
}
return msg;
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

Explain the various techniques of Management Development.

Answered: 1 week ago

Question

Describe alternative training and development delivery systems.

Answered: 1 week ago

Question

Summarize the learning organization idea as a strategic mind-set.

Answered: 1 week ago