w3resource

JavaFX Food ordering application

JavaFx User Interface Components: Exercise-12 with Solution

Write a JavaFX food ordering application using CheckBox options for different food items (e.g., "Burger," "Pizza"). Calculate the total cost based on the selected items.

Sample Solution:

JavaFx Code:

//Main.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Food Order App");

        // Create a label to display the total cost.
        Label totalCostLabel = new Label("Total Cost: $0.00");

        // Create CheckBox options for food items.
        CheckBox burgerCheckBox = new CheckBox("Burger ($5.00)");
        CheckBox pizzaCheckBox = new CheckBox("Pizza ($8.00)");

        // Handle the action when a CheckBox is clicked.
        burgerCheckBox.setOnAction(event -> updateTotalCost(totalCostLabel, burgerCheckBox, pizzaCheckBox));
        pizzaCheckBox.setOnAction(event -> updateTotalCost(totalCostLabel, burgerCheckBox, pizzaCheckBox));

        // Create a layout (VBox) to arrange the CheckBox options and label.
        VBox root = new VBox(10);
        root.getChildren().addAll(burgerCheckBox, pizzaCheckBox, totalCostLabel);

        // Create the scene and set it in the stage.
        Scene scene = new Scene(root, 300, 200);
        primaryStage.setScene(scene);

        // Set the title of the window.
        primaryStage.show();
    }

    // Update the label to display the total cost based on selected items.
    private void updateTotalCost(Label label, CheckBox burgerCheckBox, CheckBox pizzaCheckBox) {
        double totalCost = 0.0;

        if (burgerCheckBox.isSelected()) {
            totalCost += 5.00;
        }
        if (pizzaCheckBox.isSelected()) {
            totalCost += 8.00;
        }

        label.setText("Total Cost: $" + String.format("%.2f", totalCost));
    }
}

In the above JavaFX food ordering application, we use CheckBox options for Burger and Pizza. When a CheckBox is selected, the label displays the total cost based on the selected items. The elements are organized using a 'VBox'.

Sample Output:

JavaFx: JavaFX Food ordering application.
JavaFx: JavaFX Food ordering application.
JavaFx: JavaFX Food ordering application.

Flowchart:

Flowchart: JavaFX Food ordering application.

Java Code Editor:

Previous: JavaFX preference form using RadioButton.
Next: JavaFX ToggleButton Radio Application.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/javafx/javafx-user-interface-components-exercise-12.php