1 00:00:00,940 --> 00:00:03,880 Okay, so we've got our BankAccount class all set up. 2 00:00:03,880 --> 00:00:07,930 And we've get credits, debits, and transactions going on. 3 00:00:07,930 --> 00:00:12,750 So it would be nice to be able to see what our balance is. 4 00:00:12,750 --> 00:00:17,390 So let's go ahead and write a method that calculates the balance for 5 00:00:17,390 --> 00:00:21,280 us by looping through the transactions. 6 00:00:21,280 --> 00:00:23,470 So we know that transactions is an array. 7 00:00:23,470 --> 00:00:25,020 And in the Ruby Loops course, 8 00:00:25,020 --> 00:00:29,940 we learned about iterating through arrays with the each method. 9 00:00:29,940 --> 00:00:35,360 Let's go ahead and use the each method to iterate over the transactions and 10 00:00:35,360 --> 00:00:36,700 add up the balance. 11 00:00:38,080 --> 00:00:44,120 So, we can say @transactions.each do. 12 00:00:46,520 --> 00:00:47,820 |transaction| 13 00:00:47,820 --> 00:00:52,340 This will iterate over the @transactions array and 14 00:00:52,340 --> 00:00:55,080 pass each item into this little code block. 15 00:00:56,690 --> 00:01:01,070 And now we'll create a local variable, which we're just gonna call balance. 16 00:01:02,490 --> 00:01:08,060 And then we'll add on to the balance in each transaction. 17 00:01:09,530 --> 00:01:13,118 So, we'll say the balance is equal to whatever it was before, 18 00:01:13,118 --> 00:01:18,928 plus the amount of this transaction. 19 00:01:18,928 --> 00:01:25,766 [SOUND] And then, we can return the balance. 20 00:01:25,766 --> 00:01:28,572 [BLANK_AUDIO] 21 00:01:28,572 --> 00:01:33,660 Now we know that the transaction amount is gonna be positive or negative. 22 00:01:34,740 --> 00:01:38,360 We know that because we're going to be calling the credit and 23 00:01:38,360 --> 00:01:44,310 debit methods each time we add on to the transactions. 24 00:01:45,530 --> 00:01:49,630 So we know that when there's a debit, this goes in as a negative amount. 25 00:01:50,960 --> 00:01:52,830 And credits go in as a positive amount. 26 00:01:54,630 --> 00:02:00,080 Therefore, adding on to the balance will work as we expect it to, 27 00:02:00,080 --> 00:02:04,200 because adding a negative number is the same as subtraction. 28 00:02:07,070 --> 00:02:13,750 So now instead of this inspect method, let's go ahead and print out the balance. 29 00:02:17,870 --> 00:02:23,166 And that functions exactly as we 30 00:02:23,166 --> 00:02:28,468 expect it to by returning 60. 31 00:02:28,468 --> 00:02:32,434 Now what would happen if we made this 0.0? 32 00:02:32,434 --> 00:02:35,840 Well, it prints out 60.0. 33 00:02:35,840 --> 00:02:40,525 Now if we wanted this to print out like currency, 34 00:02:40,525 --> 00:02:45,670 say having 60.0 and instead we want 60.00, 35 00:02:45,670 --> 00:02:53,100 we can use a method called sprintf to format this particular string. 36 00:02:53,100 --> 00:02:57,170 You can read the documentation for this if you'd like to find out a little bit more. 37 00:02:58,200 --> 00:03:05,150 The string that we need to use, is a percent, 38 00:03:05,150 --> 00:03:09,340 a zero, a dot, a two, and an f. 39 00:03:09,340 --> 00:03:13,560 Which means it's going to print this floating point number to two decimals. 40 00:03:16,710 --> 00:03:22,770 So if we run this again, it prints out 60.00 just like we expect. 41 00:03:22,770 --> 00:03:28,787 Now that we have this balance method all set up, let's go ahead and 42 00:03:28,787 --> 00:03:33,527 override the to string method to print that all out. 43 00:03:33,527 --> 00:03:41,189 So to_s, we'll say the name is the name that we initialized the class with. 44 00:03:41,189 --> 00:03:44,490 And the balance is the balance. 45 00:03:44,490 --> 00:03:50,447 And you know what, let's go ahead and call balance just like we do here. 46 00:03:50,447 --> 00:03:56,623 [BLANK_AUDIO] 47 00:03:56,623 --> 00:03:58,296 Now this is a little tricky, 48 00:03:58,296 --> 00:04:02,490 because we're doing string interpolation inside of another string. 49 00:04:04,370 --> 00:04:09,131 So we can go down here. 50 00:04:09,131 --> 00:04:12,050 And just print out the bank account. 51 00:04:12,050 --> 00:04:16,710 So now, it says name Jason and the balance is 60.00. 52 00:04:16,710 --> 00:04:20,000 In our next video, we'll be printing out a formatted register.