We're going to build a BankAccount
class where two instances of the class can transfer money to each other through a Transfer
class.
You can't just transfer money to another account without the bank running checks first, and the Transfer
class acts as a space to check the transaction between two instances of the BankAccount
class. Transfer
will do all of this, as well as check the validity of the accounts before the transaction occurs. Transfer
should be able to reject a transaction if the accounts aren't valid or if the sender doesn't have the money.
A bank account:
- is associated with a user's
name
. - is opened with a
balance
of exactly $1000. - has a
status
, and can beopen
,closed
, orzero
. An account is initially alwaysopen
.
Note: The name on the account should not be able to be changed, but we're going to ignore that for now.
-
Build a new class called
BankAccount
and instantiate a new account for a user named "Kiran".- Confirm that Kiran's new account is of the type
BankAccount
. - Confirm that the name on Kiran's account is "Kiran".
- Confirm that Kiran's account has a balance of $1000.
- Confirm that Kiran's account is
open
. - Set Kiran's balance to $2000. Confirm his new account balance.
- Confirm that Kiran's new account is of the type
-
What other properties might a bank account need? Add three more properties to the
BankAccount
class. -
Write a new method for the
BankAccount
class calleddeposit
that adds money to the account. -
Write a new method for the
BankAccount
class calledcheck_balance
that displays a message to the user indicating their balance. -
Write a new method for the
BankAccount
class calledis_valid
that determines whether the account is valid (open and with funds) or not. The method returnstrue
if the bank account isopen
. If the account isclosed
orzero
, the method returnsfalse
.- Instantiate a new account, and set its balance to 0.
- Check that the status of the account is
zero
. - Check whether the account is valid.
- Instantiate another new account, and set its status to
closed
. - Check whether the account is valid.
-
Write a new method for the
BankAccount
class calledclose_account
that closes an account.
- Make a new bank account for "Amanda".
- Build a new class called
Transfer
that initializes with a sender, a recipient, and an amount. Then instantiate a newTransfer
that sends $50 from Amanda to Kiran.- Confirm that the transfer is of the type
Transfer
. - Confirm that the transfer had a sender named Amanda, a recipient named Kiran, and an amount of $50.
- Confirm that the transfer is of the type
- Write a new method for the
Transfer
class calledboth_valid
that confirms whether both accounts are valid.- Confirm that the transfer from Amanda to Kiran is a valid transaction.
- Confirm that the
both_valid
method uses thevalid
method on eachBankAccount
.
- Add a property to the
Transfer
class calledstatus
:- The
status
of a transfer is always initiallypending
. - A transfer's status is
complete
once it has occurred. - A transfer's status is
rejected
if the sender doesn't have sufficient funds. If this occurs, the sender gets the message: "Transaction rejected. Please check your account balance." - A transfer's status is
reversed
if money is returned from a recipient to a sender, but only for a transfer that wascomplete
.
- The
- Write a new method for the
Transfer
class calledexecute_transaction
that removes money from the sender's account, deposits that same amount of money into the recipient's account, and sets the status of the transfer tocomplete
.- Confirm that the status of the transfer is
complete
after the transaction has taken place, and that the account balances are what you would expect.
- Confirm that the status of the transfer is
- Use the
Transfer
class to send $4000 from Kiran to Amanda.- Confirm that the transfer is
rejected
and display the message "Transaction rejected. Please check your account balance."
- Confirm that the transfer is
- Write a new method for the
Transfer
class calledreverse_transfer
that reverses the transfer and sets the status toreversed
.- Confirm that a reversed transaction for $50 from Kiran back to Amanda has the status
reversed
and that the account balances are what you would expect. - Confirm that a transfer can only be reversed if it had previously been
completed
.
- Confirm that a reversed transaction for $50 from Kiran back to Amanda has the status
- Have you tried to change the name on an account? Do you find it a bit weird that we could just swap in a new name on an account? Explore and implement "getters" and "setters" in order to fix the value of a property. Then try to change the name on an account and see what happens.