Spring 2010

Lab07
Exceptional BankAccounts

Due: Tuesday April 20, 2010 11:59 PM

Lab07 - Exceptional BankAccounts

Requirements

InsufficientFundsError class created20 pts
NegativeAmountError class created20 pts
Exception raised when negative amount is given10 pts
Exception raised when there is insufficient funds10 pts
NegativeAmountError is rescued10 pts
InsufficientFundsError is rescued10 pts
Output from NegativeAmountError is correct10 pts
Output from InsufficientFundsError is correct10 pts
Total100 pts

Resources

Objective

To practice working with Ruby exceptions

Instructions

For this assignment you will need to improve on the BankAccount program. To do this you will need to create two custom exception classes called InsufficientFundsError and NegativeAmountError which will represent those two conditions.

The BankAccount class should be modified to raise these exceptions when the corresponding error occurs within the class. (i.e. when the user tried to withdraw more than is in the account the InsufficientFundsError should be raised)

The "driver" part of the program that runs the menu system and allows the user to input choices will also need to be modified to rescue these exceptions. When either of these exceptions is rescued in the driver it should puts the exception, displaying the return from it's to_s method to the console.

The to_s method of the InsufficientFundsError class should return a string like:

"Account [id] has insufficient funds ($[balance]) to allow the withdrawal of $[amount]"

where [id] is the account number of the account that was being acted upon, [balance] is the balance of that account, and [amount] is the amount the user tried to withdraw.

The to_s method of the NegativeAmountError class should return a string like:

"You cannot [action] a negative amount for account [id]"

where [action] is the action the user was taking on the account (deposit or withdraw), and [id] is the id number of the account that was being acted upon.