1 00:00:00,300 --> 00:00:05,280 This solution is going to cover tasks one to four from part two, this part is exciting because we get 2 00:00:05,280 --> 00:00:11,130 to revisit 2D arrays and nested loops and the vending machine is identified by the items that it manages. 3 00:00:11,130 --> 00:00:13,460 And it has one action dispensing an item. 4 00:00:13,740 --> 00:00:19,650 And since the items are arranged in rows right there, we know that the field needs to be a tutelary. 5 00:00:20,280 --> 00:00:23,010 So add the items field to the machine class. 6 00:00:28,390 --> 00:00:33,610 And as always, if your class has fields, you need to apply the big three steps, we need a constructor, 7 00:00:33,610 --> 00:00:34,660 a getter and a setter. 8 00:00:35,350 --> 00:00:39,280 The constructor is public, as always, shares the same name as the class. 9 00:00:40,690 --> 00:00:46,960 And it's going to receive as many parameters as there are fields, so one, a 2D array of items whose 10 00:00:46,960 --> 00:00:50,110 values we're going to copy into the items field. 11 00:00:51,330 --> 00:00:54,240 So first, we need to set the field equal to a new 2D array. 12 00:01:00,600 --> 00:01:05,640 Remember that the first bracket is the number of rose, the items field is going to have the same number 13 00:01:05,640 --> 00:01:07,770 of rose as the items parameter. 14 00:01:12,690 --> 00:01:17,730 And the second bracket refers to the number of elements in each row, you can pick any row from the 15 00:01:17,730 --> 00:01:18,630 items parameter. 16 00:01:18,640 --> 00:01:21,660 It doesn't matter which row, they all have the same number of elements. 17 00:01:22,040 --> 00:01:23,280 So I'm going to zero zero. 18 00:01:25,600 --> 00:01:27,520 And we'll get the length of Rosero. 19 00:01:29,970 --> 00:01:36,000 OK, now we need to run through every object in the items parameter we can use for eye to autocomplete, 20 00:01:36,010 --> 00:01:36,720 the outer loop. 21 00:01:39,720 --> 00:01:45,570 And this loop is going to run through every row and the items parameter, where is the Arrow Index and 22 00:01:45,570 --> 00:01:48,330 once again we can use for it to complete the inner loop. 23 00:01:53,010 --> 00:01:59,040 But be careful as the outer loop runs through every row eye, the inner loop needs to run through every 24 00:01:59,040 --> 00:02:00,340 element in that row. 25 00:02:00,390 --> 00:02:00,780 I. 26 00:02:03,730 --> 00:02:09,340 Inside the inner loop, the euro index is going to be fixed to a row while the Innaloo counterplay j 27 00:02:09,340 --> 00:02:11,740 index is a specific element in that euro. 28 00:02:14,480 --> 00:02:23,060 And we're going to set every element I and spot J equal to a new items copy of every element in the 29 00:02:23,060 --> 00:02:26,300 items parameter at eye and spot J. 30 00:02:28,740 --> 00:02:32,970 And that's really the constructor is going to update every element in the items field with an object 31 00:02:32,970 --> 00:02:34,170 from the items parameter. 32 00:02:36,110 --> 00:02:37,490 Next, we want to get her. 33 00:02:51,910 --> 00:02:54,370 I receives two parameters, the row. 34 00:02:57,200 --> 00:02:58,760 And the spot within that row. 35 00:03:06,550 --> 00:03:08,200 And this letter is going to return. 36 00:03:10,950 --> 00:03:13,110 A new item copy of the object. 37 00:03:19,410 --> 00:03:22,680 At the request that spot inside the row, that was Pastan. 38 00:03:26,340 --> 00:03:27,210 All right, that's all. 39 00:03:29,060 --> 00:03:32,210 Next, we want to add a Seder that receives three parameters. 40 00:03:38,350 --> 00:03:44,740 An item object that you're going to use to update one of your elements, the row and spot of the object 41 00:03:44,740 --> 00:03:45,790 that you want to update. 42 00:03:51,240 --> 00:03:55,620 All right, then, we're going to end next the object at the requested spot inside the row. 43 00:04:02,660 --> 00:04:06,140 And set it equal to a new copy of the object being passed in. 44 00:04:14,590 --> 00:04:16,959 And not so I think we're ready to test our code. 45 00:04:19,220 --> 00:04:21,950 Inside men, Alan, comment, the items are Ray. 46 00:04:40,240 --> 00:04:43,090 Then I'll create a new object of the machine class. 47 00:04:54,040 --> 00:04:56,140 And I'll pass in the array to the constructor. 48 00:05:03,310 --> 00:05:05,020 And let's say I want to get this object. 49 00:05:06,240 --> 00:05:09,030 I'm going to use the GETER to get the object I wrote to. 50 00:05:11,640 --> 00:05:13,770 And one of the items field. 51 00:05:15,410 --> 00:05:17,270 Then I'm going to print that object directly. 52 00:05:29,500 --> 00:05:30,310 Run the code. 53 00:05:43,720 --> 00:05:49,210 And perfect, the constructor works because it clearly populated the items field with every item object 54 00:05:49,540 --> 00:05:50,820 and the getter works as well. 55 00:05:52,860 --> 00:05:57,930 Last thing I want to do is test the setar, so before putting the object at row and spot one, I'm going 56 00:05:57,930 --> 00:06:00,150 to change the price of that same object. 57 00:06:00,150 --> 00:06:01,530 So I'll store this in a variable. 58 00:06:17,630 --> 00:06:21,710 Change its price, so we'll call the set price. 59 00:06:28,590 --> 00:06:32,940 Then I'm going to call machine that said item with the new item we just updated. 60 00:06:36,980 --> 00:06:38,800 Row two and spot one in. 61 00:06:46,100 --> 00:06:47,400 All right, let's run the code. 62 00:06:47,900 --> 00:06:51,380 I'm anticipating that the updated items would have a price of two ninety nine. 63 00:06:59,670 --> 00:07:04,920 And this output, I can confirm that the constructor getter and setter are all working as they should. 64 00:07:05,970 --> 00:07:09,900 I hope you were able to follow along if you were having trouble with the concept of two. 65 00:07:10,590 --> 00:07:13,950 I encourage you to revisit the array section in Module one. 66 00:07:14,310 --> 00:07:17,730 We cover 2D arrays and nested loops in a lot of detail. 67 00:07:18,450 --> 00:07:22,860 But if you're comfortable with what we did so far, then we're going to implement tasks five to seven 68 00:07:22,860 --> 00:07:23,700 in the next video.