Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. """Simple bank."""
  2.  
  3.  
  4. class Account:
  5. """Represent a bank account."""
  6.  
  7. def __init__(self, name, balance):
  8. """
  9. Class constructor. Each account has owner's name and starting balance.
  10.  
  11. :param name: account owner name. String.
  12. :param balance: starting balance of account. Integer.
  13. """
  14. self.name = str(name)
  15. self.balance = int(balance)
  16.  
  17. def withdraw(self, amount):
  18. """
  19. Withdraw money from account.
  20.  
  21. :param amount: amount to withdraw from account, has to be positive
  22. and the balance can't go below 0.
  23. """
  24. if self.balance - amount < 0:
  25. self.balance = 0
  26. elif amount < 0:
  27. self.balance = self.balance
  28. else:
  29. self.balance = self.balance - amount
  30.  
  31. def deposit(self, amount):
  32. """
  33. Deposit money to account.
  34.  
  35. :param amount: amount to deposit to account, has to be positive
  36. """
  37. if amount < 0:
  38. return self.balance
  39. else:
  40. self.balance = self.balance + amount
  41.  
  42. def get_balance(self):
  43. """
  44. Get account balance.
  45.  
  46. :return: balance in double form
  47. """
  48. return self.balance
  49.  
  50. def get_name(self):
  51. """
  52. Get account owner name.
  53.  
  54. :return: owner name in string form
  55. """
  56. return self.name
  57.  
  58.  
  59. # Creating the Account class instance and giving it name, starting balance
  60. paul_account = Account("Paul", 100.00)
  61. jakob_account = Account("Jakob", 500.00)
  62.  
  63. print("Initial balance")
  64. print(paul_account.get_balance()) # 100.0
  65. print(jakob_account.get_balance()) # 500.0
  66.  
  67. assert paul_account.get_balance() == 100
  68. assert jakob_account.get_balance() == 500
  69.  
  70. jakob_account.withdraw(250.00)
  71. assert jakob_account.get_balance() == 250
  72. print("Jakob's balance is now ", jakob_account.get_balance()) # Jakob's balance is now 250.0
  73. paul_account.deposit(250.00)
  74. assert paul_account.get_balance() == 350
  75. print("Paul's balance is now", paul_account.get_balance()) # Paul's balance is now 350.0
  76.  
  77. print("Final state")
  78. print(paul_account.get_balance()) # 350.0
  79. print(jakob_account.get_balance()) # 250.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement