Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. '''
  2. Exercise (3 points)
  3.  
  4. a) Using the slides & the script, put together a file containing the complete
  5. class representing a bank account. The class should define the following
  6. methods:
  7.  
  8. * __init__
  9. * withdraw
  10. * deposit
  11. * set_holder
  12. * __str__
  13.  
  14. b) Extend the withdraw function such that the minimum balance allowed is -1000.
  15.  
  16. c) Write a function apply_interest(self) which applies an interest rate of
  17. 1.5% to the current balance and call it on your objects. Make sure that
  18. the balance is applied only if the balance is not negative.
  19. '''
  20.  
  21. class Account:
  22. def __init__(self, number, holder):
  23. self.number = number
  24. self.holder = holder
  25. self.balance = 0
  26.  
  27. def deposit(self, amount):
  28. self.balance += amount
  29.  
  30. def withdraw(self, amount):
  31. if self.balance < amount:
  32. amount = self.balance
  33. self.balance -= amount
  34. return amount
  35.  
  36. def set_holder(self, holder):
  37.  
  38.  
  39.  
  40.  
  41.  
  42. #
  43.  
  44. if __name__ == "__main__":
  45. print("Welcome to the Python Bank!")
  46.  
  47. print("Create an account for John:")
  48. johnsAccount = Account(1, "John Doe")
  49.  
  50. print("Create an account for Jane:")
  51. janesAccount = Account(2, "Jane Doe")
  52.  
  53. print("John deposits $2000")
  54. johnsAccount.deposit(2000)
  55. print(johnsAccount)
  56.  
  57. print("Jane deposits $500")
  58. janesAccount.deposit(500)
  59. print(janesAccount)
  60.  
  61. print("Apply interest:")
  62. johnsAccount.apply_interest()
  63. print(johnsAccount)
  64. janesAccount.apply_interest()
  65. print(janesAccount)
  66.  
  67. print("Set holder of John's account to Joe Average")
  68. johnsAccount.set_holder("Joe Average")
  69. print(johnsAccount)
  70.  
  71. print("Joe withdraws $2500")
  72. print("Joe gets:", johnsAccount.withdraw(2500))
  73. print(johnsAccount)
  74.  
  75. print("Jane withdraws $2500")
  76. print("Jane gets:", janesAccount.withdraw(2500))
  77. print(janesAccount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement