
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
balanceof 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
BankAccountand 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
BankAccountclass. -
Write a new method for the
BankAccountclass calleddepositthat adds money to the account. -
Write a new method for the
BankAccountclass calledcheck_balancethat displays a message to the user indicating their balance. -
Write a new method for the
BankAccountclass calledis_validthat determines whether the account is valid (open and with funds) or not. The method returnstrueif the bank account isopen. If the account isclosedorzero, 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
BankAccountclass calledclose_accountthat closes an account.
- Make a new bank account for "Amanda".
- Build a new class called
Transferthat initializes with a sender, a recipient, and an amount. Then instantiate a newTransferthat 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
Transferclass calledboth_validthat confirms whether both accounts are valid.- Confirm that the transfer from Amanda to Kiran is a valid transaction.
- Confirm that the
both_validmethod uses theis_validmethod on eachBankAccount.
- Add a property to the
Transferclass calledstatus:- The
statusof a transfer is always initiallypending. - A transfer's status is
completeonce it has occurred. - A transfer's status is
rejectedif 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
reversedif money is returned from a recipient to a sender, but only for a transfer that wascomplete.
- The
- Write a new method for the
Transferclass calledexecute_transactionthat 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
completeafter 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
Transferclass to send $4000 from Kiran to Amanda.- Confirm that the transfer is
rejectedand display the message "Transaction rejected. Please check your account balance."
- Confirm that the transfer is
- Write a new method for the
Transferclass calledreverse_transferthat reverses the transfer and sets the status toreversed.- Confirm that a reversed transaction for $50 from Kiran back to Amanda has the status
reversedand 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.