Deviopath › TypeScript

Bank Ledger with Rollback

Hard · Classes

Implement the Ledger class. An account is created by its first successful deposit or incoming transfer (a balance can be 0).

- deposit(account, amount): fails if amount <= 0.

- withdraw(account, amount): fails if amount <= 0, the account is missing or its balance is too small.

- transfer(from, to, amount): fails if from === to or the withdrawal would fail.

- begin() opens a (possibly nested) transaction. commit() closes the innermost one and keeps its changes; rollback() closes it and restores all balances and accounts to how they were at its begin(). Both fail if none is open.

- balances() returns a copy of the current balances.

Methods return true on success, false on failure (a failed call changes nothing). The driver runLedger(ops) returns the final balances and the indexes of failed operations.

Next: Inventory Commands with Undo