Question

In: Computer Science

Modify the Tip Calculator app to allow the user to enter the number of people in...

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>

Solutions

Expert Solution

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 *******************************************************


Related Solutions

In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *...
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
Forms often allow a user to enter an integer. Write a program that takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no PYTHON 3
Forms often allow a user to enter an integer. Write a programthat takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
IN ANDROID STUDIO, you will create a mortgage calculator appthat allows the user to enter...
IN ANDROID STUDIO, you will create a mortgage calculator app that allows the user to enter a purchase price, down-payment amount, and an interest rate.Based on these values, the app should calculate the loan amount (purchase price minus down payment) and display the monthly payment for 10, 20, and 30-year loans.Allow the user to select a custom loan duration (in years) by using a SeekBar and display the monthly payment for that custom loan duration.Assignment deliverables (all in a ZIP...
*Create a python function that uses a while list to allow a user to enter values...
*Create a python function that uses a while list to allow a user to enter values for sales and to add each value into a list in order to track all values entered. Set the gathering of values to stop when the value of zero is entered. Then take the created list and calculate the values within the list to return the total for all the values. Next, take the previously created list and display all the values from smallest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT