At the Counter

Now the bank opens its counter. The new array today holds today's transactions, in the order in which they happened. Each one is a Transaction: a deposit ("deposit") or a withdrawal ("withdraw") of an amount of gold, by the owner of an account.

A deposit adds the gold to the account. A withdrawal takes it away, but only if the account has enough: otherwise, it fails with not enough gold, and the account stays as it was. A transaction by someone who has no account fails with no account. After the accounts in the morning, print a line for each transaction, and then the accounts and the bank's gold once more, as they are in the evening:

Kira: 120 gold
Bruno: 40 gold
Lena: 75 gold
The bank holds 235 gold.
Bruno deposits 30 gold. Balance: 70
Kira withdraws 50 gold. Balance: 70
Bruno can't withdraw 100 gold: not enough gold
Mira can't deposit 10 gold: no account
Kira: 70 gold
Bruno: 70 gold
Lena: 75 gold
The bank holds 215 gold.

A tip: something that can fail is a job for the type Result<T> from the Generics lesson. A method that withdraws gold can return a Result<number>: the new balance, or the reason why it failed.

Instructions

Print a line for each transaction, as in the example.

After the transactions, print the accounts and the bank's gold again.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

2/3