The solution is going to cover part four.
We're going to come back to this test case, but the next one tells us to make sure we calculate the
correct subtotal.
So I'm going to write a unit test named subtotal as valid.
Each item in the cart is one ninety nine.
So our unit test is going to assert the price we're expecting 398.
Is equal to the price that gets returned by cart, get subtotal.
And now inside Córdova write code to make the test fail.
I'll define a function that returns a double named get subtotal.
And for now, we'll return a random number.
Our test fails, that's fine, let's write code to make it pass.
Inside, get subtotal, I'm going to add a temporary double subtotal variable.
And then create a loop that runs through the size of the array list.
And during each run, I'm going to add the price of each item to the subtotal.
And then return the subtotal.
If we run our test now, it works, the assertion passes.
But can get subtotal be any simpler, yes, loops are messy, you should always favor running the Israelis
through a pipeline of functions.
You learned about the intermediate operation map map updates every element in the sequence.
But map to double, expect you to update every element in the sequence with a double value just like
map and expect to lambda expression, the Lambda receives each element in the sequence as a parameter.
And for each element, we're going to return the price.
So at this point in the pipeline, our stream is full of double values.
More specifically, we have a double stream and every double stream is capable of calling the terminal
operations some.
Some is a terminal operation because it ends the pipeline and it returns the final results.
In this case, it's going to return the sum of every price in the double stream.
And to me, this looks a lot better than before.
And as always, as you refactor run, the unit has to make sure there aren't any bugs and there aren't
any.
Now we ask ourselves the same question, can the code be 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 Jeff is going to know you intend on returning each double because not to double expects it.
Again, working with lambda expressions really simplifies your code.
We on the unit test and beautiful once again, can the code be refactored?
Nope, we're done.