There are two more classes we need to write up in this video.
We'll do the shopping cart.
The shopping cart is identified by the items that it carries and the number of items can increase or
decrease in the shopping cart.
So right there, we know we need to make an array list that can store item objects.
And as always, if a class has failed, you need to apply the big three steps, you need to take instructor,
a getter and a setter.
The instructor is public shares the same name as a class.
And it receives no parameters because in the beginning, your card is going to be empty, we're just
going to set the field equal to a new era list that can store item objects.
OK, now we want to get her there, receives a one parameter.
The index of the item object they're trying to access.
And this letter is going to return a new copy of the item object at the request that inducts.
Now, we went out a centre that receives two parameters.
The index of the element you want to update.
And an item object that we're going to update the element with.
Then we're going to get the element at the requested index.
And set it equal to a new copy of the item object being Pastan.
And that's all for the big three steps, if you feel like it's getting repetitive, then that's good.
It means you're getting the hang of it.
Now we're going to add actions.
One thing we can do is add an item to the card.
So all credit boolean method named ADD it receives one parameter and item object.
And the duck comment tells us we can add an item only if it wasn't already added, I'm going to ignore
this condition for now.
I'll come back to it at the end.
For now, I'm just going to add the item to the items list.
And return true.
The next action that it can perform is to remove an item from a cart.
This method is void called remove, and it receives one parameter, the name of the item you want to
remove.
So what we'll do is create a far loop that runs through the size of the array list.
And inside the loop, we're going to get the item object at every index I.
We'll get the name field of that item and we'll check if it equals the name of the item, though, as
Pastan.
And if that's the case, then we're going to remove that item from the array list.
The last method is check out this one return to string that resembles a receipt.
And the receipt needs to include three things, the subtotal, which means the price before tax, the
tax they have to pay and the total.
And instead of creating three separate variables, I'd rather create an array with ALENKA three.
And so the idea is that the first element is going to be the subtotal, the second element, the tax
and the third element is going to be the total.
So during each run, I'm going to update the first element, the subtotal and add to the price of the
item inside the cart.
So that should give us the price of every item before tax, the tax rate is 13 percent.
So we'll set the second element equal to the first one.
Times point one three.
And now the total is equal to the subtotal plus the tax, so we'll set the third element equal to the
subtotal plus tax.
All right, now we have the total subtotal end tax so we can return a receipt, just copy over the format
from the article.
And replace the placeholder is where we need to.
Kate, now our final task before we can test our code is add it to string method that returns a nicely
formatted string of every item inside the cart.
As always, public string to string.
I'm going to create a string named temp for temporary.
It's going to be an empty string for now and we'll just return it's.
First, don't create a loop that runs through the size of the array list.
And during each run, I'm going to get the two string of the item object at the index eye and I'm going
to add the string to the temp variable that we're returning.
And after adding the two string of an item, I'm only going to add one line to separate it from the
next two string that gets added.
OK, now we're going to test our code before we do anything else.
I'll have to create a shopping cart object inside Main.
I'm going to add four items to the shopping cart, so I'll go back to the article, I'll copy them over.
And print the cards.
And by printing the cart, it prints a nicely formatted string of every item inside the cart, but it
seems we added the salary twice for our purposes.
We only want to allow the user to put one of each item.
We can't have any duplicates.
Well, thankfully, every aerialist has a contained method, the method receives an object, it returns
true if one of the elements inside the array list equals the object they were passing in.
So what I'm going to do is check if the items array list already contains the item being Pastan.
Then I'm going to return false.
Otherwise, the item will get added and will return true.
And now you might think, OK, we're good contains is going to behave exactly like we want it to.
It's going to check if the array list already contains an object before adding it.
Yeah, let's run the code and see what happens.
First, it adds the celery, OK, that's fine.
But then we get the exact same Sellery object, its fields are identical to the one already in the array
list, the array list and our eyes already contains that object.
But it gets added anyway, so why isn't this condition evaluating to true, why doesn't it recognize
that the array list already contains a salary object?
The short answer is we need to help the contains method achieve its goal.
So before we move on to part four, you need to learn about the equals method.