Working on a bank program. I need to make sure that users (person objects) have their own bank accounts that they can create with different balances in each account that they can interact with. They should be able to deposit, withdraw, transfer money, etc. I'm having trouble outputting correctly.
As you will see, when it says how much money each person has in their account which is being called, it's displaying the total from all accounts created by that user. Any ideas on how I can fix this?
Also, I need a little guidance on the if statement in my transfer method. I can't get it to display correctly for each scenario.
class Bank
attr_accessor :balance, :withdrawal, :deposit, :transfer, :users
@@accounts = {}
#@@balance = {}
def initialize(bname)
@bname = bname
@users = {}
@@accounts[users] = bname
#@@balance[users] = bname
puts "#{@bname} bank was just created."
end
def open_account(name, bname, balance = 0)
if @users.include?(name)
bname.each do |users|
@@accounts.push('users')
end
end
@balance = balance
puts "#{name}, thanks for opening an account at #{@bname} with an initial $#{balance} deposit!"
end
def user
@users
end
def withdrawal(name, amount)
@balance -= amount
puts "#{name} withdrew $#{amount} from #{@bname}. #{name} has #{@balance}. #{name}'s account has #{@balance}."
end
def deposit(name, amount)
@balance += amount
puts "#{name} deposited $#{amount} to #{@bname}. #{name} has #{@balance}. #{name}'s account has #{@balance}."
end
def transfer(name, account2, amount)
if name == name
@balance -= amount
@transfer = amount
puts "#{name} transfered $#{amount} from #{@bname} account to #{account2} account. The #{@bname} account has $#{amount} and the #{account2} account has $#{@balance}."
else
puts "That account doesn't exist."
end
end
end
class Person
attr_accessor :name, :cash
def initialize(name, cash = 100)
@name = name
@cash = cash
puts "Hi, #{name}. You have $#{cash} on hand!"
end
end
chase = Bank.new("JP Morgan Chase")
wells_fargo = Bank.new("Wells Fargo")
randy = Person.new("Randy", 1000)
kristen = Person.new("Kristen", 5000)
justin = Person.new("Justin", 1500)
chase.open_account('Randy', "JP Morgan Chase", 200)
chase.open_account('Kristen', "JP Morgan Chase", 300)
chase.open_account('Justin', "JP Morgan Chase", 400)
wells_fargo.open_account('Randy', "Wells Fargo", 200)
wells_fargo.open_account('Kristen', "Wells Fargo", 300)
chase.deposit("Randy", 200)
chase.deposit("Kristen", 350)
chase.withdrawal("Kristen", 500)
chase.transfer("Randy", "Wells fargo", 100)
chase.deposit("Randy", 150)
The current output for this code is:
JP Morgan Chase bank was just created.
Wells Fargo bank was just created.
Hi, Randy. You have $1000 on hand!
Hi, Kristen. You have $5000 on hand!
Hi, Justin. You have $1500 on hand!
Randy, thanks for opening an account at JP Morgan Chase with an initial $200 deposit!
Kristen, thanks for opening an account at JP Morgan Chase with an initial $300 deposit!
Justin, thanks for opening an account at JP Morgan Chase with an initial $400 deposit!
Randy, thanks for opening an account at Wells Fargo with an initial $200 deposit!
Kristen, thanks for opening an account at Wells Fargo with an initial $300 deposit!
Randy deposited $200 to JP Morgan Chase. Randy has 600. Randy's account has 600.
Kristen deposited $350 to JP Morgan Chase. Kristen has 950. Kristen's account has 950.
Kristen withdrew $500 from JP Morgan Chase. Kristen has 450. Kristen's account has 450.
Randy transfered $100 from JP Morgan Chase account to Wells fargo account. The JP Morgan Chase account has $100 and the Wells fargo account has $350.
Randy deposited $150 to JP Morgan Chase. Randy has 500. Randy's account has 500.