Digital Wallet
A minimal money-movement core: provision accounts, credit and debit them, and transfer between two accounts atomically.
Requirements
- Accounts are provisioned explicitly with
createAccount(initial balance 0); a duplicatecreateAccountis ignored and never resets an existing balance. credit,debit, andtransferact only on accounts that already exist. Crediting or debiting an unknown user has no effect / fails, andbalanceof an unknown user is 0.debitsucceeds only when the balance is at leastamt, then subtracts it.transfersucceeds only when both accounts exist and the sender holds at leastamt; it then moves the funds atomically — a rejected transfer leaves both balances exactly as they were (no partial debit).- All amounts are positive integers — think of the smallest currency unit (paise/cents), so there is no rounding.
API to implement
Implement the class Wallet. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| createAccount <user> | — | Open an account for user with balance 0. A duplicate call for an existing user is ignored (it never resets the balance). |
| credit <user> <amt> | — | Add amt to user's balance. No-op if the account doesn't exist. |
| debit <user> <amt> | boolean | Subtract amt from user's balance. Returns true only if the account exists and holds at least amt (then subtracts); otherwise false. |
| transfer <fromUser> <toUser> <amt> | boolean | Move amt from fromUser to toUser. Returns true only if both accounts exist and fromUser holds at least amt — then moves the funds atomically. Otherwise false and nothing changes. |
| balance <user> | int | Current balance of user. Returns 0 for an unknown user. |
Input format
The first line contains Q, the number of operations. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
5
createAccount alice
credit alice 100
balance alice
debit alice 40
balance alice100
true
608
createAccount a
createAccount b
credit a 100
transfer a b 30
balance a
balance b
transfer a b 200
balance atrue
70
30
false
70