【发布时间】:2014-06-01 20:31:31
【问题描述】:
从事银行项目。我需要确保用户(个人对象)有他们自己的银行账户,他们可以在可以与之交互的每个账户中使用不同的余额创建这些账户。他们应该能够存款、取款、转账等。我无法正确输出。
如您所见,当它显示每个人在被调用的帐户中有多少钱时,它显示的是该用户创建的所有帐户的总数。关于如何解决此问题的任何想法?
另外,我需要一些关于我的转移方法中的 if 语句的指导。我无法让它为每个场景正确显示。
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)
这段代码的当前输出是:
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.
【问题讨论】: