Exercise: Encapsulation
This exercise puts the encapsulation lesson into code.
Scenario
Model a bank account whose balance is private and starts at zero. Callers never touch the balance directly — every change goes through a method that validates the request first. The account is driven by a list of commands, and each command produces exactly one output line.
Commands
The program reads one command per line and prints one line of output per command.
| Command | Behavior | Output |
|---|---|---|
["deposit", amount] | Add amount to the balance | "Balance: <bal>", or "Invalid amount" if amount <= 0 |
["withdraw", amount] | Subtract amount from the balance | "Balance: <bal>", or "Invalid amount" if amount <= 0, or "Insufficient funds" if amount exceeds the balance |
["balance"] | Report the current balance | "Balance: <bal>" |
Amounts are integers. The balance never goes negative, because withdraw rejects any amount it
cannot cover.
7 deposit 100 deposit -50 withdraw 30 balance withdraw 200 withdraw 70 balance
Balance: 100 Invalid amount Balance: 70 Balance: 70 Insufficient funds Balance: 0 Balance: 0
Your task
Implement the skeleton in the editor below so the commands produce the output shown in the example above.