1 00:00:00,390 --> 00:00:02,220 The solution is going to cover part for. 2 00:00:06,600 --> 00:00:11,190 We'll come back to the third test case, but the fourth one tells us to check if a movie is unavailable 3 00:00:11,190 --> 00:00:12,180 after it's rented. 4 00:00:12,780 --> 00:00:15,450 So I'll write a unit test named Rent a movie test. 5 00:00:24,840 --> 00:00:27,240 And inside our rent out The Godfather. 6 00:00:36,600 --> 00:00:43,260 And then our unit test is going to assert The Godfather isn't available after Rentoul assert false store, 7 00:00:43,560 --> 00:00:44,280 get movie. 8 00:00:46,960 --> 00:00:49,210 At index one is available. 9 00:00:52,840 --> 00:00:56,350 OK, now inside, start our Java write code to make the test failed. 10 00:00:56,470 --> 00:00:58,630 I'll define a function named Arunta movie. 11 00:01:00,020 --> 00:01:02,810 It's a void and receives a string parameter name. 12 00:01:08,900 --> 00:01:10,090 And for now, we'll do nothing. 13 00:01:15,580 --> 00:01:19,180 Our test fails, that's fine, let's write code to make it pass. 14 00:01:21,560 --> 00:01:25,970 Inside the movie, I'm going to create a loop that runs through the size of the array list. 15 00:01:34,490 --> 00:01:37,820 And when it finds a name that matches the one being Pastan. 16 00:01:51,010 --> 00:01:53,230 Then we'll set its availability to false. 17 00:02:02,190 --> 00:02:04,320 If we were on our test now, it should work. 18 00:02:12,840 --> 00:02:14,130 I can run the debugger. 19 00:02:30,380 --> 00:02:34,670 Rent, movie updates, the availability to false and our assertion passes. 20 00:02:39,920 --> 00:02:41,330 Now, can rent movies be simpler? 21 00:02:41,450 --> 00:02:42,290 Absolutely. 22 00:02:42,320 --> 00:02:48,230 Rent movies, doing way too much work, it's retrieving the index and it's setting the movie's availability 23 00:02:48,230 --> 00:02:48,820 to false. 24 00:02:49,430 --> 00:02:51,710 It should only be performing a single task. 25 00:02:53,690 --> 00:02:59,120 So we'll create another method that's solely responsible for retrieving the movie index, a function 26 00:02:59,120 --> 00:03:00,590 name is get movie index. 27 00:03:00,920 --> 00:03:04,310 It returns an integer and receives a string parameter called name. 28 00:03:08,850 --> 00:03:12,630 And inside this function, we're going to copy over the logic from rent a movie. 29 00:03:23,730 --> 00:03:29,550 And if it doesn't find a movie, we still need to return and we'll use negative a thousand to indicate 30 00:03:29,550 --> 00:03:30,780 that it couldn't find anything. 31 00:03:36,270 --> 00:03:39,420 OK, and now we can all get moving index from rent a movie. 32 00:03:58,820 --> 00:04:03,710 And this looks better, I like it when each function is only performing one task. 33 00:04:03,740 --> 00:04:05,600 This is what I mean when I say Modula. 34 00:04:12,280 --> 00:04:16,630 OK, if we rerun the test, it passes good, we don't have any bugs. 35 00:04:19,990 --> 00:04:23,450 But we still have to ask the question, can we refactor? 36 00:04:23,800 --> 00:04:27,060 And the answer is yes, loops are pretty messy. 37 00:04:27,070 --> 00:04:30,330 And since Java eight, we can use something called an end stream. 38 00:04:30,790 --> 00:04:36,400 What an end stream does is it goes through a pipeline of functions until it returns an integer that 39 00:04:36,400 --> 00:04:37,600 represents an index. 40 00:04:38,570 --> 00:04:42,040 First, you need to specify the range from which you're getting the index. 41 00:04:42,080 --> 00:04:45,470 The range is going to start from zero until the size of the array list. 42 00:04:51,100 --> 00:04:52,780 The next operation is filter. 43 00:04:57,100 --> 00:05:03,550 Filter, expecta, lambda expression that returns a boolean, the lambda receives each index from zero 44 00:05:03,550 --> 00:05:05,250 until the size of the array list. 45 00:05:05,590 --> 00:05:06,940 We're going to call it I. 46 00:05:11,040 --> 00:05:16,050 And for each index, we're going to return a boolean that checks if the name of the movie object at 47 00:05:16,050 --> 00:05:17,460 the requested index. 48 00:05:25,420 --> 00:05:26,770 Equals the parameter. 49 00:05:33,870 --> 00:05:38,520 And Stream is going to filter the index for which this condition is true. 50 00:05:40,380 --> 00:05:47,970 And now we need to return this index, so we'll use the terminal operation, find first, find first 51 00:05:47,970 --> 00:05:54,090 is a terminal operation because it ends the pipeline by returning the first index for which this condition 52 00:05:54,090 --> 00:05:54,570 is true. 53 00:05:56,750 --> 00:06:01,550 And if fine first, does it find anything if all of the indexes return false? 54 00:06:02,640 --> 00:06:04,260 Then we'll say or else. 55 00:06:05,460 --> 00:06:06,690 Negative, a thousand. 56 00:06:10,300 --> 00:06:13,090 And we'll return the result that comes out of this pipeline. 57 00:06:16,980 --> 00:06:21,750 OK, as you can tell by now, I'm a pretty big fan of replacing Lupe's with a pipeline of functions 58 00:06:21,750 --> 00:06:22,600 whenever I can. 59 00:06:22,740 --> 00:06:28,020 And as always, as you refactor, run the unit test to make sure there aren't bugs and there aren't 60 00:06:28,020 --> 00:06:29,130 any perfect. 61 00:06:35,080 --> 00:06:40,540 So now we ask ourselves the same question, can it be a refactored and the answer is yes. 62 00:06:41,050 --> 00:06:46,960 If your expression is only a single line, then you can remove the curly brackets as well as the return 63 00:06:46,960 --> 00:06:47,490 keyword. 64 00:06:47,740 --> 00:06:50,650 And Java is going to know you intend on returning the Boolean. 65 00:06:55,520 --> 00:06:56,630 We run the unit test. 66 00:07:00,870 --> 00:07:02,160 And beautiful. 67 00:07:06,250 --> 00:07:08,650 Once again, can the code be refactored? 68 00:07:08,830 --> 00:07:09,940 Nope, we're done.