in the previous video we finished the changes to the stock item. class and
obviously got that on the screen now but in the stock list class we also need
methods to reserve and unreserved stock and if we open up stock list we already got
some code on starting on line 32 there's a method their for selling stock but that would
need to be changed to use a new stock item.finalized stock method rather
than performing a stock adjustment now before we make those changes lets have a look
at the add stock method which you can also see on the screen starting on line 17 now at
the moment if you look on line 23 the add stock method adjusts the stock level
of the new stock item that's being added and it does that while making sure that any
existing stock levels are also included and how it does that is by calling
the quantity in stock method now had we modify this method rather than creating
the new available quantity method then it now be a bug in add stock and it could be
a very difficult bug that would take a lot of research figuring out to
find out and that's because anyone reading the code would naturally expect
the quantity in stock method to return the value of the corresponding field in
stock item now when we get to the end of this solution you probably noticed that
I only actually use the available quantity method inside stock item so it's
reasonable to ask why I didn't make it private or just performed a subtraction in
the stock item.reserve stock method and do away with available quantity the
method altogether now that would have removed the potential for bug that
I have just discussed but the thing to remember is that in a real world
application it would be necessary to show the customers how many of each item
are available before they try adding them to the basket so although we don't use it
outside of the stock item class in this example it would be unnecessary method
in practice and I thought it'd be worth pointing out the
potential for error that you can actually produce so lets go ahead and add some methods
the first is gonna be reserved stocks so I'm going to come down and add that
I'm just gonna add it here below actually I'll add it below sales stocks so....
the reserve stock do that one first so....
.....
....and just a recap both the reserves stock and unreserve stocks attempt to retrieved
the stock item from the list and they then call the items reserve stock or unreserved
stock as appropriate so let's go ahead and write that finish the code for this so.....
.....
.....so that's
the reserve stock and let's also code the unreserve stock so....
....
lets just copy this code to save a bit of time copying all the code
that's come out of the reserve stock method and the code is largely the same all
the retrieval of the stock item codes is the same testing for null and
testing the see if the quantities is greater than 0 is still the same
the only thing we need to do is reserved in stock
.unreserve stock instead of reserve stock so in here will change this to unreserve stock ok so that's
reserves stock and unreserve stock now as you can see above on line 32
the stock list class already has a cell stock method but now instead of
adjusting the item stock level directly it just calls the
finalize stock method or basically that's the change we're about to make their we are changing from
existing functionally as you can see at the moment what it's doing its using this
adjust stock but I've talked about that before we are changing the code for that
so let's go ahead and adjust the code for this and what I'm going to do just to say
you've got a record of it I'm going to comment out all the existing code for
sell stock and what I'm going to do it then is just type it in again so.....
.....
....
....and if you get
down to here it means there was a problem we weren't able to sell the stock we return 0 I've left
obviously the code in their as it is so again confirmation that instead of
adjusting the item stock level directly we just calling the finalize
stock method with a positive quantity rather than negative when it
was adjusting the stock and you can see that on line 42 that's commented out
there that was the negative quantity that was being called previously but
now that we are calling finalize stock with the positive quantity we don't need to do that
anymore all checks for sufficient stock levels are performed in finalize stock
so that condition can be simplified as well
really sell stock only needs to check the item exists in the list or in its list
and that the quantity being sold is greater than 0 and it was also abusing
that get or default
option as well so we remove that and just made a purely a list.get so we're
guaranteed to retrieve an item if one exists for that item name instead of getting null back
and the finalize method returns to quantity sold or 0 if their weren't enough so instead of
returning quantity the sell stock method can now return the result of finalizing the
sale and obviously I have done that on line 36 so we are nearly done at this point but
what we do need to do now is provide the basket case not basket case
basket class with a way to remove items from the basket now this could be
because the customers chose there mind completely and no longer wants the item at
all or alternatively could be because they want to reduce the number they
originally added either way we've got to provide that function so lets go ahaed and have a look at basket.java so we are gonna add
a remove from basket option remembering we got an add we haven't got a remove so lets go ahead and type that in
and described it as we go through so.....
.....
.....so we need to do a test first to make sure that its a valid item so.....
....
.....
....and it is appropriate to use get or default here
because if it is already in the basket we want to return the current quantity for this
item
otherwise we want to return 0 so bottom line is after line 32 is executed we got the current
quantity that has already been allocated by the customer or added to the
basket I should say or 0 and we need to figure out the new quantity and that's a case of
adding in basket plus the quantity that was passed to this method then we need to do a
check to make sure that we have actually got a change here so...
....
.....
....
.....
.....so basically the quantity
has now been reduced to zero it means basically we want to remove it from the
basket because obviously an item should only be in the basket if their is a quantity of
1 or more and that's why we're doing a test for 0 here exactly and less than
zero would mean there's a problem so we don't want to include that we wanted that
to fall through and to return an error in this case to return 0 to indicate
that it haven't done anything because it was an invalid quantity
down here outside of the if we will just put a return 0 which is our fall through code if there was a problem so that
should be pretty straightforward how we calculated the new quantity we've
updated the item in the basket if their was still like at least 1 or more items after doing
our calculations to determine the quantity and if the quantity was 0 then
we're going to remove it from the basket all together and even in that case we are going to still return quantity which
was a quantity of that was originally sent through to this method and if that all fails
through and either the quantity basically was less than zero we return 0
or to be more precise if new quantity ends up being less than 0 alright so we basically
getting to the stage now where we are going to be ready to test the new functionality so
what I'll do is I'm going to stop the video now and in the next video will go back to our
main method our main challenge method and will start putting some code in their to check out
or to test rather these changes that we made in this video and the previous one so
see you as always in the next video