Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

13.6 (Tip Calculator Modification) The Tip Calculator app from Section 12.5 does not need a Button to perform its calculations. Reimplement this app to use

13.6 (Tip Calculator Modification) The Tip Calculator app from Section 12.5 does not need a Button to perform its calculations. Reimplement this app to use property listeners to perform the calculations whenever the user modifies the bill amount or changes the custom tip percentage. Also use a property binding to update the Label that displays the tip percentage.

// Fig. 12.19: TipCalculator.java

// Main application class that loads and displays the Tip Calculator's GUI.

import javafx.application.Application;

import javafx.fxml.FXMLLoader;

import javafx.scene.Parent;

import javafx.scene.Scene;

import javafx.stage.Stage;

public class TipCalculator extends Application {

@Override

public void start(Stage stage) throws Exception {

Parent root =

FXMLLoader.load(getClass().getResource("TipCalculator.fxml"));

Scene scene = new Scene(root); // attach scene graph to scene

stage.setTitle("Tip Calculator"); // displayed in window's title bar

stage.setScene(scene); // attach scene to stage

stage.show(); // display the stage

}

public static void main(String[] args) {

// create a TipCalculator object and call its start method

launch(args);

}

}

// TipCalculatorController.java

// Controller that handles calculateButton and tipPercentageSlider events

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.text.NumberFormat;

import javafx.beans.value.ChangeListener;

import javafx.beans.value.ObservableValue;

import javafx.event.ActionEvent;

import javafx.fxml.FXML;

import javafx.scene.control.Label;

import javafx.scene.control.Slider;

import javafx.scene.control.TextField;

public class TipCalculatorController {

// formatters for currency and percentages

private static final NumberFormat currency =

NumberFormat.getCurrencyInstance();

private static final NumberFormat percent =

NumberFormat.getPercentInstance();

private BigDecimal tipPercentage = new BigDecimal(0.15); // 15% default

// GUI controls defined in FXML and used by the controller's code

@FXML

private TextField amountTextField;

@FXML

private Label tipPercentageLabel;

@FXML

private Slider tipPercentageSlider;

@FXML

private TextField tipTextField;

@FXML

private TextField totalTextField;

// calculates and displays the tip and total amounts

@FXML

private void calculateButtonPressed(ActionEvent event) {

try {

BigDecimal amount = new BigDecimal(amountTextField.getText());

BigDecimal tip = amount.multiply(tipPercentage);

BigDecimal total = amount.add(tip);

tipTextField.setText(currency.format(tip));

totalTextField.setText(currency.format(total));

}

catch (NumberFormatException ex) {

amountTextField.setText("Enter amount");

amountTextField.selectAll();

amountTextField.requestFocus();

}

}

// called by FXMLLoader to initialize the controller

public void initialize() {

// 0-4 rounds down, 5-9 rounds up

currency.setRoundingMode(RoundingMode.HALF_UP);

// listener for changes to tipPercentageSlider's value

tipPercentageSlider.valueProperty().addListener(

new ChangeListener() {

@Override

public void changed(ObservableValue ov,

Number oldValue, Number newValue) {

tipPercentage =

BigDecimal.valueOf(newValue.intValue() / 100.0);

tipPercentageLabel.setText(percent.format(tipPercentage));

}

}

);

}

}

///TripCalculator.fxml

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

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

1 for suggestions). Be specific.

Answered: 1 week ago

Question

LO1 Understand risk management and identify its components.

Answered: 1 week ago