In: Computer Science
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.
Code:
----------------TipCalculator.java-----------------------
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--------------------
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------------
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="8.0" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
xmlns="http://javafx.com/javafx/8.0.60"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="TipCalculatorController">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES"
minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Amount" />
<Label fx:id="tipPercentageLabel" text="15%"
GridPane.rowIndex="1" />
<Label text="Tip" GridPane.rowIndex="2" />
<Label text="Total" GridPane.rowIndex="3" />
<TextField fx:id="amountTextField" GridPane.columnIndex="1"
/>
<TextField fx:id="tipTextField" editable="false"
focusTraversable="false" GridPane.columnIndex="1"
GridPane.rowIndex="2" />
<TextField fx:id="totalTextField" editable="false"
focusTraversable="false" GridPane.columnIndex="1"
GridPane.rowIndex="3" />
<Slider fx:id="tipPercentageSlider" blockIncrement="5.0"
max="30.0" value="15.0" GridPane.columnIndex="1"
GridPane.rowIndex="1" />
<Button maxWidth="1.7976931348623157E308"
mnemonicParsing="false" onAction="#calculateButtonPressed"
text="Calculate" GridPane.columnIndex="1" GridPane.rowIndex="4"
/>
</children>
<padding>
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0"
/>
</padding>
</GridPane>
TipCalculator.java (Main class)
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);
stage.setScene(scene);
stage.setTitle("Tip Calculator");
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
TipCalculator.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="8.0" maxHeight="-Infinity"
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="231.0" prefWidth="365.0"
xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="tipcalculator.TipCalculatorController">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES"
minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Amount" />
<Label fx:id="tipPercentageLabel" text="15%"
GridPane.rowIndex="2" />
<Label text="Tip" GridPane.rowIndex="3" />
<Label text="Total" GridPane.rowIndex="4" />
<TextField fx:id="amountTextField" GridPane.columnIndex="1"
/>
<TextField fx:id="tipTextField" editable="false"
focusTraversable="false" GridPane.columnIndex="1"
GridPane.rowIndex="3" />
<TextField fx:id="totalTextField" editable="false"
focusTraversable="false" GridPane.columnIndex="1"
GridPane.rowIndex="4" />
<Slider fx:id="tipPercentageSlider" blockIncrement="5.0"
max="30.0" value="15.0" GridPane.columnIndex="1"
GridPane.rowIndex="2" />
<Button maxWidth="1.7976931348623157E308"
mnemonicParsing="false" onAction="#calculateButtonPressed"
text="Calculate" GridPane.columnIndex="1" GridPane.rowIndex="6"
/>
<Label text="Amount per person" GridPane.rowIndex="5"
/>
<TextField fx:id="amountPerPersonTextField" editable="false"
GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Label text="Number of people" GridPane.rowIndex="1" />
<TextField fx:id="numPeopleTextField" GridPane.columnIndex="1"
GridPane.rowIndex="1" />
</children>
<padding>
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0"
/>
</padding>
</GridPane>
TipCalculatorController.java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.text.NumberFormat;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
public class TipCalculatorController implements Initializable
{
private static final NumberFormat currency =
NumberFormat.getCurrencyInstance();
private static final NumberFormat percent =
NumberFormat.getPercentInstance();
private BigDecimal tipPercentage = new BigDecimal(0.15);
@FXML
private TextField amountTextField;
@FXML
private TextField numPeopleTextField;
@FXML
private Label tipPercentageLabel;
@FXML
private Slider tipPercentageSlider;
@FXML
private TextField tipTextField;
@FXML
private TextField totalTextField;
@FXML
private TextField amountPerPersonTextField;
@FXML
private void calculateButtonPressed(ActionEvent event)
{
try {
BigDecimal amount = new
BigDecimal(amountTextField.getText());
BigDecimal tip = amount.multiply(tipPercentage);
BigDecimal numPeople = new
BigDecimal(numPeopleTextField.getText());
BigDecimal total = amount.add(tip);
tipTextField.setText(currency.format(tip));
totalTextField.setText(currency.format(total));
BigDecimal amountPerPerson = total.divide(numPeople, 2);
amountPerPersonTextField.setText(currency.format(amountPerPerson));
}
catch (NumberFormatException ex) {
amountTextField.setText("Enter amount");
numPeopleTextField.setText("Enter total number of people");
amountTextField.selectAll();
amountTextField.requestFocus();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// 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<Number>() {
@Override
public void changed(ObservableValue<? extends Number>
observable, Number oldValue, Number newValue) {
tipPercentage = BigDecimal.valueOf(newValue.intValue() /
100.0);
tipPercentageLabel.setText(percent.format(tipPercentage));
}
});
}
}
******************************************************************** SCREENSHOT *******************************************************