Loading...
Loading...
A small bank opens accounts numbered 1, 2, 3… in order and handles deposits, withdrawals, balance checks and transfers. A balance can never go below 0.
Write a class Account (fields number, holder, balance; methods deposit and boolean withdraw) and a class Bank with an Account[] field, a count field, Account open(String holder, long amount) and Account find(int number) that returns null for an unknown number.
open NAME amount, deposit k x, withdraw k x, balance k or transfer from to x.OPENED k for open; BALANCE b after a deposit or withdrawal; k NAME b for balance; TRANSFERRED x for a transfer. On failure: NO ACCOUNT (any unknown number), SAME ACCOUNT (from equals to), or INSUFFICIENT FUNDS.intExample
Input
6 open Asha 1000 open Ravi 0 transfer 1 2 400 withdraw 2 500 balance 1 balance 2
Output
OPENED 1 OPENED 2 TRANSFERRED 400 INSUFFICIENT FUNDS 1 Asha 600 2 Ravi 400
Ravi has only 400 after the transfer, so withdrawing 500 fails.
Try the problem first. Open one hint at a time.
After Bank bank = new Bank(); set bank.accounts = new Account[q]: there can never be more accounts than commands.
Account k is stored at index k - 1; find checks 1 ≤ k ≤ count first.
For a transfer, find both accounts, check null and from == to, then withdraw from one and deposit into the other.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor