now just before we get to the stage of updating main to actually start testing
this code the other thing we need to do now is add another method in basket and what we
need to do is when the contents of the shopping basket are actually checked out
we need a mechanism to empty the basket so what we going to do is write that
method here in basket its gonna be....and we are just going to do....
....now we are just calling the clear method of the underlined
map object because that's a lot faster to clear all options or all items out of the map other
than iterating through the map removing objects one by one which would be
another alternative so we're adding it that way which is much faster
let's go back now to main and start at looking at doing some testing so what we need to
do is before we start we need to look at this sell item method that we write initially
because it needs some new functional well it is a small change actually now
that we've got this reserve stock method from the stock list we need
to use that rather than sell stock so let's look at changing that as well so
here on line 87
its got if stock list.sell stock we are gonna change that
and make that reserve stock now the other thing I want to point out is that this does contain the
potential bug which won't caused a problem in this example but is still an example of
bad coding that I want to highlight so basically on line 89 it returns quantity
so it is assuming that the call to add to basket on the previous line actually worked ok
now their is not much point of going to all the trouble of designing methods to return it
for in the results that ignore this results so instead of
actually returning quantity we should return the result of calling the baskets
add to basket method so we can change that so instead of quantity here we are gonna
put......
.....
now the remove item method it is very similar as you know to the sell item but we need
to do a similar thing to that and obviously if your looking in this class we
haven't got anything for that so let's add add to that so I'm just copy this entire
method first because it's very similar but there are a couple of changes we need to make so paste that
in and lets call it remove item.......so the first part is where it is actually getting the items ok....
......
.....
......
and if that is the case then what we're going to do is actually return unreserved
stocks so.....
.....
....whereas in that case we are only passing we are only using the string though so we are just
going to return the string there so that is valid so basically
we're checking that the remove basket returns the quantity that was specified
only if it does meaning that we are able to successfully do that do we then try and
unreserve the stock by calling stock list.unreserve stock with the stock
name that is the string item and the quantity and obviously I made a mistake here as well because
we're returning it what we should be doing there is in fact deleting that
line because remember I was returning quantity before we just don't call it
was we only want to call basket.add basket once we are actually selling the
item so that's why I'm returning now the result instead of just returning the
quantity which of course would have potentially been a bug if
whatever reason we didn't get the right value back so we are now returning that
correctly in the cell item method so next we need a check out method so the
check out method is used when the customer finally decides to purchase their items and it
loops through all the items in the basket using the unmodifiable map
thats returned by the items method and for each item them calls the sell
stock method of the stock list class and once they are all being sold it clears the basket
so let's go ahead and write that so it's going to be....
......
.....
.......so go through and sell all the stock in the basket and
then we finished....... to clear out the basket
effectively because the purchase has now been completed and the stock has been
deducted we sold the stock in other words so we can clear the basket now
strictly speaking it should really be checking the return value of sell stock
this is a line 108 I'm talking about but here as the quantities are coming from
the baskets items and we checked before being added to the basket and this
approach is safe now once I have covered exceptions later in the course I would write this
method to throw an exception in the unlikely event that sell stock return 0
ok so we are now in a position were pretty much ready to give this program a run so one other thing I need to
change is we need to go back into the stock item.compared to method and I'll tell
you why when we get in there and in the compared to we got this print out entering stock
item. compared to let's comment that out just so it doesn't ease up all our space
on the screen because it will clutter our display when we actually gonna run this program
so we're going to comment that out and if I know run this you can still see
that it seems to be working ok contains 1 items reserved we don't sell spanners
don't sell a spanner which you can see there shopping
basket contains 1 item the items are going to our stock list seems to be ok so the basic programs
working and we now need to start looking at the main method we need to
start rejigging this little bit more so we've got a basic bit of code here that
what we do in the next video is do a little bit more tests will actually cleaned up
a little bit and also look at removing some of the items we've added to the basket etc
remove some of the stock listings etcetera and clean it up a little bit and
give it a final run so we should be able to finish the challenge and get this all working
completely in the next video