The solution is going to cover part three, this test case tells us to check if an item is removed after
being sold.
So I'm going to write a unit test named Removed Item Test.
And the item we're going to remove from the cart is.
Krush.
So ideally, calling remove should remove this object from the area list.
And now we're going to make an assertion, assert false.
And this test is going to pass if Calthorpe contains the crash, object returns false.
And now inside Kageyama, we're going to write code to make the test fail.
Public void remove.
And the unit test implies we pass a string into our method and for now we'll do nothing.
I want my test to fail.
Good.
Now we write code to make the test pass.
The simplest code I can think of is to just create a loop that runs through the size of the array list.
And if the item object at the index matches the name being Pastan.
Will remove the item and index I.
Let's run the test.
And it passes.
OK, here comes the more important question, can this be a refactored, can it be made simpler?
And the answer to that is absolutely loop's are messy.
Array lists have a remove if method.
This items that remove if.
The remove if method removes an item that matches the predicate that we write a predicate is just the
function that returns true or false.
So that means our lambda expression is going to receive an element as a parameter.
Remember, it has an arrow key that points to code.
And here we need to return a boolean and we're going to return a boolean that checks if the string of
the element we're running through is equal to the name parameter.
And the element that matches this predicate is going to be removed.
And as you can see, using a functional approach is more elegant than using loops.
So whenever it's possible, try to call a function that does the work for you.
And as we refactor, we need to always run the unit test to make sure there aren't bugs.
Hend Perfect's.
Here, it removes the item and our assertion passes because the cart doesn't contain it anymore.
Now, once again, can we refactor, that's a question you have to keep asking yourself until you're
absolutely certain that your code is as simple as it's going to get in this case, if the code is only
one line, then you can remove the curly brackets as well as the return keyword.
Java's going to know you intend on returning a boolean because it's expecting a predicate.
So that's pretty cool.
I'll rerun the unit test.
And Perfect's, the test passes, so I know that my code is working as intended now, once again, can
we refactor?
Nope.
So far we're good and we can move on to the next test.