arrays - 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? -


working on bank program. need make sure users (person objects) have own bank accounts can create different balances in each account can interact with. should able deposit, withdraw, transfer money, etc. i'm having trouble outputting correctly.

as see, when says how money each person has in account being called, it's displaying total accounts created user. ideas on how can fix this?

also, need little guidance on if statement in transfer method. can't display correctly 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 created."   end    def open_account(name, bname, balance = 0)     if @users.include?(name)       bname.each |users|         @@accounts.push('users')       end     end     @balance = balance     puts "#{name}, opening account @ #{@bname} initial $#{balance} deposit!"   end    def user     @users   end    def withdrawal(name, amount)     @balance -= amount     puts "#{name} withdrew $#{amount} #{@bname}.  #{name} has #{@balance}.  #{name}'s account has #{@balance}."   end    def deposit(name, amount)     @balance += amount     puts "#{name} deposited $#{amount} #{@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} #{@bname} account #{account2} account.  #{@bname} account has $#{amount} , #{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}.  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 code is:

jp morgan chase bank created. wells fargo bank created. hi, randy.  have $1000 on hand! hi, kristen.  have $5000 on hand! hi, justin.  have $1500 on hand! randy, opening account @ jp morgan chase initial $200 deposit! kristen, opening account @ jp morgan chase initial $300 deposit! justin, opening account @ jp morgan chase initial $400 deposit! randy, opening account @ wells fargo initial $200 deposit! kristen, opening account @ wells fargo initial $300 deposit! randy deposited $200 jp morgan chase.  randy has 600.  randy's account has 600. kristen deposited $350 jp morgan chase.  kristen has 950.  kristen's account has 950. kristen withdrew $500 jp morgan chase.  kristen has 450.  kristen's account has 450. randy transfered $100 jp morgan chase account wells fargo account.  jp morgan chase account has $100 , wells fargo account has $350. randy deposited $150 jp morgan chase.  randy has 500.  randy's account has 500. 

i think of issues may down misunderstanding of object oriented design, rather of ruby. so, let's specify bank:

bank

  • open(name, balance) open account name , balance given. return account deposit, withdraw , transfer from.
  • deposit(amount) credits balance of bank.
  • withdraw(amount) debits balance of bank (if have enough money).

and see need account class, but, we'll creating instances of inside bank#open , don't want people able create instances other way, we'll hide class inside bank:

bank::account

  • deposit(amount) credits balance of account , bank well.
  • withdraw(amount) debits balance of account , user (if have enough money).
  • transfer(amount, account) transfers 1 account other, if required funds available.

now notice bank , bank::account sharing methods, namely deposit, , withdraw, , additionally, state, (the balance, , name of entity e.g. "jp morgan chase" bank or "joe bloggs" person). let's abstract out own class, (i'll call balanceholder doesn't matter much, long makes sense).

putting together, this:

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) 

perhaps important difference between i've written , have written called separation of concerns (a similar idea single responsibility principle, or srp). referring that, in interface, if want deposit money account, call method on account, (rather call method on bank, giving account name parameter), and, aid this, each account aware of bank belongs (because part of state of particular account).

also worth noting there nothing bank can account after opened, there no point in bank being aware of accounts has associated it.

one thing have in model have not incorporated mine idea 1 person can have multiple accounts @ different banks. if this, there several ways go it. of them involve creating yet class called person, deals balance , name particular person.

  • one way make person subclass of balanceholder , have account update associated person's balance each transaction.
  • alternatively, have person class keep track of of accounts, , when requesting balance, sums balances of of accounts.

i opt later because reduces coupling, design decision made when featureset of program requires it.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -