Skip to content

Instantly share code, notes, and snippets.

@spenserfiller
Created September 9, 2014 23:29
Show Gist options
  • Select an option

  • Save spenserfiller/da925e4577f3560d61d3 to your computer and use it in GitHub Desktop.

Select an option

Save spenserfiller/da925e4577f3560d61d3 to your computer and use it in GitHub Desktop.
MakerSquare coding challenge
class Person
attr_accessor :name
attr_accessor :cash
def initialize(name, cash)
@name = name
@cash = cash
puts "Hi, #{@name}. You have #{@cash}!"
end
end
class Bank
attr_accessor :accounts
attr_accessor :name
def initialize(name)
@accounts = Hash.new(0)
@name = name
puts "#{@name} was just created."
end
def open_account(person)
@accounts[person.name] = 0
puts "#{person.name}, thanks for opening an account at #{@name}!"
end
def withdraw(account, amount)
if @accounts[account.name] > amount
@accounts[account.name] -= amount
account.cash += amount
puts "#{account.name} withdrew $#{amount} from #{@name}. #{account.name} has #{account.cash}. #{account.name}'s account has $#{@accounts[account.name]}"
else puts "#{account.name} does not have enough money in the account to withdraw $#{amount}."
end
end
def deposit(account, amount)
if account.cash >= amount
@accounts[account.name] += amount
account.cash -= amount
puts "#{account.name} desposited $#{amount} to #{@name}. #{account.name} has $#{account.cash}. #{account.name}'s account has $#{@accounts[account.name]}"
else puts "#{account.name} does not have enough cash to deposit $#{amount}."
end
end
def transfer(account, bank, amount)
if @accounts[account.name] > amount
self.withdraw(account, amount)
bank.deposit(account, amount)
puts "#{account.name} transfered $#{amount} from the #{@name} account to the #{bank.name} account. The #{@name} account has $#{@accounts[account.name]} and the #{bank.name} account has $#{bank.accounts[account.name]}."
else puts "Not enough money!"
end
end
def total_cash_in_bank
total = 0
@accounts.each_value { |value| total += value }
"#{self.name} has $#{total} in the bank"
end
end
# Test code:
chase = Bank.new("JP Morgan Chase")
wells_fargo = Bank.new("Wells Fargo")
me = Person.new("Shehzan", 500)
friend1 = Person.new("John", 1000)
chase.open_account(me)
chase.open_account(friend1)
wells_fargo.open_account(me)
wells_fargo.open_account(friend1)
chase.deposit(me, 200)
chase.deposit(friend1, 300)
chase.withdraw(me, 50)
chase.transfer(me, wells_fargo, 100)
chase.deposit(me, 5000)
chase.withdraw(me, 5000)
puts chase.total_cash_in_bank
puts wells_fargo.total_cash_in_bank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment