Well Done!... But how to transfer "money" from one account to another?


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-32cN3/0/7?curriculum_id=5059f8619189a5000201fbcb


Well, I tried a lot of different things and i am really stuck after trying a lot of different ways to transfer "money" from one account to another. I just deleted everything I made myself, because it was totally not doing what I wanted.
Firstly, what i thought is that you could write down the first part like a withdrawal. But how do you tell the method to add the ammount to the other account? Secondly, the second part could be written down as a deposit. Basically this last step is what I couldn't find out!

I hope some genius Rubyist can help me out?


def transfer(pin_number, amount)
        if pin_number == pin
        @balance -= amount

#What should be coded here to add the amount subtracted from "account1" 
#after starting the transfer as followed: account1.transfer(pin_number, amount)
#and add this amount to "account2"?

end

I would be gratefull!:slight_smile:

Cheers,

A.Spapens


#2

I've been looking at this problem for a good bit, off and on all evening. Still not down to coding. Trying to figure how two instances can share/exchange data. That is the challenge here. That we can subtract from one account is moot. We need access to the other account, which is another instance of the Account class.

We must go on the assumption that it can be done or our pursuit is pointless.


#3

One thought is to have both instances present in the environment. The exchanger does not need to know the pin of the exchangee since the 'bank' is handling the transfer. They know the account and have access to it. All we need do is tell them which account. We have both instances so that is doable. All we need is the mechanics of how to ....


#4

You would need to give the method a reference to the other account.

a = Account.new
b = Account.new

b.transferTo(1234, 57, a);

(obviously the transferTo method needs to be implemented first)

Alternatively just withdraw and deposit into the other account


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.