Design an expense sharing app. Friends add expenses to a group, say who paid and how it is divided, and the app keeps track of who owes whom. It can also show the smallest set of payments that settles everything.
Model users, groups, expenses and each person's share.
We have the idea of shared costs but no objects for them. A `Group` has members (`User`s) and expenses. An `Expense` records who paid, the total and a list of `Split`s. Each `Split` says how much one person owes for that expense. The split list is what makes the model work: once every expense has splits, balances are just addition. Amounts are whole numbers (paise), so nothing is lost to rounding. Storing splits on every expense uses more space than a formula would, but it keeps old expenses fixed even if rules change later.
Support equal, exact and percentage splits without editing existing code.
Every split type has its own maths and its own validation. Putting them all in `Expense` would mean a branch per type. A `SplitStrategy` interface turns an amount and the participants into a list of `Split`s. `EqualSplit`, `ExactSplit` and `PercentSplit` each implement it and validate their own input. This is **Strategy**: the expense does not care how shares were worked out. Adding 'by shares' (2 shares, 1 share) is one new class. Equal splits of an amount that does not divide evenly leave a remainder. Decide the rule once (give the extra paisa to the first people) so every client agrees.
Update who owes whom as expenses arrive, and give the UI one simple entry point.
To know who owes what we would re-add every expense each time someone opens the app. A `BalanceSheet` keeps each user's **net** balance (positive means they are owed). `apply(expense)` adds the payer's credit and subtracts each person's share. `ExpenseService` is the single entry point: it builds the expense with a strategy, adds it to the group and updates the sheet. A net balance per person is enough to answer 'who owes what'. Updating it as expenses arrive makes reads instant. `ExpenseService` is a **Facade**, so the UI never touches splits or sheets directly. A running balance can drift if an old expense is edited or deleted. Either reverse and re-apply the expense, or rebuild the sheet from the expenses when in doubt.
Settle everything in as few transfers as possible.
If A owes B and B owes C, paying around the circle wastes transfers. People want the smallest list of payments that settles everyone. A `DebtSimplifier` takes the net balances and repeatedly matches the person who owes the most with the person who is owed the most. Each match becomes a `Transaction` (from, to, amount) and removes at least one person from the problem. Only the **net** balance matters, not who owed whom originally, so the result never has more than n-1 transfers for n people. Matching biggest with biggest is fast and gives a good, easy-to-explain answer. Finding the true minimum number of transfers is a harder (NP-hard) problem. Greedy matching is the practical choice and what real apps use.