Created
February 26, 2018 21:17
-
-
Save GermanHoyos/a12461dcae1df1c166a98a7666a24bd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BankAccount | |
attr_reader :balance, :number_of_withdrawals | |
def initialize(balance) #constructor | |
@balance = balance | |
@number_of_withdrawals = 0 | |
end | |
def display_balance | |
@balance | |
end | |
def deposit(amount) | |
if(amount > 0) | |
@balance += amount | |
else | |
'Cannot deposit negative amounts or 0' | |
end | |
end | |
def withdraw(amount) #setter | |
if (amount <= @balance) | |
@balance -= amount | |
@number_of_withdrawals += 1 | |
else | |
'Insufficient funds' | |
end | |
end | |
end | |
class CheckingAccount < BankAccount | |
attr_reader :number_of_withdrawals | |
MAX_FREE_WITHDRAWALS = 3 | |
def get_free_withdrawal_limit | |
MAX_FREE_WITHDRAWALS | |
end | |
def initialize(balance) | |
super(balance) # calls the parent initialize constructor and variable | |
@number_of_withdrawals = 0 | |
end | |
def deposit | |
super | |
end | |
def withdraw(amount) | |
super | |
if (@number_of_withdrawals > MAX_FREE_WITHDRAWALS) | |
@balance -= 5 | |
end | |
end | |
def transfer(account, amount) | |
self.withdraw(amount) | |
account.deposit(amount) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment