The solution is going to cover tasks four and five of Part eight.
We're going to start by defining a function named manage items.
It's not going to return anything, nor will it receive any parameters.
I'm going to start by creating a new object of the scanner class that can receive input from the system.
And the next task to ultimately create an infinite loop while true.
And if I go back to the article, it tells me to copy over the print statements.
So I'll need to remove what I have here and call the manage items instead.
Simple enough and then I need to let the user choose between adding removing from cart or checking items
out from a cart.
I'm sure there's a print statement to go along with the step.
And there it is.
Cool.
I'll just copy it over.
And then I need to pick up the response using scan dot next line.
The next step is to compare the response against three possible cases.
So I'll create a switch statement that compares response.
Against Case A.
Case be.
Casey.
If the user wants to add a car, will tell them.
Choose an aisle between one and seven.
Then we'll pick up the using the next ENT.
But remember that in Java we start counting from zero.
So the row is going to be whatever the user inputs minus one.
Okay.
So we're using next end and next line in the same loop and that can be problematic.
We're likely to fall into the next line trap, which can cause a lot of bugs.
So before we fall into the trap, I'm going to add a throwaway next line right after the next end.
And the throwaway next line is going to get wasted by blank space.
So it's not going to do anything.
But it's going to prevent the other next lines that are actually useful from being wasted on blank space.
Okay.
So then we'll just ask the user to pick an item number between one and three.
Once again, we'll pick the value up using scan to an extent.
But we minus one because in code we actually start counting from zero.
And once again, we'll add a throwaway next slide to prevent useful next lines from being wasted on
blank space.
Cool.
So now we have the row and column.
We can use the getter from store to get item to get the item at the requested row and column.
And after getting the item, you can add it to the user shopping carts.
Now back in the workbook, it tells us to print a message that tells the user add their item was successfully
added.
So I guess here, I'll have to put this in a separate variable.
And there you go.
Perfect.
And don't forget to always add the break keyword at the end of each case in a switch statement.
So after the user adds their item, the runtime jumps to here.
Where we're going to print the updated shopping cart.
I'm going to print two new lines, followed by the words shopping cart.
Two more newlines followed by the object.
I'm sure there are more stuff to print.
Here, let's check the workbook.
And there you go.
We'll put a scanner next line to allow the user to pause and or something before they continue.
Later on the code.
I'll buy the crush aisle one item to.
There is my coat.
Cool.
I'll continue.
Then I'll buy some coriander.
My current updates.
Then I'll get a printer.
And this is working really well.
Now, what if I had the same item to the shopping cart like crush?
And sweet.
Our contains method effectively stops the user from doing so.
But it doesn't make sense for it to print item was added to your cart when in fact nothing was added.
So we can stop the runtime using control C for both Mac and Windows.
In fact, our code, the ad method returns a boolean that tells us whether the item was added or not.
So here, I'll say.
So here, I'll check if the item wasn't added to the cart.
That item is already in your shopping cart.
Great.
Let's test that.
We run the code.
I'll add Pepsi.
I'll add Pepsi again.
And it works beautifully.
Now we can work on case B.
In this case, the user wants to remove an item.
So we'll ask them what they'd like to remove.
Copy the print statement from the workbook.
And then pick up the name using scandal and explain.
And remove it from the cart.
I'm going to add Pepsi.
A lot of television.
Then I removed television.
And our shopping cart updates effectively sweep.
An advocacy widget to print the receipt.
And remember that cart checkout returns the receipt.
So we can just call card checkout.
And print the return value.
And that's it.
And now once they check out, that means they're done.
So here we have to close scanner.
And instead of breaking, we're going to return because break is only going to break the switch statement.
Return breaks the entire function, and that includes the loop.
And since the function is void, make sure nothing comes after return.
Just put return.
Cool.
Run the code.
I'll add a few items.
Check out.
And there is my receipt.
Now what if the user enters an option that doesn't exist?
It's a bit awkward that it displays the shopping carts.
So inside default.
If they are some random response, we're just going to restart the while loop is and continue.
I'm just going to enter a bad value.
Beautiful.
Now I can just pick an item.
Continue.
I'll do it again.
And nice.
Where officially done building the shopping cart and this turned out really well.
You created item objects, a cart object, a store object, and you glued them all together inside Main.
And in the end, we were able to achieve this beautiful application.
We're done building the application, but now we get the, quote unquote bullet proof fit.
In other words, we want to make sure the application doesn't fail or crash no matter what.
And we'll do that in part nine.
See you in there.