w3resource

JavaFX shopping cart application

JavaFx Events and Event Handling: Exercise-7 with Solution

Write a JavaFX application that simulates a shopping cart. Add items with buttons, and implement action listeners to update the cart when an item is added.

Sample Solution:

JavaFx Code:

//Main.java

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    private VBox cart;
    private int itemCount = 0;
    private Label cartLabel;

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Shopping Cart");

        // Create a list to represent the shopping cart
        cart = new VBox();
        cart.setSpacing(10);
        cart.setPadding(new Insets(10));

        cartLabel = new Label("Shopping Cart:");
        HBox cartContainer = new HBox(cartLabel, cart);

        // Create item buttons
        Button addItemButton1 = createAddItemButton("Item 1", 10.0);
        Button addItemButton2 = createAddItemButton("Item 2", 20.0);
        Button addItemButton3 = createAddItemButton("Item 3", 30.0);

        // Create the layout
        VBox root = new VBox(cartContainer, addItemButton1, addItemButton2, addItemButton3);
        root.setSpacing(10);
        root.setPadding(new Insets(10));

        Scene scene = new Scene(root, 300, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Button createAddItemButton(String itemName, double itemPrice) {
        Button button = new Button("Add " + itemName + " to Cart");
        button.setOnAction(event -> addItemToCart(itemName, itemPrice));
        return button;
    }

    private void addItemToCart(String itemName, double itemPrice) {
        Label itemLabel = new Label(itemName + " - $" + itemPrice);
        cart.getChildren().add(itemLabel);
        itemCount++;
        updateCartLabel();
    }

    private void updateCartLabel() {
        cartLabel.setText("Shopping Cart (" + itemCount + " items):");
    }
}

The above exercise creates a JavaFX shopping cart application with a list that simulates adding items. When we click the "Add Item" button for an item, it's added to the cart, and the cart's label is updated to show the number of items in the cart. We can expand this example by adding more items or customizing the appearance of items in the cart.

Sample Output:

JavaFx: JavaFX shopping cart application.
JavaFx: JavaFX shopping cart application.

Flowchart:

Flowchart: JavaFX shopping cart application.
Flowchart: JavaFX shopping cart application.

Java Code Editor:

Previous: JavaFX simple calculator.
Next: JavaFX Login form 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://w3resource.com/java-exercises/javafx/javafx-events-and-event-handling-exercise-7.php