Étude 9-2: Logging Errors

Write a module named bank that contains a function account/1. The function takes a numeric Balance, which gives the current balance in the account in imaginary dollars.

The function will repeatedly ask for a transaction (deposit, withdraw, balance inquiry, or quit). If a deposit or withdrawal, it asks for the amount to deposit or withdraw, and then does that transaction. If a deposit is more than $10,000, the deposit may be subject to hold.

Provide output to the customer, and also use error_logger to write to a log file (which, in this case, will go to your terminal). Choose any form of input prompts and feedback and logging messages that you deisre. Handle the following situtations:

  • Deposits and withdrawals cannot be negative numbers (error)
  • Deposits of $10,000 or more might be subject to hold (warning)
  • All other transactions are successful (informational)

Use get_number/1 from Étude 5-1 to allow either integer or float input.

Here is sample output. Due to Erlang’s asynchronous nature, the user prompts and logging are often interleaved in the most inconvenient places.

1> c(bank).
{ok,bank}
2> bank:account(2000).
D)eposit, W)ithdraw, B)alance, Q)uit: D
Amount to deposit: 300
Your new balance is 2300
D)eposit, W)ithdraw, B)alance, Q)uit:
=INFO REPORT==== 26-Jan-2013::06:42:52 ===
Successful deposit 300
W
Amount to withdraw: -200
Withdrawals may not be less than zero.
=ERROR REPORT==== 26-Jan-2013::06:42:56 ===
Negative withdrawal amount -200
D)eposit, W)ithdraw, ...

Get Études for Erlang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.