The solution is going to cover part for.
We'll come back to the third test case, but the fourth one tells us to check if a movie is unavailable
after it's rented.
So I'll write a unit test named Rent a movie test.
And inside our rent out The Godfather.
And then our unit test is going to assert The Godfather isn't available after Rentoul assert false store,
get movie.
At index one is available.
OK, now inside, start our Java write code to make the test failed.
I'll define a function named Arunta movie.
It's a void and receives a string parameter name.
And for now, we'll do nothing.
Our test fails, that's fine, let's write code to make it pass.
Inside the movie, I'm going to create a loop that runs through the size of the array list.
And when it finds a name that matches the one being Pastan.
Then we'll set its availability to false.
If we were on our test now, it should work.
I can run the debugger.
Rent, movie updates, the availability to false and our assertion passes.
Now, can rent movies be simpler?
Absolutely.
Rent movies, doing way too much work, it's retrieving the index and it's setting the movie's availability
to false.
It should only be performing a single task.
So we'll create another method that's solely responsible for retrieving the movie index, a function
name is get movie index.
It returns an integer and receives a string parameter called name.
And inside this function, we're going to copy over the logic from rent a movie.
And if it doesn't find a movie, we still need to return and we'll use negative a thousand to indicate
that it couldn't find anything.
OK, and now we can all get moving index from rent a movie.
And this looks better, I like it when each function is only performing one task.
This is what I mean when I say Modula.
OK, if we rerun the test, it passes good, we don't have any bugs.
But we still have to ask the question, can we refactor?
And the answer is yes, loops are pretty messy.
And since Java eight, we can use something called an end stream.
What an end stream does is it goes through a pipeline of functions until it returns an integer that
represents an index.
First, you need to specify the range from which you're getting the index.
The range is going to start from zero until the size of the array list.
The next operation is filter.
Filter, expecta, lambda expression that returns a boolean, the lambda receives each index from zero
until the size of the array list.
We're going to call it I.
And for each index, we're going to return a boolean that checks if the name of the movie object at
the requested index.
Equals the parameter.
And Stream is going to filter the index for which this condition is true.
And now we need to return this index, so we'll use the terminal operation, find first, find first
is a terminal operation because it ends the pipeline by returning the first index for which this condition
is true.
And if fine first, does it find anything if all of the indexes return false?
Then we'll say or else.
Negative, a thousand.
And we'll return the result that comes out of this pipeline.
OK, as you can tell by now, I'm a pretty big fan of replacing Lupe's with a pipeline of functions
whenever I can.
And as always, as you refactor, run the unit test to make sure there aren't bugs and there aren't
any perfect.
So now we ask ourselves the same question, can it be a refactored and the answer is yes.
If your expression is only a single line, then you can remove the curly brackets as well as the return
keyword.
And Java is going to know you intend on returning the Boolean.
We run the unit test.
And beautiful.
Once again, can the code be refactored?
Nope, we're done.