Threesuns

Email

Contact

All Semesters

Semesters

All Courses

Courses

Ruby

CSYS2853

3 credits

Labs

Exceptional BankAccounts

Lab

100 points

Requirements

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

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.