In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of January 18th
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #12 exercise sheet]
In this problem you should submit a class as described. Inside the class do not print anything that was not asked!
Now that IP students have learned the basics of programming, they can become filthy rich 😁 At least if they code the bank application!
Create a BankAccount class that has the following attributes: account_holder
(string) and balance
(number).
You should start by creating the __init__
initializer to be able to create a new account with the account_holder
name and an optional initial balance, balance
.
By default, the balance
should be initialized to 0.0
.
Then, add some methods to the BankAccount
class that allow you to perform certain operations on any bank account:
check_balance
: Returns the current account balance
.deposit
: Takes a certain amount
and adds that specified amount to the account balance
. Assume that the deposit amount
is always positive.withdraw
: Takes a certain amount
and deducts the specified amount from the account balance
if there are sufficient funds."Invalid Operation: insufficient funds."
and the current balance
should remain unchanged. Assume that the withdraw amount
is always positive.transfer
: Allows to transfer money between two accounts if there are sufficient funds. Assume that two valid accounts are given. "Invalid Operation: insufficient funds."
and the current balance
of both accounts should remain unchanged.The following constraints are guaranteed in all the test cases that will be given to your program:
account_holder
will always be given, but the initial balance
might not be. balance
should be 0.0
by default (check the Examples section).amount
values given to the deposit
, withdraw
and transfer
methods will always be positive numbers (they can be integers or floats).withdraw
or transfer
from it.Example Input 1 | Example Output 1 |
account_1 = BankAccount("Joy", 3000) print(account_1.account_holder) print(account_1.balance) account_2 = BankAccount("James") print(account_2.account_holder) print(account_2.balance) |
Joy 3000 James 0.0 |
Example Input 2 | Example Output 2 |
account_1 = BankAccount("Joy", 3000) print(account_1.check_balance()) |
3000 |
Example Input 3 | Example Output 3 |
account_1 = BankAccount("Joy", 3000) print(account_1.check_balance()) account_1.deposit(5000) print(account_1.check_balance()) |
3000 8000 |
Example Input 4 | Example Output 4 |
account_1 = BankAccount("Joy", 3000) print(account_1.check_balance()) account_1.withdraw(2000) print(account_1.check_balance()) |
3000 1000 |
Example Input 5 | Example Output 5 |
account_1 = BankAccount("Joy", 3000) print(account_1.check_balance()) account_1.withdraw(5000) print(account_1.check_balance()) |
3000 Invalid Operation: insufficient funds. 3000 |
Example Input 6 | Example Output 6 |
account_1 = BankAccount("Joy", 3000) account_2 = BankAccount("James") print(account_1.check_balance()) print(account_2.check_balance()) account_1.transfer(account_2, 600) print(account_1.check_balance()) print(account_2.check_balance()) |
3000 0.0 2400 600.0 |
Example Input 7 | Example Output 7 |
account_1 = BankAccount("Joy", 3000) account_2 = BankAccount("James") print(account_1.check_balance()) print(account_2.check_balance()) account_1.transfer(account_2, 5000) print(account_1.check_balance()) print(account_2.check_balance()) |
3000 0.0 Invalid Operation: insufficient funds. 3000 0.0 |