so what we need to do in this video is we need to go back to our main class
main.java we need to just adjust the main method a little bit to clean up some
things and then we need to go through and obviously test this challenge because we
typed a lot of code in we now want to run it and make sure that it all works as it
should be working so we finished off in the last video just starting to talk
about that so I am at main.java now and scroll down and start
we need to start looking at code starting from line 47 I will put that on the screen
there and lets just adjust this a little bit so that we can actually go through and
fix this and have the right output and add a new basket etc and just sort
of test everything else out so make a space their ok that first one can stay in
there then we got through a second car and we print out again so that is ok and we still doing
a test to see whether there's no more cars in stock that's ok the spanner
we're testing a product that doesn't exist so that should work but
there isn't any point putting a basket out in that case because obviously the inventory
count isn't adjusted because the product doesn't exist so comment it out we don't
need that the 3 lines there the juice the cup and the bread they can stay in their and I might
just comment that out that basket as
well there are also comment out that stock list which we don't probably need
right their so let's add a new basket here now so put.....
it is our second basket we really had Tim's basket defined and will add some items their so....
.....
....lets also then removes some items to
test that is working so.......
.....
...and with the code line 72 let's actually change that lets try
and remove something from the basket as well so....
......so basically we are going to be
testing to see that the quantities are actually working adding an item and
then removing it to see if that that works ok so we remove those three items and
print out down here will do something like....
......
.....and that shouldn't actually work os it should not remove any at that point alright let's
print out tim's basket again.......ok let's actually removed the items now from
tim's baskets so do....
...we will remove everything so it should end up being having nothing in it...so we would do that
for all of so I'm gonna copy and paste
....and will do the same for cup juice and also bread which I've already done so
the second bread we removed one cup lets removed 3 this should take it if my calculations are correct
to 0 cup juice to 4 and cup again will do a second one.... take that three of that that
should be zero so again what we doing here is.....
....and let's print out the basket to confirm its empty which it should be if I've done to my
calculations correct and lets display the stock list before and after checkout
so......
....okay then we'll print out the basket because the
basket is the one we are using for our checkout then will also print out the stocklist will also then
check out cause you remember we write the check out function which takes it out of
shopping basket and takes it and sorts of updates the quantity so.....
.....
....then we'll print out the basket which should be empty again and then we print our
stock lists that should hopefully be correct ok that should all we need to do I think and
lets clean up some of this code down the bottom as well so what we got down here we
can probably leave it in there this for loop we can probably take that out becaus that's
just going to muddy the waters a little bit we don't really need that anymore so lets just
take that our comment that out and lastly down here in the bottom lets do a checkout
for Tim's basket which again was empty so nothing should happen and will print
out tim's basket
ok so let's first is run this to see whether it works and we'll take it from
there so we will run this ok so lets look at the output ok so we got a stock list that we will ignore
we'll have a look at the shopping basket items Tim contains one item that
our first shopping list I'm gonna move that over a little bit so we can see the code
then we can roughly sort of follow along with the code so we created a new
basket tim's basket and added a car to it so reserved one the purchased one then we added
another car so reserved 2 purchase 2 that's correct so far the spanner
we got another option here there are no more cars stocks so that's working this one here
we don't sell spenders so that's good we didn't do another print of the basket their and
that's correct
ok then we added juice cup and bread juice cup and bread which is correct we haven't
gotten an output their so that's the output in case your wondering why that is a 111 we didn't actually
printed out that point so moving on down
we have now sold another hundred cups then we removed a cup so that's a hundred take
one is 99 + the 12 allocated from the other basket so that's a hundred and eleven
so why we got a 111 reserved
overall that's reserved for all baskets but a 101 purchase that's actually
interesting that on their 101 purchased i think is wrong because what that should have
been was a hundred actually what that should have been is 99 because there was a hundred this is one
I'm looking on line 70 here sell item basket cup a hundred that
allocating a hundred
and we removed an item which should have taken it down to 99 so it is a 101 so that seems to be
a bug their so let's just check that out that's probably or its remove item I'm guessing that could be
that's where I would look first to see if there's a problem on that's and that been basket
lets have a look at that add to basket
and remove from basket
and yes that's the problem on line 32 if your recall I actually copied pasted add
to basket to make remove from basket at least I think I did and what I've done I've added the
quantity so obviously if removing an item form a basket we should be taking
the quantity what's in there and deducting the quantity because we're
actually removing items so that should have been minus so that's a bug that I introduced
when I was typing that in so I would think that would fix it so let's just run again to get
were up to before then continue ok their we got you can see now
if you look at the output so we down to basically this part here now the cup so where
we had a hundred and one before we have now correctly got 99 purchase so it is
deducting their now so that is good ok cars removed we got that indication their cars
removed remove item tim's basket shouldn't work shouldn't remove any and the
reason why the third one didn't work was of course we've removed an item on
line 75 that deducted or reduced the quantity down to 1 the on line 77
now we remove it to 0 so just doing another testing on line 78 which shouldn't work because we
haven't got any any left in the basket to remove and that's why we correctly got that
message up here to say cars removed 0 so that's also working then we printed
out Tim's basket again here below that so we've got three items bread now a
reserve 1 and 1 purchase reserved a 102 cups remembering that's the total
count of cups for all customers and three of them have actually been purchased
and I'll look again to make sure that's correct so the Shopping Basket Tim we've allocated bread again just going back up
to the top here we have bread 1 cups 12 so we're starting off with 12
cups we have added a hundred here so that's a hundred and twelve so looking
at cups for customers just to make sure that the cups count is correct their should
be 99 purchased for shopping for customer cups or when I say purchased ones
that are actually in the item ok so here we are now we are at the
stage now where we wanted to confirm that our counts are right so bread
reserved 1 purchase 1 for Tim so we saw above that that that was correct so you can see their
Tim's basket we added one to it tim's basket also had 12 cups and then we actually down
here we actually removed 9 cups so that should leave 3 you can see
cups 3 have been purchased
reserved should still be hundred and two and the calculations for that for the cups is basically
if you go through and type this up manually we got 12 that was started with
another hundred that have been allocated so that's a hundred and twelve removed an items that is a 111 and we
removed 9 which would leave a 102 so we got amongst the two shopping baskets a
102 and the other ones look to be ok as well and shopping basket Tim
where we removed all the items from tim's basket this is right down here and we removed
the rest of the items because we had three left of the cup and the other items there and
the second remove I've added in well that shouldn't work obviously because
the 3 took the 0 anyway and we print out tim's basket shopping basket tim contains 0 items so
there's nothing left
basically we've removed all the items and looking at the final display in the stock list
we got a Shopping Basket customer contains two items have now got the
99 that they have allocated for cup and juice they did 5 so go back and
confirm that was 5 juice was 5 as you can see there and obviously this transaction
deducted from cups so we really have 2 items in that basket
that's correct looking down at the stock lists now with our final print out
we got this before hand we got cup we got a 108 stock and we know we are about to reduce that
by 99
ok so looking at this we just scroll down again so basically we've got to this last part now
this displaying stock list and that should be correct because if you recall with I am just looking at
the cup 99 reserved and 108 in stock which is the available count because we started
off with cup up here at the top with 217 in stock so
consequently what we looking for now in this final checkout is....
that one so we've got basically cup 99 reserved a 108 in
stock and the check out basically removed them from reserved and the in stock
doesn't change that is still a 108 because that is how many that are actually
available for sale from other customers effectively so that's correct at this
point we are good to go we've actually done pretty well
full test here and juice had the same scenario where 5 is reserved and 31 in stock is now reserved
to 31 in stock so again we deducted it from the inventory at the time it was reserved
so we don't need to deducted it again at that point so at this point the solution
seems to work part fine so items can be added too or removed from the shopping baskets and the
stock levels correctly reflect this activity now checking out a basket
updates the stock level by removing it from the Reserve count as we saw and any
attempt to add or remove qualities that are not available are actually handled correctly as you
saw in this little run through so that components working fine what we haven't
done we haven't really tested some of the boundary conditions such as how the
program behaves before any items are added to the stock list now the main
functionality of the program works fine if we did that but we'll actually get a
error down here on lines 98 to 99 where we attempt to
adjust the stock cars but where we actually haven't got any stock of it that's because
we've failed to test the return value of the GET method to ensure that we do have
a valid item object before calling its adjust stock so obviously you can see its a stock list.items
.getcar.AdjustStock so
if that returns null that's obviously going to cause an error so we can actually show
that or simulate that error by just briefly commenting out all these additions
so that we haven't got any stock you will find that we actually will get an error
so I'm going to comment that out and run it again we notice we do get the error on line 98 and
and again line 98 is because we tried to adjust this without
determining whether it's null or not so what we should really do there is you would
actually do a proper tests so we would put something like....
.....
...so that should prevent any errors
....you can see now we don't get any error because obviously we don't get any processing because we've taken all these
things out so they haven't got stock for those items but the main point here is that we're
now testing boundary conditions and preventing errors if we haven't got the stock
so that's a good thing to do in your programs to check things like this
obviously we were running it with inventory but without actually having any inventory in their to make
sure for you to be doing some tests to make sure that that is still going to work so I
just put those back in again now so to confirm it is gonna work which is
their is no reason it won't but we will do that anyway so make sure our code is still working we got a correct
results back again now so closing that down that is good but I just wanna go back to stock item and their is
2 warnings here firstly stock item you can see the constructor that takes the
double stock item name isn't being used but we did discuss how this
could be useful so for that reason we would leave it in there for sort of future
purposes so it could be useful in that scenario to call that we didn't want to
actually passed the initial quantity in stock if there is no quantity in stock and also the
other warning is that set price doubles on line 36 isn't used so changing the
price items is a very crude operation that as our example stands it is not something we
need it so we should probably consider removing so as a general rule
with getters and setters the advice would be don't add them unless your going to
use them so that's probably something you would probably want to remove in your
program and if the situation came up if your wanting to set the price other than the
constructor then you would add it back again and just before I finish off stock list
you can see it's got one warning here it is saying price list method isn't used
we did use it if you remember when we commented out the call to remove clutter from
the outputs so we only included it to demonstrate running or returning rather a list of
items in a way that wouldn't compromise the stock list class though so again that one should
probably be removed
and basket we haven't got any warnings there
and that's actually it so our challenge is completed and that is the end of the challenge and also the
end of this section on the Java Collections Framework I'll see you in the
next section cheers for now