In this lesson, we're going to finalize the application.
The data contains shirts and pants, and you can store each line as an object of the product class,
so I'm going to start by removing this code.
Could a function that returns a product to Ray?
Called Get Data.
And it's going to throw a file not found exception.
All right, now create a new object of the Phalen input stream class.
And the family bitstream is going to connect to products, not text the file name.
And now we can create a new object of the scanner class that's going to pick up the data from the input
stream.
OK, looking back, there are 18 arrows, so we'll need to create a product array that can store 18
objects.
Now, you might think Tikrit a while loop while scandal has next line, but we need a counter that keeps
track of every row so that we can populate the array properly.
So here is a for loop instead.
For it is equal to zero and the loop is going to keep running as long as Skåne has a next line.
Plus, plus.
OK, great.
Now, looking back at the file, notice that the first piece of data tells us what the object is.
So we'll pick up the first value in that line using Scandi next and put it in a switch statement if
that value happens to be pant's.
We're going to set the element at that index I.
Equal to a new object of the class.
Looking back at the data, we got to pick up an ENT double string string next and.
Next double.
Next.
If that value happens to be a shirt.
We're going to set that element and index I.
Equal to a new object of the class, looking back at the data, we got to pick up a string, double
string string.
So we'll say next.
Next double.
Next and next.
Now, this gives an error because this constructor doesn't exist.
The constructor expects an item type, but we're passing in a string.
What you can actually do is get an enum value from a string, so from the item size will get the enum
value that corresponds to scan next.
So if scandal next to small, we're going to get back in value of small.
If it's medium, we'll get back medium anyways, that's really a.
And so we populated the products array with shirts and pants, that's polymorphism for you right there,
because each child object is taking the form of its parent.
Now, I'll close scanner.
In return, the product to Ray.
And perfect.
That was the hard part.
Now we get to enjoy the fruits of our labor.
Inside, man, we're going to try to call the get data function and store the result in an array.
And will catch the exception if the code happens to fail.
And will print the exception that it comes with it.
And assuming everything works, you're going to sort the array, every object in that array, we know
implements the comparable interface and so the array is going to be sorted alphabetically and by price
because that's how we designed it.
Now run through every product in the product array and print it.
OK, we'll put a break point here, run the debugger.
Step into the function.
OK, first one, a shirt, so with is a shirt object.
Next one is so a store is a pants object.
And I want to skip the rest.
So I'll put another break point and press next.
Here is our array, fully populated.
We'll keep going now, our Arae got sorted pans show up first.
Shirt after in the array is also sorted based on price, that's perfect, will print it.
And that's kind of ugly.
It wouldn't hurt to touch things up.
Go to shirt that java, remove the brackets, they're not various statically pleasing.
A white shirt in the beginning and put a tab of space before each value.
I think they should look pretty nice.
And I'll do the same thing for Pentz.
I'll give you some time to pause and get that done.
OK, I'll rerun my app.
And beautiful.