Code Logo

Snack Order Summary

Published at22 Apr 2026
Java OOP Easy 0 views
Like0

A snack booth records each order as one item name, one unit price, and one quantity. The staff wants a short summary line for each order so they can check it quickly before payment.

Your task is to use a small object to return a string in the format "Name xQuantity = Total", where Total is price * quantity.

For example, if the item is "Cookie", the price is 5, and the quantity is 3, the answer should be "Cookie x3 = 15". If the item is "Juice", the price is 4, and the quantity is 2, the answer should be "Juice x2 = 8".

This is an easy class-design task. The order object should store the item data and expose a method that builds the final summary text from that stored state.

Example Input & Output

Example 1
Input
name = "Cookie", price = 5, quantity = 3
Output
"Cookie x3 = 15"
Explanation

The total is calculated from one item's price and quantity.

Example 2
Input
name = "Juice", price = 4, quantity = 2
Output
"Juice x2 = 8"
Explanation

The summary line keeps the item name and quantity visible.

Example 3
Input
name = "Tea", price = 6, quantity = 1
Output
"Tea x1 = 6"
Explanation

A quantity of one should still follow the same summary format.

Algorithm Flow

Recommendation Algorithm Flow for Snack Order Summary
Recommendation Algorithm Flow for Snack Order Summary

Solution Approach

This problem works well as a simple object model because the item name, price, and quantity all belong to the same order line. Instead of passing those values around separately forever, a class can hold them together and offer a method that computes the final summary when needed.

A class like OrderItem can have three fields and a constructor to initialize them. Then a method such as getSummary() can calculate the total and format the string in the required output style.

The core method might look like this:

public String getSummary() {
    int total = price * quantity;
    return name + " x" + quantity + " = " + total;
}

The outer solution method then becomes very small. It creates an OrderItem object and returns the result of getSummary(). That is exactly the kind of separation OOP is good at: data lives in the object, and behavior that depends on that data stays close to it.

You could calculate the total directly in the solution method without defining a class, but that would not practice the idea this category is trying to teach. The value of the exercise is learning how a class can represent one real-world thing cleanly.

The runtime is constant, O(1), because the method does only a few fixed operations. The interesting part is the class design, not the complexity.

Best Answers

java - Approach 1
class OrderItem {
    private String name;
    private int price;
    private int quantity;

    public OrderItem(String name, int price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public String getSummary() {
        int total = price * quantity;
        return name + " x" + quantity + " = " + total;
    }
}

class Solution {
    public static String buildOrderSummary(String name, int price, int quantity) {
        return new OrderItem(name, price, quantity).getSummary();
    }
}