Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A bank account is made up of transactions. In this video, we're going to create methods to add transactions to our bank account. The transactions can be credits or debits and affect the running balance.
Code Samples
class BankAccount
attr_reader :name
def initialize(name)
@name = name
@transactions = []
add_transaction("Beginning Balance", 0)
end
def credit(description, amount)
add_transaction(description, amount)
end
def debit(description, amount)
add_transaction(description, -amount)
end
def add_transaction(description, amount)
@transactions.push(description: description, amount: amount)
end
end
bank_account = BankAccount.new("Jason")
bank_account.credit("Paycheck", 100)
bank_account.debit("Groceries", 40)
puts bank_account.inspect
Returns the following:
#<BankAccount:0x007f00882acbc0
@name="Jason",
@transactions=[
{:description=>"Beginning Balance", :amount=>0},
{:description=>"Paycheck", :amount=>100},
{:description=>"Groceries", :amount=>-40}
]>
-
0:00
So when we last left off, we had created our bank account class.
-
0:05
We defined a couple instance variables on the class and
-
0:08
then we instantiated it and just took a look at how the bank account was looking.
-
0:14
Now let's go ahead and create some methods to work with this bank account.
-
0:18
First let's create a method for adding a transaction.
-
0:24
We'll call that add_transaction.
-
0:30
And what that's going to do is append to this list of transactions.
-
0:37
And for right now, let's go ahead and make name an attribute reader.
-
0:45
So we can always see whose name it is.
-
0:48
But we only wanna be able to write to it the first time we instantiate the class.
-
0:54
Now by adding a transaction to the array we'll always be able to
-
0:57
keep track of what happened inside of this bank account.
-
1:01
And this is pretty easy to do.
-
1:03
We'll just say we want a description of the transaction, and an amount.
-
1:13
And now we can say transactions.push, since it's an array.
-
1:22
And, we will create a hash here, with a key
-
1:27
of description, and another key for the amount.
-
1:35
So now that we have created this add_transaction method,
-
1:39
we can go ahead and use it.
-
1:43
So, we've got this bank_account.inspect.
-
1:48
Now, let's go ahead and add a transaction.
-
1:50
[SOUND] And we'll say we wanted groceries,
-
1:58
and that cost $40.
-
2:02
[SOUND] Let's go ahead and inspect the bank account again.
-
2:07
[SOUND] Gonna click down in here, and run this one more time.
-
2:14
[SOUND] Now we can see when we call this add_transaction method,
-
2:20
it appends to the list of transactions.
-
2:25
Now if we wanted to, we could make transactions an attribute accessor.
-
2:30
But if we really think about how a bank account works, we wouldn't want just
-
2:35
any other object being able to write to this list of transactions.
-
2:42
So, we're going to provide an interface for working with these transactions.
-
2:48
And now that we have this transactions method, let's go ahead and
-
2:52
as soon as we initialize the bank account, we'll add a transaction.
-
2:57
[SOUND] That says Beginning Balance.
-
3:05
And it's going to be zero.
-
3:08
[SOUND] So if we run it again, we can see that we now have two transactions.
-
3:18
Now there's a little bit of a difference between a couple different kinds
-
3:22
of transactions.
-
3:23
Because let's think about this for
-
3:26
just a moment, we've got this add transaction, say I spent $40 on groceries.
-
3:32
But I actually think this should be
-
3:35
negative because this is a debit rather than a credit.
-
3:42
So let's go ahead and code that.
-
3:47
And we'll do one for credit and debit.
-
3:50
[SOUND] These both should take a description and amount.
-
3:58
We can just say, add transaction for credit description and amount and
-
4:03
what that will do will be called the add_transaction method.
-
4:08
With the description and amount being sent in.
-
4:11
[SOUND] And we can do the same thing for the debit method,
-
4:16
only we're going to make that a negative transaction.
-
4:21
[SOUND] So let me get this
-
4:26
first inspect out of here.
-
4:33
Bank account.
-
4:34
Now we'll call the credit method.
-
4:36
And say paycheck, that was 100.
-
4:43
And then we'll call this debit method.
-
4:46
We bought groceries for $40.
-
4:49
So now if we run this, I'm gonna clear my screen here.
-
4:52
We should see three transactions for zero, 100, and minus 40.
-
4:58
And we do,
-
4:59
we can see our transactions is an array containing all of these different hashes.
-
5:05
[BLANK_AUDIO]
You need to sign up for Treehouse in order to download course files.
Sign up