Snack Order Summary
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
The total is calculated from one item's price and quantity.
The summary line keeps the item name and quantity visible.
A quantity of one should still follow the same summary format.
Algorithm Flow

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:
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
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();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
