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