Now we're going to finalize the main class, the next thing we got to do is add a method that returns
every transaction from transactions that text but as a transaction object.
So what we'll do is define a public static method that returns an array list of transaction objects.
The method is going to be called return transactions.
And we already know it's going to throw a file not found exception.
So we'll throw it right now.
OK.
First thing we'll do is create a new object of the Phalen bitstream class.
That connects to the transactions file.
Then we'll create a new object of the scanner class.
That receives input from the foreign input stream.
OK.
And now what we want to do is create the array list of transaction objects that were eventually going
to return array list.
Transaction.
Transactions.
Is equal to an.
Is equal to a new ArrayList that can store transaction objects.
And now what I want to do is create a while loop that runs through every single line in the transactions
file.
So while scanned as a next line.
And as long as there is a next line, we're going to pick it up, scanned our next line.
And if you look back at transactions dot text, we can split every line into four values.
They're all separated based on a comma.
So here we can say scanned.
Our next line split.
Around a comma.
And what that's going to do is return an array of four string values for each line.
And then using these values, we can add a new transaction object to the transactions ArrayList transactions
and there will create a new transaction objects.
Now if you look back at the transaction constructor, first, we're expecting an enum type.
We'll have to say transaction type.
And that tape can be one of two constants withdraw or deposit, we can get one of these constants by
using the value of index one.
So here we'll say transaction type the value of.
Values at index one.
The next parameter is long time stamp, which is that index is zero.
Here will say values at index zero, but the constructor expects a long.
So what will have to do is say.
Long, dark, past long.
And we're going to pass along out of this string value.
OK, next parameter is a string ID.
We can just say values at index to.
Values at Index three, make sure to pass a double from the following string.
I think we're missing a bracket.
And that's pretty much it.
That's why Loop should populate the transactions array list with three hundred and fifty two transactions.
But before we return, the transactions are our list.
We want to sort it.
We'll say collections does sort.
And we will sort the transactions are a list based on time stamp.
Close, Skinner.
And return transactions.
OK, before we move on, we want to make sure this method works by going over to men and instead of
calling return accounts, call or return transactions.
Switch this to an array a list of transactions.
OK.
At a break point, debug the runtime.
If this works, we should get an array list of three hundred and fifty two sorted transactions.
Seems like it works.
Are they all sorted based on time stamp?
Indeed, they are.
This is wonderful.
OK.
So we were able to return every single transaction from the transactions file, but as an object, the
next step is to create a method that executes transactions.
So I'll create a public static void method called run transactions.
They receives the array list of transactions we want to execute.
And inside the function, what I'm going to do is run through every single transaction in the transactions
array list.
And here I'll say bank, don't execute transaction transaction.
And it's really nice if everything is coming together.
If you look back at transactions, not text, every transaction has an ID.
The ID determines in which account this particular transaction is going to take place.
This transaction is going to take place in the account 73 DB, which would be.
A checking account.
So what I want to do in Maine is define a method that gets every single transaction that belongs to
a particular account and print the transaction history.
The method is public static void transaction history.
It receives a strong aid, it receives the ID of the account, whose transactions we want to print.
And there's really nothing meaningful in this function, it's all aesthetics, and I left you all the
aesthetics inside of learning the parts, so we'll copy it over.
Copy over this entire comment.
And first thing we want to print is this fancy message.
Six out.
And your quotes.
Next thing we want to print is every transaction that corresponds to the I.D..
So what we want to do is create a for loop.
Here we'll call banks get transactions will pass and the account ID, whose transactions we want to
get.
So this is going to return an array of transactions for a particular account.
And so here we want to run through every single transaction in this array of transactions.
And for every transaction that we run through, we're going to wait.
Three hundred milliseconds.
And print the following message.
OK, now we just got to print the following messages.
Print after tax.
And here we need to fill our string with an account that corresponds to the ID, so we'll just say bank
get accounts.
I'd.
So ultimately, all this function is doing is running through all of the transactions for a particular
account.
And here we're getting that account and printing, it's two string.
At this point, we're pretty much done, all we got to do is copy this inside of our main method and
test our code.
I can't wait.
So there's nothing too crazy going on here.
First, we're just grabbing every single account from the accounts that tax file.
We're loading all of these accounts into the bank object.
Then we're grabbing every single transaction from any transactions file.
We're executing on any transactions from the bank object.
And then after all, the withdrawals and deposits have been made, the bank deduct taxes and then we're
running through every single account in the accounts ArrayList.
We're printing the two string of that account and then we're printing the history of all transactions
that occurred at this account.
I can't wait to test this out.
Let's just press the run button.
And this is looking pretty good.
I really hope you enjoyed this challenge and learned a lot by trying to solve it.
This is definitely the hardest challenge in this course, and this wraps up inheritance and polymorphism.