CJCoding With Joseph
15free / daySign inCreate account

Account with Three Custom Exceptions

Build a small bank account and the three exceptions it throws, from scratch:

- InvalidAmountError, InsufficientFundsError and SameAccountError each inherit from std::runtime_error, with an explicit constructor that takes a const std::string& message and passes it on - Account(const std::string& owner, double balance), with const std::string& owner() const and double balance() const - void deposit(double amount): an amount of 0 or less throws InvalidAmountError("amount must be positive"); otherwise it is added - void withdraw(double amount): an amount of 0 or less throws the same InvalidAmountError; an amount above the balance throws InsufficientFundsError with the text insufficient funds for and the owner; otherwise it is subtracted - void transferTo(Account& other, double amount): when other is this very account, throw SameAccountError("cannot transfer to the same account"); otherwise withdraw the amount from this account and deposit it into other, so the checks of those two operations apply and nothing changes when one of them throws

The tests create two accounts, run several transfers and deposits, and print transferred, or a label (same account: , funds: , amount: or deposit: ) followed by what(); balances are printed by the tests as the owner, : and the balance with two decimals.

Do NOT write a main function.

Expected Output:

transferred
Ada: 70.00
Lin: 50.00
funds: insufficient funds for Lin
Lin: 50.00
same account: cannot transfer to the same account
Ada: 70.00
amount: amount must be positive
deposit: amount must be positive
Lin: 55.00

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Exceptions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.