1 00:00:00,330 --> 00:00:04,980 The solution is going to cover part three, this test case tells us to check if an item is removed after 2 00:00:04,980 --> 00:00:05,730 being sold. 3 00:00:07,720 --> 00:00:10,420 So I'm going to write a unit test named Removed Item Test. 4 00:00:26,660 --> 00:00:29,060 And the item we're going to remove from the cart is. 5 00:00:30,390 --> 00:00:31,140 Krush. 6 00:00:33,400 --> 00:00:37,480 So ideally, calling remove should remove this object from the area list. 7 00:00:38,620 --> 00:00:41,140 And now we're going to make an assertion, assert false. 8 00:00:43,650 --> 00:00:48,720 And this test is going to pass if Calthorpe contains the crash, object returns false. 9 00:01:04,260 --> 00:01:07,650 And now inside Kageyama, we're going to write code to make the test fail. 10 00:01:10,230 --> 00:01:11,670 Public void remove. 11 00:01:17,440 --> 00:01:22,220 And the unit test implies we pass a string into our method and for now we'll do nothing. 12 00:01:22,270 --> 00:01:23,530 I want my test to fail. 13 00:01:35,190 --> 00:01:35,610 Good. 14 00:01:35,670 --> 00:01:37,710 Now we write code to make the test pass. 15 00:01:42,040 --> 00:01:46,600 The simplest code I can think of is to just create a loop that runs through the size of the array list. 16 00:01:55,750 --> 00:01:59,710 And if the item object at the index matches the name being Pastan. 17 00:02:14,580 --> 00:02:16,440 Will remove the item and index I. 18 00:02:25,280 --> 00:02:26,330 Let's run the test. 19 00:02:32,440 --> 00:02:33,430 And it passes. 20 00:02:38,030 --> 00:02:42,900 OK, here comes the more important question, can this be a refactored, can it be made simpler? 21 00:02:43,340 --> 00:02:47,030 And the answer to that is absolutely loop's are messy. 22 00:02:47,390 --> 00:02:49,700 Array lists have a remove if method. 23 00:02:51,540 --> 00:02:53,640 This items that remove if. 24 00:02:55,600 --> 00:03:01,330 The remove if method removes an item that matches the predicate that we write a predicate is just the 25 00:03:01,330 --> 00:03:03,130 function that returns true or false. 26 00:03:03,160 --> 00:03:06,770 So that means our lambda expression is going to receive an element as a parameter. 27 00:03:07,170 --> 00:03:09,370 Remember, it has an arrow key that points to code. 28 00:03:13,120 --> 00:03:17,500 And here we need to return a boolean and we're going to return a boolean that checks if the string of 29 00:03:17,500 --> 00:03:20,350 the element we're running through is equal to the name parameter. 30 00:03:22,330 --> 00:03:25,400 And the element that matches this predicate is going to be removed. 31 00:03:25,900 --> 00:03:29,650 And as you can see, using a functional approach is more elegant than using loops. 32 00:03:29,860 --> 00:03:33,960 So whenever it's possible, try to call a function that does the work for you. 33 00:03:35,550 --> 00:03:39,930 And as we refactor, we need to always run the unit test to make sure there aren't bugs. 34 00:03:43,190 --> 00:03:44,240 Hend Perfect's. 35 00:03:56,340 --> 00:04:01,370 Here, it removes the item and our assertion passes because the cart doesn't contain it anymore. 36 00:04:02,770 --> 00:04:07,840 Now, once again, can we refactor, that's a question you have to keep asking yourself until you're 37 00:04:07,840 --> 00:04:13,270 absolutely certain that your code is as simple as it's going to get in this case, if the code is only 38 00:04:13,270 --> 00:04:17,470 one line, then you can remove the curly brackets as well as the return keyword. 39 00:04:17,769 --> 00:04:22,460 Java's going to know you intend on returning a boolean because it's expecting a predicate. 40 00:04:22,720 --> 00:04:23,680 So that's pretty cool. 41 00:04:26,100 --> 00:04:27,420 I'll rerun the unit test. 42 00:04:30,150 --> 00:04:37,680 And Perfect's, the test passes, so I know that my code is working as intended now, once again, can 43 00:04:37,680 --> 00:04:38,610 we refactor? 44 00:04:38,890 --> 00:04:39,440 Nope. 45 00:04:39,630 --> 00:04:42,270 So far we're good and we can move on to the next test.