Skip to content

Day 8 : Order + Payment System

Below is Day 8 — Order + Payment System, built fully and strictly in the same machine-coding, interview-ready format.
This is harder than Day 7.5, but still SDE-1 safe. No new theory. No design patterns. Just more coordination + rules.

🟢 Day 8 — Order & Payment System (Multi-Class, Rule-Heavy)

This problem appears (in variants) in Amazon, Flipkart, Uber, Meesho interviews.

🎯 Day 8 Objective

By the end of Day 8, the candidate should be able to:
Implement a multi-class business workflow
Coordinate Order + Items + Payment
Enforce state-based restrictions
Prevent invalid transitions
Pass a real backend-style machine coding round
Day 8 tests whether the candidate can build a small backend system, not just classes.

⏱ Interview Simulation Rules

Give these rules before coding:
⏰ Time limit: 90 minutes
❌ Do NOT change class names
❌ Do NOT change method signatures
❌ Do NOT add new public methods
❌ Do NOT modify test file
❌ No print statements
✅ Logic must match requirements exactly

🧠 Problem Theme — Online Order System

You are implementing a basic order system with payment support.
The system supports:
Creating an order
Adding items
Calculating total price
Making payment
Enforcing order state rules

🧱 System Overview (Mental Model)

Order
├── has many OrderItems
├── has total price
├── has payment state
└── controls what actions are allowed

📦 Order States

An order can be in one of these states:
CREATED
PAID
CANCELLED

🚫 Rules Summary (Read Carefully)

Items can be added only when order is CREATED
Payment can happen only once
Order cannot be modified after payment
Cancelled order cannot be paid
Total amount must match payment amount

🧱 Class Definitions (DO NOT MODIFY)

class OrderItem:
def __init__(self, item_id: str, price: int, quantity: int):
pass

def get_total_price(self) -> int:
pass


class Payment:
def __init__(self, amount: int):
pass

def is_successful(self) -> bool:
pass


class Order:
def __init__(self, order_id: str):
pass

def add_item(self, item: OrderItem) -> bool:
pass

def get_total_amount(self) -> int:
pass

def make_payment(self, payment: Payment) -> bool:
pass

def cancel(self) -> bool:
pass

def get_status(self) -> str:
pass

📌 Detailed Requirements (NO ASSUMPTIONS)

🔹 OrderItem

__init__(item_id, price, quantity)

Store:
item_id
price
quantity

get_total_price()

Return price * quantity

🔹 Payment

__init__(amount)

Store payment amount

is_successful()

Always return True
(Actual payment gateway is NOT part of this problem)

🔹 Order

__init__(order_id)

Initialize:
order_id
empty list of items
status = CREATED
no payment attached

add_item(item)

Return True if:
Order status is CREATED
Item quantity > 0
Item price > 0
Else return False.

get_total_amount()

Return sum of get_total_price() for all items
If no items → return 0

make_payment(payment)

Return True if:
Order status is CREATED
No payment has been made before
payment.is_successful() is True
payment.amount == get_total_amount()
Total amount > 0
Then:
Save payment
Change status to PAID
Return True
Else return False.

cancel()

Return True if:
Order status is CREATED
Then:
Change status to CANCELLED
Return True
Else return False.
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.