Bird
0
0

Given the following Aggregate root code snippet, what will be the output of order.total_price()?

medium📝 Analysis Q4 of 15
LLD - Advanced LLD Concepts
Given the following Aggregate root code snippet, what will be the output of order.total_price()?
class OrderItem:
    def __init__(self, price, quantity):
        self.price = price
        self.quantity = quantity

class Order:
    def __init__(self):
        self.items = []
    def add_item(self, item):
        self.items.append(item)
    def total_price(self):
        return sum(i.price * i.quantity for i in self.items)

order = Order()
order.add_item(OrderItem(10, 2))
order.add_item(OrderItem(5, 4))
print(order.total_price())
A40
B30
C50
D20
Step-by-Step Solution
Solution:
  1. Step 1: Calculate total for each OrderItem

    First item: 10 * 2 = 20; Second item: 5 * 4 = 20
  2. Step 2: Sum all item totals

    20 + 20 = 40
  3. Final Answer:

    40 -> Option A
  4. Quick Check:

    Sum of (price * quantity) = 40 [OK]
Quick Trick: Multiply price by quantity for each item, then sum [OK]
Common Mistakes:
  • Adding prices without multiplying by quantity
  • Multiplying quantities only
  • Ignoring one of the items

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes