1 00:00:00,450 --> 00:00:01,530 Welcome to Part seven. 2 00:00:01,600 --> 00:00:06,570 We're going to finalize the bank class, the first task was to create an add transaction method. 3 00:00:06,960 --> 00:00:12,150 This method is going to be public void called transaction. 4 00:00:12,840 --> 00:00:15,450 And it's going to receive a transaction object. 5 00:00:16,980 --> 00:00:22,590 This transaction object we're going to add to the transactions are a list, so we'll say this that transactions 6 00:00:22,600 --> 00:00:26,520 that add and we're going to add to the area list a copy. 7 00:00:27,160 --> 00:00:31,500 A new transaction copy of the object being passed in. 8 00:00:39,460 --> 00:00:46,690 Now, if you go back to the data, go to transactions that text, every transaction has an I.D. that 9 00:00:46,690 --> 00:00:48,280 matches one of the accounts. 10 00:00:48,700 --> 00:00:56,020 So what we need to do is create a method that returns an array of transactions that match a particular 11 00:00:56,020 --> 00:00:56,500 account. 12 00:00:56,890 --> 00:00:59,650 So we'll go to Bank Dot Java. 13 00:01:00,790 --> 00:01:01,810 And we'll call it. 14 00:01:03,090 --> 00:01:07,080 Get transactions, so it's going to return an array of transactions. 15 00:01:08,410 --> 00:01:12,490 Called Get Transactions, it's going to receive an account ID. 16 00:01:14,120 --> 00:01:15,960 Whose transactions we want to return? 17 00:01:16,280 --> 00:01:21,650 Now you might think to credit for a loop that runs through the entire transactions array list and FAA 18 00:01:21,650 --> 00:01:26,650 transactions, it happens to match the account ID added to the array and return that. 19 00:01:26,660 --> 00:01:27,980 But that's really messy. 20 00:01:27,990 --> 00:01:29,870 I would rather use a stream pipeline. 21 00:01:30,230 --> 00:01:34,610 So we're going to say this dart transactions that stream. 22 00:01:36,490 --> 00:01:40,120 And we're going to run the stream of elements through a pipeline of operations. 23 00:01:40,420 --> 00:01:46,630 The first operation we're going to use is the familiar filter operation, and inside we'll go through 24 00:01:46,630 --> 00:01:50,860 every single transaction and filter the ones whose I.D.. 25 00:01:51,980 --> 00:01:56,030 Get ID matches, the account ID being passed in. 26 00:02:00,210 --> 00:02:05,880 So at this point, the stream of elements consists of only the filtered transactions, and we're going 27 00:02:05,880 --> 00:02:08,759 to collect this updated stream of elements into a list. 28 00:02:09,060 --> 00:02:10,979 So from the list of collectors. 29 00:02:12,830 --> 00:02:14,510 We're going to choose to list. 30 00:02:17,310 --> 00:02:22,260 This terminal operation is going to return our filtered sequence of elements into our list, so we're 31 00:02:22,260 --> 00:02:23,820 going to say list. 32 00:02:25,290 --> 00:02:26,190 Transaction. 33 00:02:28,610 --> 00:02:32,990 And into this list of objects, we're going to store the result of this pipeline. 34 00:02:36,260 --> 00:02:40,010 Cannot convert from list transaction to a list transaction. 35 00:02:42,570 --> 00:02:44,040 Oh, we got an import list. 36 00:02:48,080 --> 00:02:48,380 All right. 37 00:02:48,410 --> 00:02:53,990 And now this method expects an array, so what we're going to do is return list to array. 38 00:02:57,060 --> 00:03:03,120 Now, the problem is that two Iraq returns an array of objects, which is not good, mortuary can actually 39 00:03:03,120 --> 00:03:09,330 receive an argument and inside the argument, you need to pass in an array of type transaction. 40 00:03:09,900 --> 00:03:13,860 That's how it's going to know, OK, I need to return an array of that custom type. 41 00:03:17,740 --> 00:03:21,970 And when you're creating this array, here is where you put the length in this case, what would you 42 00:03:21,970 --> 00:03:24,670 put a length of one, a length of two, a length of three? 43 00:03:25,060 --> 00:03:30,220 Well, if you want a copy over every element from this list, the length of the array needs to be the 44 00:03:30,220 --> 00:03:31,360 size of the list. 45 00:03:31,480 --> 00:03:37,150 This is the workaround that I use to make sure to array returns an array of a custom type, not of the 46 00:03:37,150 --> 00:03:38,500 generic object type. 47 00:03:40,030 --> 00:03:44,230 OK, now we want to test our code to back on during the Part two, we'll copy this over into main. 48 00:03:58,790 --> 00:04:00,620 Maybe we'll need to import some stuff. 49 00:04:00,890 --> 00:04:01,790 Of course we do. 50 00:04:02,240 --> 00:04:04,250 We'll import transaction. 51 00:04:05,430 --> 00:04:07,980 Should cover all of that will import. 52 00:04:10,070 --> 00:04:16,850 We'll need to add a bank object, so here we'll say bank bank make it static because we're inside main. 53 00:04:19,089 --> 00:04:19,570 OK. 54 00:04:20,019 --> 00:04:20,920 Import Bank. 55 00:04:23,000 --> 00:04:25,490 Is equal to a new object of the bank class. 56 00:04:28,920 --> 00:04:33,240 And now you may not be used to the syntax, but it's just a for a loop that runs through every single 57 00:04:33,240 --> 00:04:35,400 account in the accounts array. 58 00:04:35,910 --> 00:04:40,780 And this one runs through every single transaction in the transactions array. 59 00:04:42,060 --> 00:04:47,040 Like this would be the exact same thing as saying This is just that here. 60 00:04:47,040 --> 00:04:48,360 We don't have to deal with counters. 61 00:04:48,370 --> 00:04:55,620 You can just use the element as is, if you want to use a normal for a loop and say, bank dot transaction 62 00:04:55,620 --> 00:04:58,500 transactions, I mean, that's also fine. 63 00:04:58,500 --> 00:04:59,640 You can do whatever you want. 64 00:04:59,940 --> 00:05:02,910 I'm just going to stick with the code we got from learn the parts. 65 00:05:03,540 --> 00:05:08,580 And anyways, here it adds a bunch of accounts to the bank object. 66 00:05:09,150 --> 00:05:12,360 Here it adds a bunch of transactions to the bank object. 67 00:05:12,690 --> 00:05:18,720 And the idea is that this line over here should filter the transactions that match the account. 68 00:05:18,720 --> 00:05:20,220 F8 4c. 69 00:05:20,640 --> 00:05:26,280 So there should be a total of one two three four five. 70 00:05:27,000 --> 00:05:29,850 Anyways, will place and breakpoint over here. 71 00:05:31,800 --> 00:05:32,550 And test it out. 72 00:05:36,580 --> 00:05:38,050 I'll have to step over this line. 73 00:05:39,200 --> 00:05:40,340 And beautiful. 74 00:05:40,520 --> 00:05:43,340 One, two, three four five transactions. 75 00:05:44,180 --> 00:05:45,860 Let's make sure they're the right ones. 76 00:05:47,050 --> 00:05:50,860 Withdraw a deposit, withdraw, withdraw a deposit, OK? 77 00:05:51,010 --> 00:05:52,930 Our function works perfectly. 78 00:05:54,270 --> 00:06:00,900 OK, now back inside Bangkok, Java, we're going to add the logic for getting the account for a particular 79 00:06:00,900 --> 00:06:01,620 transaction. 80 00:06:02,770 --> 00:06:05,200 So what I'll do here is, I'll say public. 81 00:06:06,230 --> 00:06:06,920 Account. 82 00:06:07,760 --> 00:06:12,260 We're going to get an account corresponds to a transaction I.D.. 83 00:06:17,360 --> 00:06:22,100 And once again, you might think to just create a for a loop that runs through the entire length of 84 00:06:22,100 --> 00:06:23,330 the accounts are released. 85 00:06:23,630 --> 00:06:27,920 And if the account happens to match the transaction ID, then return that. 86 00:06:28,310 --> 00:06:28,640 But. 87 00:06:29,540 --> 00:06:35,330 You know, loops are messy, it's better to use a stream pipeline, so here we'll say return accounts 88 00:06:35,540 --> 00:06:38,060 that stream the filter. 89 00:06:38,510 --> 00:06:40,310 We're going to filter the account. 90 00:06:41,870 --> 00:06:42,720 Whose idea? 91 00:06:45,210 --> 00:06:47,730 Matches the transaction ID being passed in. 92 00:06:50,920 --> 00:06:52,510 Place that on a separate line. 93 00:06:57,170 --> 00:07:00,500 There should only be a single account that matches the transaction ID. 94 00:07:00,590 --> 00:07:02,420 So here we're just going to say find first. 95 00:07:04,320 --> 00:07:06,870 So find the first element and the filter at array. 96 00:07:07,230 --> 00:07:12,240 And if they can't find anything, if the array happens to be empty, we'll say or else no. 97 00:07:26,870 --> 00:07:28,670 All right, now, we're going to test our method. 98 00:07:30,780 --> 00:07:32,370 By adding this line inside men. 99 00:07:39,280 --> 00:07:39,830 Over here. 100 00:07:49,240 --> 00:07:52,540 And for the transaction, IDC zero 70. 101 00:07:53,620 --> 00:07:55,840 We get the account where the transaction took place. 102 00:07:55,920 --> 00:08:01,570 See 070 perfect in the next video, we'll continue the remaining tasks.