Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Exercise (3 points)
- a) Using the slides & the script, put together a file containing the complete
- class representing a bank account. The class should define the following
- methods:
- * __init__
- * withdraw
- * deposit
- * set_holder
- * __str__
- b) Extend the withdraw function such that the minimum balance allowed is -1000.
- c) Write a function apply_interest(self) which applies an interest rate of
- 1.5% to the current balance and call it on your objects. Make sure that
- the balance is applied only if the balance is not negative.
- '''
- class Account:
- def __init__(self, number, holder):
- self.number = number
- self.holder = holder
- self.balance = 0
- def deposit(self, amount):
- self.balance += amount
- def withdraw(self, amount):
- if self.balance < amount:
- amount = self.balance
- self.balance -= amount
- return amount
- def set_holder(self, holder):
- #
- if __name__ == "__main__":
- print("Welcome to the Python Bank!")
- print("Create an account for John:")
- johnsAccount = Account(1, "John Doe")
- print("Create an account for Jane:")
- janesAccount = Account(2, "Jane Doe")
- print("John deposits $2000")
- johnsAccount.deposit(2000)
- print(johnsAccount)
- print("Jane deposits $500")
- janesAccount.deposit(500)
- print(janesAccount)
- print("Apply interest:")
- johnsAccount.apply_interest()
- print(johnsAccount)
- janesAccount.apply_interest()
- print(janesAccount)
- print("Set holder of John's account to Joe Average")
- johnsAccount.set_holder("Joe Average")
- print(johnsAccount)
- print("Joe withdraws $2500")
- print("Joe gets:", johnsAccount.withdraw(2500))
- print(johnsAccount)
- print("Jane withdraws $2500")
- print("Jane gets:", janesAccount.withdraw(2500))
- print(janesAccount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement