Skip to content

Instantly share code, notes, and snippets.

@GermanHoyos
Created February 26, 2018 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GermanHoyos/a12461dcae1df1c166a98a7666a24bd6 to your computer and use it in GitHub Desktop.
Save GermanHoyos/a12461dcae1df1c166a98a7666a24bd6 to your computer and use it in GitHub Desktop.
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