Account with Three Custom Exceptions
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.
Your Output
Run your code to see the output here...