Welcome to Part seven.
We're going to finalize the bank class, the first task was to create an add transaction method.
This method is going to be public void called transaction.
And it's going to receive a transaction object.
This transaction object we're going to add to the transactions are a list, so we'll say this that transactions
that add and we're going to add to the area list a copy.
A new transaction copy of the object being passed in.
Now, if you go back to the data, go to transactions that text, every transaction has an I.D. that
matches one of the accounts.
So what we need to do is create a method that returns an array of transactions that match a particular
account.
So we'll go to Bank Dot Java.
And we'll call it.
Get transactions, so it's going to return an array of transactions.
Called Get Transactions, it's going to receive an account ID.
Whose transactions we want to return?
Now you might think to credit for a loop that runs through the entire transactions array list and FAA
transactions, it happens to match the account ID added to the array and return that.
But that's really messy.
I would rather use a stream pipeline.
So we're going to say this dart transactions that stream.
And we're going to run the stream of elements through a pipeline of operations.
The first operation we're going to use is the familiar filter operation, and inside we'll go through
every single transaction and filter the ones whose I.D..
Get ID matches, the account ID being passed in.
So at this point, the stream of elements consists of only the filtered transactions, and we're going
to collect this updated stream of elements into a list.
So from the list of collectors.
We're going to choose to list.
This terminal operation is going to return our filtered sequence of elements into our list, so we're
going to say list.
Transaction.
And into this list of objects, we're going to store the result of this pipeline.
Cannot convert from list transaction to a list transaction.
Oh, we got an import list.
All right.
And now this method expects an array, so what we're going to do is return list to array.
Now, the problem is that two Iraq returns an array of objects, which is not good, mortuary can actually
receive an argument and inside the argument, you need to pass in an array of type transaction.
That's how it's going to know, OK, I need to return an array of that custom type.
And when you're creating this array, here is where you put the length in this case, what would you
put a length of one, a length of two, a length of three?
Well, if you want a copy over every element from this list, the length of the array needs to be the
size of the list.
This is the workaround that I use to make sure to array returns an array of a custom type, not of the
generic object type.
OK, now we want to test our code to back on during the Part two, we'll copy this over into main.
Maybe we'll need to import some stuff.
Of course we do.
We'll import transaction.
Should cover all of that will import.
We'll need to add a bank object, so here we'll say bank bank make it static because we're inside main.
OK.
Import Bank.
Is equal to a new object of the bank class.
And now you may not be used to the syntax, but it's just a for a loop that runs through every single
account in the accounts array.
And this one runs through every single transaction in the transactions array.
Like this would be the exact same thing as saying This is just that here.
We don't have to deal with counters.
You can just use the element as is, if you want to use a normal for a loop and say, bank dot transaction
transactions, I mean, that's also fine.
You can do whatever you want.
I'm just going to stick with the code we got from learn the parts.
And anyways, here it adds a bunch of accounts to the bank object.
Here it adds a bunch of transactions to the bank object.
And the idea is that this line over here should filter the transactions that match the account.
F8 4c.
So there should be a total of one two three four five.
Anyways, will place and breakpoint over here.
And test it out.
I'll have to step over this line.
And beautiful.
One, two, three four five transactions.
Let's make sure they're the right ones.
Withdraw a deposit, withdraw, withdraw a deposit, OK?
Our function works perfectly.
OK, now back inside Bangkok, Java, we're going to add the logic for getting the account for a particular
transaction.
So what I'll do here is, I'll say public.
Account.
We're going to get an account corresponds to a transaction I.D..
And once again, you might think to just create a for a loop that runs through the entire length of
the accounts are released.
And if the account happens to match the transaction ID, then return that.
But.
You know, loops are messy, it's better to use a stream pipeline, so here we'll say return accounts
that stream the filter.
We're going to filter the account.
Whose idea?
Matches the transaction ID being passed in.
Place that on a separate line.
There should only be a single account that matches the transaction ID.
So here we're just going to say find first.
So find the first element and the filter at array.
And if they can't find anything, if the array happens to be empty, we'll say or else no.
All right, now, we're going to test our method.
By adding this line inside men.
Over here.
And for the transaction, IDC zero 70.
We get the account where the transaction took place.
See 070 perfect in the next video, we'll continue the remaining tasks.