Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 请记住,考虑到您提供的解决方案,是否有可能得到相同的结果,或者您是否建议我调整我的原始代码,以尝试更像您提供的代码?就个人而言,我认为严格限制您可以使用的类的数量是愚蠢的。如果这是我遇到的问题,我给出的解决方案就是我将使用的解决方案,它包含4个类。但是,如_Arrays_Ruby_Hash_Accounts - Fatal编程技术网

Arrays 请记住,考虑到您提供的解决方案,是否有可能得到相同的结果,或者您是否建议我调整我的原始代码,以尝试更像您提供的代码?就个人而言,我认为严格限制您可以使用的类的数量是愚蠢的。如果这是我遇到的问题,我给出的解决方案就是我将使用的解决方案,它包含4个类。但是,如

Arrays 请记住,考虑到您提供的解决方案,是否有可能得到相同的结果,或者您是否建议我调整我的原始代码,以尝试更像您提供的代码?就个人而言,我认为严格限制您可以使用的类的数量是愚蠢的。如果这是我遇到的问题,我给出的解决方案就是我将使用的解决方案,它包含4个类。但是,如,arrays,ruby,hash,accounts,Arrays,Ruby,Hash,Accounts,请记住,考虑到您提供的解决方案,是否有可能得到相同的结果,或者您是否建议我调整我的原始代码,以尝试更像您提供的代码?就个人而言,我认为严格限制您可以使用的类的数量是愚蠢的。如果这是我遇到的问题,我给出的解决方案就是我将使用的解决方案,它包含4个类。但是,如果你试图按照课堂上的说明去做,那么我建议你按照他们说的去做,以我的答案为基础。关键的想法是,如果你想对一个人的状态做些什么,你必须通过调用一个Person方法来实现,而不是在银行。 class Bank attr_accessor :ba


请记住,考虑到您提供的解决方案,是否有可能得到相同的结果,或者您是否建议我调整我的原始代码,以尝试更像您提供的代码?就个人而言,我认为严格限制您可以使用的类的数量是愚蠢的。如果这是我遇到的问题,我给出的解决方案就是我将使用的解决方案,它包含4个类。但是,如果你试图按照课堂上的说明去做,那么我建议你按照他们说的去做,以我的答案为基础。关键的想法是,如果你想对一个人的状态做些什么,你必须通过调用一个Person方法来实现,而不是在银行。
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.
class BalanceHolder
  attr_reader :name, :balance
  def initialize(name, balance)
    @name    = name
    @balance = balance
  end

  def deposit(amount)
    @balance += amount
  end

  def withdraw(amount)
    raise ArgumentError, "#{@name} has insufficient funds." if @balance < amount
    @balance -= amount
  end
end

class Bank < BalanceHolder
  def open(name, balance)
    deposit(balance)
    Bank::Account.new(self, name, balance)
  end

  private
  class Account < BalanceHolder
    def initialize(bank, name, balance)
      @bank = bank
      super(name, balance)
    end

    def deposit(amount)
      super
      @bank.deposit(amount)
    end

    def withdraw(amount)
      super
      @bank.withdraw(amount)
    end

    def transfer(amount, account)
      withdraw(amount)
      account.deposit(amount)
    end
  end
end

chase       = Bank.new("JP Morgan Chase", 0)
wells_fargo = Bank.new("Wells Fargo", 0)

randy_chase = chase.open("Randy", 200)
randy_wells = wells_fargo.open("Randy", 200)

randy_chase.deposit(200)
randy_chase.transfer(100, randy_wells)