Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class BankAccount
  2. attr_reader :balance
  3.  
  4. def initialize(balance)
  5. @balance = balance
  6. end
  7.  
  8. def display_balance
  9. @balance
  10. end
  11.  
  12. def deposit(amount)
  13. @balance += amount if amount > 0
  14. end
  15.  
  16. def withdraw(amount)
  17. @balance -= amount if amount <= @balance
  18. end
  19. end
  20.  
  21. class CheckingAccount < BankAccount
  22. attr_accessor :number_of_withdrawals
  23.  
  24. # Define the instance constant
  25. MAX_FREE_WITHDRAWALS = 3
  26.  
  27. def initialize(balance)
  28. # Use super to inherit all of the parent class's initialized variables
  29. super(balance)
  30.  
  31. # Set the instance variable with a default number
  32. @total_withdrawals = 0
  33. end
  34.  
  35. def get_free_withdrawal_limit
  36. MAX_FREE_WITHDRAWALS
  37. end
  38.  
  39. def number_of_withdrawals
  40. @total_withdrawals
  41. end
  42.  
  43. def transfer
  44.  
  45. end
  46.  
  47. def withdraw(amount)
  48. amt = amount
  49. if @total_withdrawals > MAX_FREE_WITHDRAWALS
  50. amt += 5
  51. end
  52. if amt < @balance
  53. # If it is, reduce the @balance by the amt
  54. @balance -= amt
  55. # Add 1 to the total_withdrawals instance variable
  56. total_withdrawals++
  57.  
  58. end
  59.  
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement