1
00:00:00,390 --> 00:00:02,880
In this video, I'm going to cover the solution for part three.

2
00:00:04,570 --> 00:00:09,280
The end goal is to write up a string method that returns a nicely formatted string of every item in

3
00:00:09,280 --> 00:00:10,240
the vending machine.

4
00:00:11,380 --> 00:00:16,360
So task one was to create the two string method, as always, public string.

5
00:00:17,710 --> 00:00:18,400
To string.

6
00:00:23,920 --> 00:00:28,000
And I'm going to create a string variable named temp that equals an empty string for now.

7
00:00:31,560 --> 00:00:32,430
In return, it.

8
00:00:39,910 --> 00:00:44,920
So first, create a nested loop that runs through every single item in the items field, so the outer

9
00:00:44,920 --> 00:00:51,100
loop country is going to index every row and the inner loop countered J is going to index every single

10
00:00:51,100 --> 00:00:52,840
element in a row I.

11
00:00:53,790 --> 00:00:54,240
OK.

12
00:00:57,150 --> 00:01:01,350
Inside the inner loop, I'm going to index every single element in a two day array.

13
00:01:03,260 --> 00:01:04,519
Get its two string.

14
00:01:07,850 --> 00:01:10,670
And append it into the temp variable that we're returning.

15
00:01:14,810 --> 00:01:19,940
All right, two string should now return a string description of every single items object in the vending

16
00:01:19,940 --> 00:01:20,400
machine.

17
00:01:20,870 --> 00:01:23,210
So back in Maine, I'm going to print the vending machine.

18
00:01:28,440 --> 00:01:29,280
Run the code.

19
00:01:42,030 --> 00:01:44,520
And it prints every single item in the items array.

20
00:01:44,670 --> 00:01:46,830
That's a good start, but we can do a lot better.

21
00:01:47,550 --> 00:01:51,330
That's why Task three was that spacing between each row of items.

22
00:01:55,980 --> 00:02:01,080
So as the outer loop is fixed, the one row, the inner loop is going to loop through every element

23
00:02:01,080 --> 00:02:07,230
in that row, at which point after it goes through an entire row of items, we're going to add two lines,

24
00:02:07,230 --> 00:02:08,009
separators.

25
00:02:15,680 --> 00:02:17,090
All right, rewriting the code.

26
00:02:26,730 --> 00:02:32,430
After the inner loop runs through a row of items, we're adding to line separators before appending

27
00:02:32,430 --> 00:02:34,590
the next throw, perfect.

28
00:02:36,830 --> 00:02:42,380
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

29
00:02:42,380 --> 00:02:46,490
spacing before the inner loop appends the next throw of items.

30
00:02:48,110 --> 00:02:50,270
Temp plus equal tab of space.

31
00:02:56,250 --> 00:02:57,270
Rewriting the code.

32
00:03:06,290 --> 00:03:07,720
And this is looking really good.

33
00:03:11,860 --> 00:03:16,580
Now, I don't like how the bracket is squished with the two string that follows up, so we're going

34
00:03:16,580 --> 00:03:18,430
to out to space after the bracket.

35
00:03:26,060 --> 00:03:26,930
We run the code.

36
00:03:30,370 --> 00:03:31,860
And this looks a lot better.

37
00:03:34,420 --> 00:03:39,280
All right, now, the last step is purely for aesthetic purposes, after the entire loop runs and spends

38
00:03:39,280 --> 00:03:44,830
every item in the two tray, it would be nice to add the following line of stars as some kind of border

39
00:03:44,830 --> 00:03:47,950
for the vending machine so we can copy this over.

40
00:03:59,540 --> 00:04:00,440
Run the code.

41
00:04:09,060 --> 00:04:09,780
And we're done.

42
00:04:13,010 --> 00:04:18,500
By virtue of just printing the machine object, Java internally calls the two string method, which

43
00:04:18,500 --> 00:04:23,540
returns a nicely formatted string of every item in the vending machine, and that's all.

