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
With all of our methods in place for working with our account, it's time to put it all together. In the conclusion, we'll format and print out the register, including all of the different account transactions.
Links
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
def print_register
puts "#{name}'s Bank Account"
puts "-" * 40
puts "Description".ljust(30) + "Amount".rjust(10)
@transactions.each do |transaction|
puts transaction[:description].ljust(30) + sprintf("%0.2f", transaction[:amount]).rjust(10)
end
puts "-" * 40
puts "Balance:".ljust(30) + sprintf("%0.2f", balance).rjust(10)
puts "-" * 40
end
end
bank_account = BankAccount.new("Jason")
bank_account.credit("Paycheck", 100)
bank_account.debit("Groceries", 40)
bank_account.debit("Gas", 10.51)
puts bank_account
puts "Register:"
bank_account.print_register
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 here's our BankAccount class as
it is.
0:00
We've got transactions and we can print
out a balance.
0:02
But now, let's go ahead and print out the
whole register for people.
0:08
So this will loop through the
transactions, say what the description
0:12
was, and the amount of the transaction,
and then will also print out the balance.
0:15
So, we'll just call this print_register.
0:21
And the first thing that we'll do is print
out the name.
0:27
And we'll just say name's bank account.
0:32
And then we will print out the
description.
0:42
And the amount, and then we'll loop
through the transactions and
0:46
print those out too.
0:48
So we'll say description, and
0:50
we'll throw a tab in here, and amount.
0:55
Now, we'll loop through these
transactions.
1:00
And we'll print out the transaction
description.
1:10
Plus a tab character.
1:18
And the transaction amount.
1:21
Before we go any further let's go ahead
and see how that looks.
1:27
And we'll leave the bank account being
printed out.
1:34
And then we'll say register.
1:38
Alright.
1:44
So now we'll run this.
1:46
Uh-oh.
1:49
On line 39, it says no implicit conversion
of Fixnum into String.
1:51
So let's go ahead and actually call the
to_s method here on the transaction
2:01
amount, which will force a conversion to a
string.
2:07
So we run that and that fixes it.
2:15
But it doesn't really look that great.
2:18
First off, the numbers are back to being
printed with nothing here.
2:21
So let's go ahead and do the same thing
that we did for printing out the balance.
2:26
[BLANK_AUDIO]
2:31
And we will say, sprintf.
2:34
[BLANK_AUDIO]
2:38
With the transaction amount.
2:46
And that should hopefully fix that.
2:48
Okay.
2:51
That looks a bit better.
2:52
We should also print out the ending
balance.
2:56
And we can call this sprintf again.
3:02
[BLANK_AUDIO]
3:04
And so here we have the balance again.
3:14
But, I really don't like the formatting of
this.
3:18
Luckily, the string class gives us a
couple methods
3:23
to justify text that we send in, and this
is the ljust and rjust methods.
3:29
So we assume that we want to print out
about 40 characters on a line.
3:38
And let's just say that the description is
gonna be 30 of those characters.
3:43
If we look back at the documentation here
we throw in the integer,
3:50
and the string that we want to pad it
with.
3:56
Which in our case is just going to be a
space.
3:59
So we do this by saying transaction[:description]
4:05
And since it's a string we call
4:11
this method ljust with an integer of 30.
4:15
And we can do the same thing with the
amount of
4:21
the transaction and rjust, which we'll
give 10.
4:26
And this'll add up to 40.
4:32
And that looks a lot better.
4:37
So, the only thing left to do
4:39
is the same thing with description and
amount.
4:43
[BLANK_AUDIO]
4:45
And we can do the same thing.
4:54
[BLANK_AUDIO]
4:55
With the balance.
5:05
So, when we run that, this is all formated
very nicely.
5:08
[BLANK_AUDIO]
5:12
I think I'd like to put some lines though.
5:15
Here's a cool trick.
5:17
We can put a dash 40 times by using the
multiplication operator on the string.
5:19
So, we'll just do the separators, and we
should be good to go.
5:29
Run this again.
5:37
And that looks great.
5:40
Here's my bank account, and
5:42
it shows each item in here as we go
through and add it along.
5:45
Let's go ahead and add another
transaction.
5:50
Make sure it's all working okay.
5:54
Clear the screen on the bottom with
Ctrl+L.
5:59
And run this again.
6:03
And this looks really good.
6:04
So we've gone through and created our own
class and added methods to it.
6:10
Plus we also created all of these
different internal variables.
6:16
And created an interface for working with
it.
6:21
Great job everybody.
6:25
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