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
In part 3 of Build a Bank Account class, we're going to calculate the balance based off of all of the transactions that exist within the account. We do this by writing a method called `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
def balance
balance = 0.0
@transactions.each do |transaction|
balance += transaction[:amount]
end
return balance
end
def to_s
"Name: #{name}, Balance: #{sprintf("%0.2f", balance)}"
end
end
bank_account = BankAccount.new("Jason")
bank_account.credit("Paycheck", 100)
bank_account.debit("Groceries", 40)
puts bank_account
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
Okay, so we've got our BankAccount class
all set up.
0:00
And we've get credits, debits, and
transactions going on.
0:03
So it would be nice to be able to see what
our balance is.
0:07
So let's go ahead and write a method that
calculates the balance for
0:12
us by looping through the transactions.
0:17
So we know that transactions is an array.
0:21
And in the Ruby Loops course,
0:23
we learned about iterating through arrays
with the each method.
0:25
Let's go ahead and use the each method to
iterate over the transactions and
0:29
add up the balance.
0:35
So, we can say @transactions.each do.
0:38
|transaction|
0:46
This will iterate over the @transactions
array and
0:47
pass each item into this little code
block.
0:52
And now we'll create a local variable,
which we're just gonna call balance.
0:56
And then we'll add on to the balance in
each transaction.
1:02
So, we'll say the balance is equal to
whatever it was before,
1:09
plus the amount of this transaction.
1:13
[SOUND] And then, we can return the
balance.
1:18
[BLANK_AUDIO]
1:25
Now we know that the transaction amount is
gonna be positive or negative.
1:28
We know that because we're going to be
calling the credit and
1:34
debit methods each time we add on to the
transactions.
1:38
So we know that when there's a debit, this
goes in as a negative amount.
1:45
And credits go in as a positive amount.
1:50
Therefore, adding on to the balance will
work as we expect it to,
1:54
because adding a negative number is the
same as subtraction.
2:00
So now instead of this inspect method,
let's go ahead and print out the balance.
2:07
And that functions exactly as we
2:17
expect it to by returning 60.
2:23
Now what would happen if we made this 0.0?
2:28
Well, it prints out 60.0.
2:32
Now if we wanted this to print out like
currency,
2:35
say having 60.0 and instead we want 60.00,
2:40
we can use a method called sprintf to
format this particular string.
2:45
You can read the documentation for this if
you'd like to find out a little bit more.
2:53
The string that we need to use, is a
percent,
2:58
a zero, a dot, a two, and an f.
3:05
Which means it's going to print this
floating point number to two decimals.
3:09
So if we run this again, it prints out
60.00 just like we expect.
3:16
Now that we have this balance method all
set up, let's go ahead and
3:22
override the to string method to print
that all out.
3:28
So to_s, we'll say the name is the name
that we initialized the class with.
3:33
And the balance is the balance.
3:41
And you know what, let's go ahead and call
balance just like we do here.
3:44
[BLANK_AUDIO]
3:50
Now this is a little tricky,
3:56
because we're doing string interpolation
inside of another string.
3:58
So we can go down here.
4:04
And just print out the bank account.
4:09
So now, it says name Jason and the balance
is 60.00.
4:12
In our next video, we'll be printing out a
formatted register.
4:16
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