Price a coffee order with add-ons using the Decorator pattern

Quick Overview

Build a small coffee ordering class in which a base drink is wrapped by add-ons that each add a name and a price, and the class reports the running description and the rounded total. It tests the Decorator pattern, resetting state between orders, and handling floating-point money.

Price a coffee order with add-ons using the Decorator pattern

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a coffee shop ordering system that calculates the price and description of a coffee with various add-ons using the Decorator pattern. The system should support creating a base coffee with a given name and price, then adding decorators (add-ons) such as milk, sugar, whipped cream, or any custom topping. Each add-on has its own name and price. The final cost is the sum of the base coffee price and all add-on prices. The final description is the base coffee name followed by all add-on names in the order they were added, separated by `" + "`. Implement the following methods: - `orderCoffee(name: str, price: float) -> str`: create a new base coffee order with the given name and price. This resets any previous order and add-ons. Returns the coffee name. - `addOn(name: str, price: float) -> str`: add a decorator (add-on) with the given name and price to the current coffee order. Returns the updated full description. - `getCost() -> float`: return the total cost of the current coffee order including all add-ons, rounded to 2 decimal places. - `getDescription() -> str`: return the full description of the current coffee order, with the base coffee and all add-ons joined by `" + "`. Example 1: ```text Input: ["CoffeeShop", "orderCoffee", "addOn", "addOn", "addOn", "getCost", "getDescription"] [[], ["Espresso", 3], ["Milk", 0.5], ["Sugar", 0.25], ["Whipped Cream", 0.75], [], []] Output: [null, "Espresso", "Espresso + Milk", "Espresso + Milk + Sugar", "Espresso + Milk + Sugar + Whipped Cream", 4.5, "Espresso + Milk + Sugar + Whipped Cream"] shop = CoffeeShop() # initialize the coffee shop shop.orderCoffee("Espresso", 3.00) # -> "Espresso" (base coffee ordered) shop.addOn("Milk", 0.50) # -> "Espresso + Milk" shop.addOn("Sugar", 0.25) # -> "Espresso + Milk + Sugar" shop.addOn("Whipped Cream", 0.75) # -> "Espresso + Milk + Sugar + Whipped Cream" shop.getCost() # -> 4.5 (3.00 + 0.50 + 0.25 + 0.75) shop.getDescription() # -> "Espresso + Milk + Sugar + Whipped Cream" ``` Example 2 (a new order discards the previous add-ons): ```text Input: ["CoffeeShop", "orderCoffee", "addOn", "orderCoffee", "addOn", "getCost", "getDescription"] [[], ["Latte", 4.25], ["Vanilla", 0.5], ["Mocha", 4], ["Oat Milk", 0.75], [], []] Output: [null, "Latte", "Latte + Vanilla", "Mocha", "Mocha + Oat Milk", 4.75, "Mocha + Oat Milk"] ``` The second `orderCoffee` call starts over, so neither the Latte price nor the Vanilla add-on counts any more. The total is 4.00 + 0.75 = 4.75. ```hint Shape of the classes Give the base coffee and every add-on the same small interface: one method for the cost, one for the description. Then ask what an add-on has to hold so it can answer both without knowing how many layers sit under it. ``` ### Constraints & Assumptions - `1 <= number of operations <= 1000` - Base coffee and add-on prices are non-negative floats with at most 2 decimal places. - Coffee and add-on names are non-empty strings containing only alphanumeric characters and spaces. - `getCost` returns a float rounded to 2 decimal places. - `orderCoffee` resets any previous order and add-ons. ### Clarifying Questions to Ask - What should `addOn`, `getCost` and `getDescription` do if no coffee has been ordered yet? - If the same add-on is added twice, should it be charged and listed twice? - Is a float acceptable for money here, or should prices be held as integer cents? - Will add-ons ever need to be removed, or priced as something other than a fixed amount? ### What a Strong Answer Covers - A shared component interface for cost and description, implemented by a base coffee class and by an add-on class that wraps another component. - How the shop tracks the current order and how `orderCoffee` discards earlier add-ons. - The exact description format (insertion order, the `" + "` separator) and rounding the total to 2 decimal places. - The time and space cost of each method, counting the fact that `addOn` returns the full description. - Edge cases: calls before any order, repeated add-ons, chains near the 1000-operation limit, and floating-point sums. ### Follow-up Questions - How would you add an undo that removes the most recent add-on? - How would the design change for an add-on whose price depends on the drink, such as a 10% discount on everything wrapped so far? - What changes if prices are stored as integer cents or `Decimal` values? - When is a plain list of add-ons a better design than a chain of decorators?

