Skip to content

Instantly share code, notes, and snippets.

@aspencer8111
Last active May 8, 2017 14:49
Show Gist options
  • Save aspencer8111/a13aa8ae319ef32beca30a1596e6e1b9 to your computer and use it in GitHub Desktop.
Save aspencer8111/a13aa8ae319ef32beca30a1596e6e1b9 to your computer and use it in GitHub Desktop.
Simple solution to Bloc's bank account exercise
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_reader :number_of_withdrawals
MAX_FREE_WITHDRAWALS = 3
WITHDRAWAL_FEE = 5
def initialize(balance)
super(balance)
@number_of_withdrawals = 0
end
def get_free_withdrawal_limit
MAX_FREE_WITHDRAWALS
end
def transfer(account, amount)
if account.balance >= amount
deposit(amount)
account.withdraw(amount)
end
end
def withdraw(amount)
super(amount)
@number_of_withdrawals += 1
@balance -= WITHDRAWAL_FEE if number_of_withdrawals > 3
if @balance < 0
deposit(amount)
deposit(WITHDRAWAL_FEE)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment