1
00:00:00,390 --> 00:00:02,250
The solution is going to cover part three.

2
00:00:05,340 --> 00:00:11,310
The second test case tells us that if a movie is removed after being sold, so write a unit test named

3
00:00:11,310 --> 00:00:12,450
sell movie test.

4
00:00:28,500 --> 00:00:31,170
And the movie we're going to sell is The Shawshank Redemption.

5
00:00:43,800 --> 00:00:48,270
So ideally, calling cell should remove this object from the array list.

6
00:00:52,870 --> 00:00:54,820
All right, and now we'll need to make an assertion.

7
00:00:57,050 --> 00:01:02,960
Assert false, and this test will pass a Astorga that contains The Shawshank Redemption returns false.

8
00:01:12,900 --> 00:01:16,380
And now inside Staudt Java, we can write code to make the test fail.

9
00:01:17,130 --> 00:01:18,960
Public Void Cell Movie.

10
00:01:22,810 --> 00:01:26,440
And the unit test implies that we need to pass a string into our method.

11
00:01:34,930 --> 00:01:37,570
And for now, we'll do nothing, I want my test to fail.

12
00:01:44,860 --> 00:01:49,000
And it does good now we need to write code to make the test pass.

13
00:01:51,720 --> 00:01:55,950
The simplest code I can think of is to just create a loop that runs through the size of the array list.

14
00:02:03,940 --> 00:02:06,880
And if the name of the movie object at the index.

15
00:02:13,050 --> 00:02:14,760
Matches the name being Pastan.

16
00:02:17,540 --> 00:02:19,490
Then we'll remove the movie and index I.

17
00:02:27,500 --> 00:02:28,460
Let's run the test.

18
00:02:33,720 --> 00:02:34,590
And it passes.

19
00:02:48,090 --> 00:02:52,950
Here, it removes the movie and our assertion passes because the story doesn't contain it anymore.

20
00:02:58,470 --> 00:03:03,480
OK, here comes the most important question can this be a refactored, can it be made simpler?

21
00:03:04,020 --> 00:03:07,470
And the answer to that is absolutely loop's are really messy.

22
00:03:07,470 --> 00:03:11,010
And since Java eight aerialists and call a remove F method.

23
00:03:15,280 --> 00:03:17,260
They stop movies that remove if.

24
00:03:22,030 --> 00:03:26,170
The remove, if once a predicate, a predicate is something that is true or false.

25
00:03:26,200 --> 00:03:29,980
So that means our lambda expression is going to receive every element as a parameter.

26
00:03:31,350 --> 00:03:35,340
Remember that every lambda expression has an arrow that points to a piece of code.

27
00:03:40,840 --> 00:03:45,790
Where we need to return a boolean and we're going to return a boolean that checks, if the string of

28
00:03:45,790 --> 00:03:49,020
the element we're running through is equal to the name parameter.

29
00:03:49,270 --> 00:03:53,500
And as you can see, using a functional approach is more elegant than using loops.

30
00:03:53,810 --> 00:03:58,480
And as we refactor, we always have to run the unit test to make sure that the new could be added does

31
00:03:58,480 --> 00:04:01,840
not have any bugs and perfect the test passes.

32
00:04:08,760 --> 00:04:10,900
Now, once again, can we refactor?

33
00:04:10,950 --> 00:04:16,079
That's a question you have to keep asking yourself until you're absolutely certain that your code is

34
00:04:16,079 --> 00:04:18,750
as simple as it's going to get in this case.

35
00:04:18,760 --> 00:04:25,350
Yes, if the code is only one line, then you can remove the curly brackets as well as the return keyword.

36
00:04:25,570 --> 00:04:28,470
And Jeff is going to know you intend on returning a boolean.

37
00:04:28,620 --> 00:04:29,970
That's kind of cool if you ask me.

38
00:04:35,370 --> 00:04:37,020
OK, I'll run the unit test.

39
00:04:40,500 --> 00:04:46,790
And beautiful, the unit test passes, so I know that my code is working as intended now.

40
00:04:46,800 --> 00:04:48,780
Once again, can we refactor?

41
00:04:49,080 --> 00:04:51,840
Nope, we can move on to the next unit test.

