Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BankAccount
- attr_reader :balance
- def initialize(balance)
- @balance = balance
- end
- def display_balance
- @balance
- end
- def deposit(amount)
- @balance += amount if amount > 0
- end
- def withdraw(amount)
- @balance -= amount if amount <= @balance
- end
- end
- class CheckingAccount < BankAccount
- attr_accessor :number_of_withdrawals
- # Define the instance constant
- MAX_FREE_WITHDRAWALS = 3
- def initialize(balance)
- # Use super to inherit all of the parent class's initialized variables
- super(balance)
- # Set the instance variable with a default number
- @total_withdrawals = 0
- end
- def get_free_withdrawal_limit
- MAX_FREE_WITHDRAWALS
- end
- def number_of_withdrawals
- @total_withdrawals
- end
- def transfer
- end
- def withdraw(amount)
- amt = amount
- if @total_withdrawals > MAX_FREE_WITHDRAWALS
- amt += 5
- end
- if amt < @balance
- # If it is, reduce the @balance by the amt
- @balance -= amt
- # Add 1 to the total_withdrawals instance variable
- total_withdrawals++
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement