This solution is going to cover tasks one to four from part two, this part is exciting because we get
to revisit 2D arrays and nested loops and the vending machine is identified by the items that it manages.
And it has one action dispensing an item.
And since the items are arranged in rows right there, we know that the field needs to be a tutelary.
So add the items field to the machine class.
And as always, if your class has fields, you need to apply the big three steps, we need a constructor,
a getter and a setter.
The constructor is public, as always, shares the same name as the class.
And it's going to receive as many parameters as there are fields, so one, a 2D array of items whose
values we're going to copy into the items field.
So first, we need to set the field equal to a new 2D array.
Remember that the first bracket is the number of rose, the items field is going to have the same number
of rose as the items parameter.
And the second bracket refers to the number of elements in each row, you can pick any row from the
items parameter.
It doesn't matter which row, they all have the same number of elements.
So I'm going to zero zero.
And we'll get the length of Rosero.
OK, now we need to run through every object in the items parameter we can use for eye to autocomplete,
the outer loop.
And this loop is going to run through every row and the items parameter, where is the Arrow Index and
once again we can use for it to complete the inner loop.
But be careful as the outer loop runs through every row eye, the inner loop needs to run through every
element in that row.
I.
Inside the inner loop, the euro index is going to be fixed to a row while the Innaloo counterplay j
index is a specific element in that euro.
And we're going to set every element I and spot J equal to a new items copy of every element in the
items parameter at eye and spot J.
And that's really the constructor is going to update every element in the items field with an object
from the items parameter.
Next, we want to get her.
I receives two parameters, the row.
And the spot within that row.
And this letter is going to return.
A new item copy of the object.
At the request that spot inside the row, that was Pastan.
All right, that's all.
Next, we want to add a Seder that receives three parameters.
An item object that you're going to use to update one of your elements, the row and spot of the object
that you want to update.
All right, then, we're going to end next the object at the requested spot inside the row.
And set it equal to a new copy of the object being passed in.
And not so I think we're ready to test our code.
Inside men, Alan, comment, the items are Ray.
Then I'll create a new object of the machine class.
And I'll pass in the array to the constructor.
And let's say I want to get this object.
I'm going to use the GETER to get the object I wrote to.
And one of the items field.
Then I'm going to print that object directly.
Run the code.
And perfect, the constructor works because it clearly populated the items field with every item object
and the getter works as well.
Last thing I want to do is test the setar, so before putting the object at row and spot one, I'm going
to change the price of that same object.
So I'll store this in a variable.
Change its price, so we'll call the set price.
Then I'm going to call machine that said item with the new item we just updated.
Row two and spot one in.
All right, let's run the code.
I'm anticipating that the updated items would have a price of two ninety nine.
And this output, I can confirm that the constructor getter and setter are all working as they should.
I hope you were able to follow along if you were having trouble with the concept of two.
I encourage you to revisit the array section in Module one.
We cover 2D arrays and nested loops in a lot of detail.
But if you're comfortable with what we did so far, then we're going to implement tasks five to seven
in the next video.