All right, so we're done the hard part, we're done designing all of our classes, we wrote all of
our unit tests.
Now we just glue everything together inside men.
So the first task was to create a new bank object.
We're done that already.
And the second task was to define a method that creates an account object and returns it.
So what I'm going to do is define a public static method that returns an account.
Called Create Object.
And this method expects an array of string values, and from these values, it's going to create an
object.
Now you can expect the era of string values to have four elements.
For example, let me just copy and paste one of those as a comment into here so you can expect an array
with four elements where the first element tells us what kind of object we want to create, whether
it's checking savings or loan.
The second, third and fourth elements are actually used to create the object.
So what we can actually do here is approach this one or two ways.
We could create a switch statement that compares the first element against three possible cases.
Case checking.
And here we would create a checking object.
Based on these values, we could do case savings.
And create a Settings object.
Or we could even do case lone.
And then create a lone object.
There is a reason why I don't like this method, and that's because it promotes copying and pasting,
because if you think about it, let's just say the case is checking, then here you would just return
new checking.
Value add index one.
Index two and three.
There you would say, double that price double from the string value because our constructor expect
to double.
Now, so far, it's fine, but the issue is that savings and loan, they have the exact same constructor
as checking.
So you could literally just copy and paste this.
And then just change the class name.
But you know, me, I'm not a big fan of copying and pasting in this case, I could permit it because
it's not always guaranteed that different classes are going to have the same constructor.
But in this case they do.
And you know me, I'm not a big fan of copying and pasting.
So what I'm going to do is actually show you a dynamic way of creating objects.
Go back to learn the part, I left you the syntax right here.
Well, it over.
So here I'm going to return.
Following.
First, you want to specify the class that you want to create an object from the class name includes
the directories that you have to drill into until you get to it.
Let's just say I want to reference the checking class.
We would need to drill into source dot main notice here.
It's not slight.
That's just how you need to pass it into four name.
So I would need to go to source Typekit main Typekit model.
Typekit account.
And here I would use Typekit checking.
So to the string, I'm going to append dot checking.
But obviously, we don't want to do that.
We want to put values at index zero.
So here we're specifying the class that we want to create an object from.
This could be either checking savings or loan.
And then we want to get the constructor for that class.
Lucky for us, checking savings and loan, all of them share the same constructor string string double.
And this line is where you basically pass parameters into the constructor.
Here I'll pass.
The idea?
The person's name.
And their balance.
We're getting an error because our function expects us to return an account object, but this isn't
sure what type of class you're creating an object from.
So we need to type, cast to account.
All right.
And now we get a lot of exceptions.
Now we're not going to throw all of these exceptions separately, that would be a pain.
Instead, consider that the exception class is the base class for every single exception, whether it's
checked or unchecked.
In other words, we can treat every exception in Java as type exception.
All right, now, the last thing I want to stress is let's assume we got these values into the values
parameter.
This would be no different than writing this return new checking.
Values one, values two and values three.
That's all.
But since we want to avoid repetition, we don't want to say new checking, new savings, new loan.
We can use this syntax to create objects on the fly to dynamically create objects.
All right.
What I want to do is confirm that this works, so I'm going to call this method for men.
We'll say create object an inside or in a pass in an array of string values.
Glenn Close, all of these and double quotes.
We get an error because this throws exception.
So what we're going to do is try to run the code.
And catch the exception if it fails, so we'll catch the exception.
And just print it for now.
E get message.
All right.
And a breakpoint right here and make sure that an object gets created.
We get an illegal argument, exception, argument type mismatch.
Oh, that makes a lot of sense.
Assume are creating an object of the checking class the constructor expect to string string and double.
As specified here, string string double, but we're passing in string, string and string.
Accordingly, it throws an exception.
We need to pass this argument into a double before passing it into the constructor.
OK.
Really useful printing that message, by the way.
Let's hope this works.
And it did, but we don't see anything because we're going to have to store this in a variable, so
what I'll do is I'll just say count.
Account is equal to create object restart the runtime.
And wow, this is flood.
I'll put a random print statement.
Try it again.
There it is.
Perfect.
Now we have a way of dynamically creating checking savings and loan objects.
All right now, task for us to define a method that loads every single account from the accounts text
file into the bank.
So what we're going to do is define a method called load accounts.
We'll say public static void load accounts.
It's going to receive an array list of account objects.
And inside of this method, we're going to run through every single accounts in the accounts are list.
And the reason why things aren't working is because we're going to have to import the aerialist class.
And for every account inside of the accounts list, we're going to add it to the bank bank account accounts.
This is great and all, but where are we going to get the ArrayList of accounts from?
We need a method that returns every single account from the accounts that tax filing.
So inside, man, Java will create another method called return accounts public.
Static, this method returns in a rare list of accounts.
It returns the error list of accounts that we're going to use over here.
Return accounts.
And I already know it's going to throw a file not found exception, so we'll just do it right now.
And now inside this method, we're going to create a new object of the final input stream class fan
input stream if this is equal to new file input stream and this file input stream is going to connect
to the accounts file.
And then we're going to create a new object of the scanner class scanner scan is equal to a new object
scanner that receives input from the file input stream.
OK.
Don't forget to import it.
And then we'll initialize the array, a list of accounts that we're going to populate array list accounts
accounts is equal to a new object of the ArrayList class that can store account objects.
And now we want to scan every single line in the accounts file, so we'll say, well, Scan Dot has
next line while the accounts file has a next line, we're going to add it to the list of accounts accounts
don't add.
And expects an account object to here will create objects and create objects, expects an array of four
values.
And if you look back at the accounts file, every line contains four values separated by a comma.
How do we split the line into four values based on a comma?
We can scan the entire line, scanned our next line and then say, does split.
And we'll use the comma separator to split the line into an array of string values.
OK.
The last thing we got to do is just return the error list return accounts.
And it expects us to handle the exception here, but you know what, I think it might make more sense
to go back to create objects and try to run the following code.
If it happens to fail, our catch method is going to catch the failure catch exception.
Here we're just going to print the message.
E get message and return, no.
We're catching the exception from inside the function, which means the function is not going to throw
an exception anymore.
Whoever is calling it, they can rest assured the exception gets caught in here and we don't need this
anymore as well.
OK, now what I want to do is call this method for men.
We'll try to run the code.
Return accounts will store the result inside of an ArrayList variable that can store account objects.
And if the code happens to fail, we're going to catch the file not found exception and print the message.
E get message.
I'll put a random prince statement before starting the debugger.
And if everything works, we should see 16 elements.
So we'll debug the runtime.
Step over.
And indeed, we do.
This is perfect in the next video, we'll continue the remaining tasks.