Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help me debug this Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart with the JavaFX version of the Future Value application

please help me debug this

Create a Future Value Calculator that displays error messages in labels.GUISpecificationsStart with the JavaFX version of the Future Value application presented in chapter 17. Create error message labels for each text field that accepts user input. Use the Validation class from chapter 17 to validate user input.Format the application so that the controls dont change position when error messages are displayed.

package murach.business;

public class Calculation { public static final int MONTHS_IN_YEAR = 12;

public static double futureValue(double monthlyPayment, double yearlyInterestRate, int years) { int months = years * MONTHS_IN_YEAR; double monthlyInterestRate = yearlyInterestRate / MONTHS_IN_YEAR / 100; double futureValue = 0; for (int i = 1; i <= months;) { futureValue = (futureValue + monthlyPayment) * (1 + monthlyInterestRate); } return futureValue; } }

/***********************************************************************************

package murach.business;

public class Validation { private 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 = ""; { 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; } } /**************************************************************************************************************

package murach.ui;

import murach.business.Validation; import murach.business.Calculation; import java.text.NumberFormat;

import javafx.application.Application; 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.scene.layout.HBox; import javafx.stage.Stage;

public class FutureValueApp extends Application { private TextField investmentField; private TextField interestRateField; private TextField yearsField; private TextField futureValueField; private Label investmentLabel; private Label interestRateLabel; private Label yearsLabel; @Override public void start(Stage primaryStage) { primaryStage.setTitle("Future Value Calculator"); GridPane grid = new GridPane(); grid.setAlignment(Pos.TOP_LEFT); grid.setPadding(new Insets(25, 25, 25, 25)); grid.setHgap(10); grid.setVgap(10);

Scene scene = new Scene(grid, 580, 220); grid.add(new Label("Monthly Investment:"), 0, 0); investmentField = new TextField(); grid.add(investmentField, 1, 0); investmentLabel = new Label(); grid.add(investmentLabel, 2, 0);

grid.add(new Label("Yearly Interest Rate:"), 0, 1); interestRateField = new TextField(); grid.add(interestRateField, 1, 1); interestRateLabel = new Label(); grid.add(interestRateLabel, 2, 1); grid(new Label("Years:"), 0, 2); yearsField = new TextField(); grid.add(yearsField, 1, 2); yearsLabel = new Label(); grid.add(yearsLabel, 2, 2); grid.add(new Label("Future Value:"), 0, 3); futureValueField = new TextField(); futureValueField.setEditable(false); grid.add(futureValueField, 1, 3);

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(); primaryStage.show(); } private void calculateButtonClicked() { // set error messages in labels Validation v = new Validation(); investmentLabel.setText( v.isDouble(investmentField.getText(), "Monthly Investment") ); interestRateLabel.setText( v.isDouble(interestRateField.getText(), "Yearly Interest Rate") ); yearsLabel.setText( v.isDouble(yearsField.getText(), "Years") ); // check if all error labels are empty if (investmentLabel.getText().isEmpty() && interestRateLabel.getText().isEmpty() && yearsLabel.getText().isEmpty()) {

// get data from text fields double investment = Double.parseDouble(investmentField.getText()); double rate = Double.parseDouble(interestRateField.getText()); int years = Integer.parseInt(yearsField.getText());

// calculate future value double futureValue = Calculation.futureValue(investment, rate, years);

// set data in read-only text field NumberFormat currency = NumberFormat.getCurrencyInstance(); futureValueField.setText(currency.format(futureValue)); } }

private void exitButtonClicked() { System.exit(0); // 0 indicates a normal exit } public static void main(String[] args) { launch(args); } }

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

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

=+is irrational.

Answered: 1 week ago

Question

Does it avoid use of underlining?

Answered: 1 week ago