This video is going to cover the solution for part three, looking back at the requirement to be classified
as another type of object, the store and the store is identified by the movies that it manages.
A store also has four actions, adding movies, selling them rent and returning them.
We'll start by adding the field, the movie's field is plural, which implies an array of movie objects,
but once again, we need to use an array list because the number of elements can change, movies can
be added and they can be removed.
And always if a class has fields, you need to apply the big three steps.
We need to add a constructor, a getter and a setter.
The constructor is public shares the same name as the class and it receives no parameters.
Because in the beginning, the list of movies should be empty.
We're just going to set the field equal to a new array list.
That can store movie objects.
Now we want to add a getter that receives one parameter.
The index of the movie object that they're trying to access, and this Geter is going to return a new
copy.
Of the movie Object at the requested index.
Now we want at a Seder that receives two parameters.
The index of the element you want to updates and a movie object that we're going to update the element
with, then we're going to set the element at the requested index.
Equal to a new copy of the movie Object being passed on.
OK, that's all for the Big Three Steps now we're going to add actions.
One action that the store can perform is at a movie to its collection, I'll create a void method named
ad movie receives one parameter, a movie object.
And inside the contacts array list, we're going to add a new copy of the movie Object Being Pastan.
OK, the next action that it can perform is telemovie, the method is Void, called Sell Movie.
And it receives one parameter, the name of the movie you want to sell.
So what we'll do is credit for a loop that runs through the size of the array list.
Remember, that array lists have a size knot length.
And inside the fur loop, we're going to get the movie object at every index I.
We'll get the name failed of that movie and we'll check if it equals the name of the movie, that was
Pastan.
And if that's the case.
Then we're going to remove the movie from their list.
We'll do the same thing for that movie.
It's also void.
And it receives one parameter, the name of the movie you want to rent.
And once again, we'll need another for a loop that runs through the size of the array list, and then
we need to get the movie object that every index I.
Get the name field of that movie.
And we'll check if it equals the name of the movie that was Pastan.
Wait a second.
The code that I'm writing now in this method is identical to the code that I wrote in cell movie.
And if you think about it, even the signature is identical, they're both void and they receive the
same parameter.
And guess what, the code for returning a movie is also going to be identical.
So instead of having three functions that are doing the exact same thing.
We're going to create one function that's void.
Named action, the first parameter is going to be name because all three methods are going to require
a name and the second parameter is a string that represents the type of action they want to perform.
Once again, will create a for loop that runs through the size of the array list.
We're going to get the movie object that every index I.
We'll get the name filled of that movie.
And we'll check if it equals the name of the movie that was Pastan.
And if this condition is true, what we do is going to depend on the action parameter, so here I'll
create a switch statement that runs against a list of cases.
If the cases sell.
Then we're going to remove the movie from the collection.
If the case is rent.
Then we're going to set the availability of that movie to false.
And if the cases return.
Then we're going to set the availability of that movie to trip.
Don't forget to add the keyword at the end of each case.
Kate, now our final task before we can test our code is that a two string method that returns a nicely
formatted string of every movie in the array list, as always, public string to string.
I'll create a string named temp for a temporary.
It's going to be an empty string for now, when we return, it's.
First will create a loop that runs through the size of the array list.
And during each run, I'm going to get the two string of the movie object at the index eye and add the
string to the temp variable that we're returning.
And after adding the two string of the movie, we can add two lines so that we can separate it from
the next two string they got to Pastan.
And that's it now we got to test our code inside main kryten object of the store class.
And the store is going to start by adding three movies, I'm going to use the values from the article.
Store, ad movie.
I'll add the first one, The Shawshank Redemption.
And I'm going to copy this format for the two other movies.
And now we can just print the story.
And good returns, a nicely formatted string of every movie in the movie store, so the ad action works.
Now I'm going to test the cell action.
I'm going to sell The Godfather.
And Perfect's, after selling The Godfather, it gets removed from our collection.
Now I'm going to rent out The Shawshank Redemption.
We run the code.
And The Shawshank Redemption is not in stock anymore.
Beautiful.
That is all for part three.
I'll see you in part for.