Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

12.3 (Enhanced Tip Calculator App) Modify the Tip Calculator app to allow the user to enter the number of people in the party. Calculate and

12.3 (Enhanced Tip Calculator App) Modify the Tip Calculator app to allow the user to enter the number of people in the party. Calculate and display the amount owed by each person if the bill were to be split evenly among the party members.

// 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));

}

}

);

}

}

//tipCalculator.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

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

What are the usual characteristics of preferred shares?

Answered: 1 week ago

Question

83 Change management methods.

Answered: 1 week ago