【问题标题】:How can I accurately push individual users and accounts to a hash and then make sure separate account balances are not added together as one?如何准确地将单个用户和帐户推送到散列,然后确保单独的帐户余额不会加在一起?
【发布时间】: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.

【问题讨论】:

    标签: arrays ruby hash accounts


    【解决方案1】:

    我认为其中一些问题可能归结于对面向对象设计的误解,而不是对 Ruby 的误解。所以,让我们指定银行:

    银行

    • open(name, balance) 使用给定的名称和余额开设一个帐户。将帐号退回至depositwithdrawtransfer from。
    • deposit(amount) 记入银行余额。
    • withdraw(amount) 借记银行的余额(如果他们有足够的钱)。

    然后我们看到我们还需要一个 Account 类,但是,我们将从 Bank#open 内部创建它的实例,我们不希望人们能够以任何其他方式创建实例,所以我们将这个类隐藏在Bank

    银行::账户

    • deposit(amount) 记入账户余额和银行。
    • withdraw(amount) 借记账户和用户的余额(如果他们有足够的钱)。
    • transfer(amount, account) 从一个帐户转移到另一个帐户(如果有所需资金)。

    现在我们注意到BankBank::Account 共享一些方法,即depositwithdraw,此外还有一些状态(余额和实体名称,例如"JP Morgan Chase"银行或"Joe Bloggs" 个人)。所以让我们把它抽象到它自己的类中,(我会称之为BalanceHolder,但它并不重要,只要它有意义就行了)。

    把它放在一起,我们得到了这个:

    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)
    

    也许我写的和你写的最重要的区别是关注点分离(类似的想法是单一责任原则,或SRP)。这指的是,在我的界面中,如果您想将钱存入帐户,则调用该帐户的方法,(而不是调用银行的方法,将帐户名称作为参数),并且,为此,每个帐户都知道它属于哪个银行(因为这是特定帐户状态的一部分)。

    另外值得注意的是,银行在开立账户后无法对账户进行任何操作,因此银行无需了解与其关联的所有账户。

    你的模型中有一个我没有纳入我的模型的想法是,一个人可以在不同的银行拥有多个账户。如果我要这样做,有几种方法可以解决。所有这些都涉及创建另一个名为Person 的类,它处理特定人的余额和姓名。

    • 一种方法是使Person 成为BalanceHolder 的子类,并让Account 在每次交易时更新其关联人员的余额。
    • 或者,我可以让Person 类跟踪其所有帐户,并且在请求余额时,它只是将其所有帐户的余额相加。

    我可能会选择后者,因为它减少了耦合,但这是在程序的功能集需要时做出的设计决定。

    【讨论】:

    • 感谢您花时间作为Quirrel!我的任务方向是开放式的,在课程中,到目前为止我们还没有处理过名称空间,尽管我已经阅读了它们并用它们做了一些练习。您的解决方案完全有意义,我将根据您的建议重新编写我的代码以拥有一个可靠的工作程序。我觉得我的看起来有点太“忙”了,绝对可以更干净,你的就是!我肯定需要能够允许此人在另一家银行开户,所以我将从您的建议开始。谢谢!
    • 还有一件事,在我的说明中,我被告知使用 2 个类来创建程序,这就是为什么我试图将所有内容都保留在 bank 和 person 类中。请记住这一点,根据您提供的解决方案是否有可能获得相同的结果,或者您是否建议我调整我的原始代码以尝试表现得更像您提供的那样?
    • 就个人而言,我认为严格限制可以使用的类数量是愚蠢的。如果这是我遇到的问题,那么我给出的解决方案就是我会使用的解决方案,它包含 4 个类。但是,如果您尝试按照课程说明进行操作,那么我建议您按照他们所说的做,以我的回答为基础。真正的关键思想是,如果你想对 Person 的状态做某事,你必须通过调用 Person 上的方法,而不是 Bank 上的方法。
    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2020-09-08
    相关资源
    最近更新 更多