Overview: Build a small coffee ordering class in which a base drink is wrapped by add-ons that each add a name and a price, and the class reports the running description and the rounded total. It tests the Decorator pattern, resetting state between orders, and handling floating-point money.

PracHub logo
General
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a coffee shop ordering system that calculates the price and description of a coffee with various add-ons using the Decorator pattern.

The system should support creating a base coffee with a given name and price, then adding decorators (add-ons) such as milk, sugar, whipped cream, or any custom topping. Each add-on has its own name and price.

The final cost is the sum of the base coffee price and all add-on prices. The final description is the base coffee name followed by all add-on names in the order they were added, separated by " + ".

Implement the following methods:

  • orderCoffee(name: str, price: float) -> str : create a new base coffee order with the given name and price. This resets any previous order and add-ons. Returns the coffee name.
  • addOn(name: str, price: float) -> str : add a decorator (add-on) with the given name and price to the current coffee order. Returns the updated full description.
  • getCost() -> float : return the total cost of the current coffee order including all add-ons, rounded to 2 decimal places.
  • getDescription() -> str : return the full description of the current coffee order, with the base coffee and all add-ons joined by " + " .

Example 1:

Input:
["CoffeeShop", "orderCoffee",   "addOn",       "addOn",         "addOn",                 "getCost", "getDescription"]
[[],           ["Espresso", 3], ["Milk", 0.5], ["Sugar", 0.25], ["Whipped Cream", 0.75], [],        []]

Output: [null, "Espresso", "Espresso + Milk", "Espresso + Milk + Sugar", "Espresso + Milk + Sugar + Whipped Cream", 4.5, "Espresso + Milk + Sugar + Whipped Cream"]

shop = CoffeeShop()                  # initialize the coffee shop
shop.orderCoffee("Espresso", 3.00)   # -> "Espresso"  (base coffee ordered)
shop.addOn("Milk", 0.50)             # -> "Espresso + Milk"
shop.addOn("Sugar", 0.25)            # -> "Espresso + Milk + Sugar"
shop.addOn("Whipped Cream", 0.75)    # -> "Espresso + Milk + Sugar + Whipped Cream"
shop.getCost()                       # -> 4.5  (3.00 + 0.50 + 0.25 + 0.75)
shop.getDescription()                # -> "Espresso + Milk + Sugar + Whipped Cream"

Example 2 (a new order discards the previous add-ons):

Input:
["CoffeeShop", "orderCoffee",   "addOn",          "orderCoffee", "addOn",            "getCost", "getDescription"]
[[],           ["Latte", 4.25], ["Vanilla", 0.5], ["Mocha", 4],  ["Oat Milk", 0.75], [],        []]

Output: [null, "Latte", "Latte + Vanilla", "Mocha", "Mocha + Oat Milk", 4.75, "Mocha + Oat Milk"]

The second orderCoffee call starts over, so neither the Latte price nor the Vanilla add-on counts any more. The total is 4.00 + 0.75 = 4.75.

Constraints & Assumptions

  • 1 <= number of operations <= 1000
  • Base coffee and add-on prices are non-negative floats with at most 2 decimal places.
  • Coffee and add-on names are non-empty strings containing only alphanumeric characters and spaces.
  • getCost returns a float rounded to 2 decimal places.
  • orderCoffee resets any previous order and add-ons.

Clarifying Questions to Ask Guidance

  • What should addOn , getCost and getDescription do if no coffee has been ordered yet?
  • If the same add-on is added twice, should it be charged and listed twice?
  • Is a float acceptable for money here, or should prices be held as integer cents?
  • Will add-ons ever need to be removed, or priced as something other than a fixed amount?

What a Strong Answer Covers Guidance

  • A shared component interface for cost and description, implemented by a base coffee class and by an add-on class that wraps another component.
  • How the shop tracks the current order and how orderCoffee discards earlier add-ons.
  • The exact description format (insertion order, the " + " separator) and rounding the total to 2 decimal places.
  • The time and space cost of each method, counting the fact that addOn returns the full description.
  • Edge cases: calls before any order, repeated add-ons, chains near the 1000-operation limit, and floating-point sums.

Follow-up Questions Guidance

  • How would you add an undo that removes the most recent add-on?
  • How would the design change for an add-on whose price depends on the drink, such as a 10% discount on everything wrapped so far?
  • What changes if prices are stored as integer cents or Decimal values?
  • When is a plain list of add-ons a better design than a chain of decorators?