Design a Billing System (LLD Variant)

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-02-27

Problem Statement

Design the Low-Level Design (LLD) of a Billing System used by a retail store (like a supermarket, e-commerce platform, or POS terminal).


The system should support itemized billing, discounts, tax calculation, and multiple payment modes, while keeping the design modular, extensible, and easy to maintain.


The focus is on designing classes, interactions, and responsibilities, not UI or database aspects.


🎯 Goal


Design a Billing System that can:


•Add items to a bill (with quantity and unit price).
•Apply applicable discounts (percentage, flat, or promotional offers).
•Calculate total amount with taxes.
•Handle different payment methods (cash, card, wallet, etc.).
•Generate and print/display the final invoice.


✅ Functional Requirements


1. Cart and Items


•A Bill consists of multiple Items.
•Each item has:
•id, name, unitPrice, quantity, category
•Total item price = unitPrice × quantity


2. Discount Handling


•Discounts can be:
•Flat amount (e.g., ₹100 off)
•Percentage (e.g., 10% off)
•Promotional (e.g., Buy 1 Get 1 Free)
•Discounts may apply:
•On specific items
•On entire bill
•On specific customer segments (optional)


3. Tax Calculation


•Each item may have a tax rate depending on category:

Example:

•Grocery — 5%
•Electronics — 18%
•Tax is calculated on the post-discount price of the item.


4. Billing and Payment


•After adding items, system should:

Compute:

•Subtotal (sum of all item prices)
•Discount total
•Tax total
•Final payable amount
•Support multiple payment methods:
•Cash
•Credit/Debit Card
•UPI/Wallet
•Split payments (optional)
•Once payment is done → mark bill as PAID


5. Invoice Generation


•Generate a detailed invoice showing:
•Item details (name, qty, price)
•Applied discounts and taxes
•Total amount
•Payment details
•Support print or digital format (PDF/Email) (optional)


⚙️ Non-Functional Requirements


•Extensible: Should easily add new discount types or tax rules.
•Accurate: Ensure financial rounding and precision.
•Maintainable: Follow SRP (Single Responsibility Principle).
•Thread-safe: For concurrent billing counters (if extended).


🧱 Key Classes to Design


ClassResponsibility


Item


Represents a product with name, price, quantity, and tax category.


Tax


Represents tax percentage and logic to compute tax per item.


Discount


Base class/interface for different discount types.


BillItem


Wraps an Item with applied discounts and computed totals.


Bill


Represents the overall bill — list of items, discounts, totals, payment info.


Payment


Handles payment processing for a bill.


InvoiceGenerator


Creates the printable/digital bill summary.


BillingService / BillCalculator


Applies discounts, taxes, and computes final total.


🧩 Suggested Class Structure

1 2Bill 3 ├── List<BillItem> billItems 4 ├── double subTotal 5 ├── double discountTotal 6 ├── double taxTotal 7 ├── double grandTotal 8 ├── Payment payment 9 ├── BillStatus status (PENDING, PAID, CANCELLED) 10 11BillItem 12 ├── Item item 13 ├── double quantity 14 ├── double discountedPrice 15 ├── double taxAmount 16 ├── double totalPrice 17 18Item 19 ├── String id 20 ├── String name 21 ├── double unitPrice 22 ├── Category category 23 24Discount (abstract) 25 ├── double apply(Bill bill) 26 27FlatDiscount, PercentageDiscount, BOGODiscount 28 ├── override apply() with specific rules 29 30Tax 31 ├── double rate 32 ├── double calculate(double amount) 33 34Payment 35 ├── PaymentType type (CASH, CARD, WALLET) 36 ├── double amountPaid 37 ├── PaymentStatus status (SUCCESS, FAILED) 38 39InvoiceGenerator 40 ├── String generateInvoice(Bill bill)


🔄 Flow Example


1.Add Items to Bill:
•2× Milk @ ₹50 → ₹100
•1× Bread @ ₹30 → ₹30

Subtotal = ₹130

1.Apply 10% discount:
•Discount = ₹13

After discount = ₹117

1.Apply tax (5% GST):
•Tax = ₹5.85

Total = ₹122.85

1.Process payment:
•Payment via UPI — ₹122.85
2.Generate Invoice:
1 2---------------------------- 3SuperMart Bill 4Items: 5Milk (2) ₹100 6Bread (1) ₹30 7Discount -₹13 8Tax (5%) ₹5.85 9---------------------------- 10Total: ₹122.85 11Payment: UPI (Success) 12----------------------------


🧠 Design Patterns


•Strategy Pattern: For different discount/tax calculation strategies.
•Factory Pattern: For creating payment methods.
•Decorator Pattern: For adding tax/discount layers dynamically.
•Builder Pattern: For step-by-step bill construction.


⚡ Possible Extensions


•Add loyalty points or coupons.
•Add refunds and cancellations.
•Add multi-currency support.
•Integrate with inventory management system.
•Add audit logging for compliance.


✅ Final Summary:


Design a Billing System (LLD) that:


•Allows itemized billing with discounts and taxes.
•Supports multiple payment types.
•Generates a detailed invoice.
•Follows modular and extensible object-oriented design principles.


Solution

Comments