In this video, I'm going to cover the solution for part three.
The end goal is to write up a string method that returns a nicely formatted string of every item in
the vending machine.
So task one was to create the two string method, as always, public string.
To string.
And I'm going to create a string variable named temp that equals an empty string for now.
In return, it.
So first, create a nested loop that runs through every single item in the items field, so the outer
loop country is going to index every row and the inner loop countered J is going to index every single
element in a row I.
OK.
Inside the inner loop, I'm going to index every single element in a two day array.
Get its two string.
And append it into the temp variable that we're returning.
All right, two string should now return a string description of every single items object in the vending
machine.
So back in Maine, I'm going to print the vending machine.
Run the code.
And it prints every single item in the items array.
That's a good start, but we can do a lot better.
That's why Task three was that spacing between each row of items.
So as the outer loop is fixed, the one row, the inner loop is going to loop through every element
in that row, at which point after it goes through an entire row of items, we're going to add two lines,
separators.
All right, rewriting the code.
After the inner loop runs through a row of items, we're adding to line separators before appending
the next throw, perfect.
All right, now the next step is to add a tub of space before a trip, so we're going to add a tub of
spacing before the inner loop appends the next throw of items.
Temp plus equal tab of space.
Rewriting the code.
And this is looking really good.
Now, I don't like how the bracket is squished with the two string that follows up, so we're going
to out to space after the bracket.
We run the code.
And this looks a lot better.
All right, now, the last step is purely for aesthetic purposes, after the entire loop runs and spends
every item in the two tray, it would be nice to add the following line of stars as some kind of border
for the vending machine so we can copy this over.
Run the code.
And we're done.
By virtue of just printing the machine object, Java internally calls the two string method, which
returns a nicely formatted string of every item in the vending machine, and that's all.