1 00:00:00,210 --> 00:00:02,970 Okay, so here's our BankAccount class as it is. 2 00:00:02,970 --> 00:00:06,930 We've got transactions and we can print out a balance. 3 00:00:08,060 --> 00:00:12,120 But now, let's go ahead and print out the whole register for people. 4 00:00:12,120 --> 00:00:15,410 So this will loop through the transactions, say what the description 5 00:00:15,410 --> 00:00:21,310 was, and the amount of the transaction, and then will also print out the balance. 6 00:00:21,310 --> 00:00:25,054 So, we'll just call this print_register. 7 00:00:27,520 --> 00:00:32,598 And the first thing that we'll do is print out the name. 8 00:00:32,598 --> 00:00:42,176 And we'll just say name's bank account. 9 00:00:42,176 --> 00:00:44,950 And then we will print out the description. 10 00:00:46,095 --> 00:00:48,520 And the amount, and then we'll loop through the transactions and 11 00:00:48,520 --> 00:00:49,400 print those out too. 12 00:00:50,930 --> 00:00:55,339 So we'll say description, and 13 00:00:55,339 --> 00:01:00,896 we'll throw a tab in here, and amount. 14 00:01:00,896 --> 00:01:10,079 Now, we'll loop through these transactions. 15 00:01:10,079 --> 00:01:18,612 And we'll print out the transaction description. 16 00:01:18,612 --> 00:01:21,910 Plus a tab character. 17 00:01:21,910 --> 00:01:27,930 And the transaction amount. 18 00:01:27,930 --> 00:01:31,010 Before we go any further let's go ahead and see how that looks. 19 00:01:34,260 --> 00:01:36,070 And we'll leave the bank account being printed out. 20 00:01:38,370 --> 00:01:40,040 And then we'll say register. 21 00:01:44,980 --> 00:01:45,530 Alright. 22 00:01:46,570 --> 00:01:49,954 So now we'll run this. 23 00:01:49,954 --> 00:01:51,289 Uh-oh. 24 00:01:51,289 --> 00:01:57,150 On line 39, it says no implicit conversion of Fixnum into String. 25 00:02:01,050 --> 00:02:07,610 So let's go ahead and actually call the to_s method here on the transaction 26 00:02:07,610 --> 00:02:13,900 amount, which will force a conversion to a string. 27 00:02:15,060 --> 00:02:18,250 So we run that and that fixes it. 28 00:02:18,250 --> 00:02:21,530 But it doesn't really look that great. 29 00:02:21,530 --> 00:02:26,210 First off, the numbers are back to being printed with nothing here. 30 00:02:26,210 --> 00:02:31,730 So let's go ahead and do the same thing that we did for printing out the balance. 31 00:02:31,730 --> 00:02:34,923 [BLANK_AUDIO] 32 00:02:34,923 --> 00:02:38,569 And we will say, sprintf. 33 00:02:38,569 --> 00:02:46,974 [BLANK_AUDIO] 34 00:02:46,974 --> 00:02:48,438 With the transaction amount. 35 00:02:48,438 --> 00:02:51,200 And that should hopefully fix that. 36 00:02:51,200 --> 00:02:52,590 Okay. 37 00:02:52,590 --> 00:02:54,410 That looks a bit better. 38 00:02:56,040 --> 00:03:02,391 We should also print out the ending balance. 39 00:03:02,391 --> 00:03:04,655 And we can call this sprintf again. 40 00:03:04,655 --> 00:03:14,373 [BLANK_AUDIO] 41 00:03:14,373 --> 00:03:18,668 And so here we have the balance again. 42 00:03:18,668 --> 00:03:21,989 But, I really don't like the formatting of this. 43 00:03:23,580 --> 00:03:29,840 Luckily, the string class gives us a couple methods 44 00:03:29,840 --> 00:03:36,920 to justify text that we send in, and this is the ljust and rjust methods. 45 00:03:38,960 --> 00:03:43,020 So we assume that we want to print out about 40 characters on a line. 46 00:03:43,020 --> 00:03:47,700 And let's just say that the description is gonna be 30 of those characters. 47 00:03:50,120 --> 00:03:55,550 If we look back at the documentation here we throw in the integer, 48 00:03:56,660 --> 00:03:59,100 and the string that we want to pad it with. 49 00:03:59,100 --> 00:04:02,530 Which in our case is just going to be a space. 50 00:04:05,960 --> 00:04:11,094 So we do this by saying transaction[:description] 51 00:04:11,094 --> 00:04:15,369 And since it's a string we call 52 00:04:15,369 --> 00:04:21,299 this method ljust with an integer of 30. 53 00:04:21,299 --> 00:04:26,274 And we can do the same thing with the amount of 54 00:04:26,274 --> 00:04:32,340 the transaction and rjust, which we'll give 10. 55 00:04:32,340 --> 00:04:37,967 And this'll add up to 40. 56 00:04:37,967 --> 00:04:39,883 And that looks a lot better. 57 00:04:39,883 --> 00:04:43,261 So, the only thing left to do 58 00:04:43,261 --> 00:04:45,386 is the same thing with description and amount. 59 00:04:45,386 --> 00:04:54,418 [BLANK_AUDIO] 60 00:04:54,418 --> 00:04:55,870 And we can do the same thing. 61 00:04:55,870 --> 00:05:05,343 [BLANK_AUDIO] 62 00:05:05,343 --> 00:05:08,315 With the balance. 63 00:05:08,315 --> 00:05:12,216 So, when we run that, this is all formated very nicely. 64 00:05:12,216 --> 00:05:15,293 [BLANK_AUDIO] 65 00:05:15,293 --> 00:05:17,557 I think I'd like to put some lines though. 66 00:05:17,557 --> 00:05:19,496 Here's a cool trick. 67 00:05:19,496 --> 00:05:28,840 We can put a dash 40 times by using the multiplication operator on the string. 68 00:05:29,910 --> 00:05:33,950 So, we'll just do the separators, and we should be good to go. 69 00:05:37,390 --> 00:05:38,170 Run this again. 70 00:05:40,090 --> 00:05:41,260 And that looks great. 71 00:05:42,420 --> 00:05:45,008 Here's my bank account, and 72 00:05:45,008 --> 00:05:50,091 it shows each item in here as we go through and add it along. 73 00:05:50,091 --> 00:05:54,526 Let's go ahead and add another transaction. 74 00:05:54,526 --> 00:05:59,460 Make sure it's all working okay. 75 00:05:59,460 --> 00:06:03,198 Clear the screen on the bottom with Ctrl+L. 76 00:06:03,198 --> 00:06:04,950 And run this again. 77 00:06:04,950 --> 00:06:06,030 And this looks really good. 78 00:06:10,050 --> 00:06:14,870 So we've gone through and created our own class and added methods to it. 79 00:06:16,240 --> 00:06:21,380 Plus we also created all of these different internal variables. 80 00:06:21,380 --> 00:06:25,190 And created an interface for working with it. 81 00:06:25,190 --> 00:06:26,130 Great job everybody.