so in this video we are gonna go through now with a challenge so what I've got loaded is
the project as we finished in the last video the sorted collections so we are
going to be making use of the project that we've created with a basket the
stock items and stock list etc so what I'm gonna do first is create a new class and
put the code in there so I'm gonna call I'm gonna right click here new Java class i'm gonna call it main challenged because main is already use and I'm gonna paste in
what the challenge is ok so here is the challenge challenge is to modify the program so
that adding items to the shopping basket doesn't actually reduce the stock
count but instead reserves the requested number of items so what you'll need is to
add a reserved
field to the stock item class to store the number of items reserved
now items can continue to be added to the basket but it should not be possible to
reserve more than the available stock of any item
now an item's available stock is to start count less than the reserved amount the stock count
for each item is reduced when a basket is checked out at which point
all reserved items in the basket have their actual stock count reduced so works much
like a real shopping cart would on a website and also once checkout is
complete the contents of the basket are cleared because obviously the transactions
completed at that point in time you should also possible to unreserved
items that were added to the basket by mistake the program should prevent any
attempt unreserve more items than were reserved for basket now add code to main that will
unreserve items now main here can be main challenge because we already got code in
main so you can put it in there or this one so add that code and that's gonna unreserve items after they have been
added to the basket and in addition it will also end up and un-reserving items that
have not been added to make sure that the code can cope with invalid
requests like that and finally after checking out the baskets display the full
stock list and also the contents of each basket that you created
that's it that's the challenge hopefully that make sense it's now time to go away and have a
go with that challenge and see what you can come up with when you ready to come back
and see me go through my version
of the challenge start the video again but for now pause that video and I'll see you soon
ok so hopefully you manage to get the challenge done and gave it a good go so lets go through now
and what I'll do is go through now and show you what my solution would be the first thing
we want to do is go to the stock item class to the top and we want to add a new field add one that's got quantity stock
want to change or add to it another field....
....and the quantity in stock we need to change that method now because
if you recall the challenge we need a reserved field which we've done item
continue to be added to the basket but it shouldn't be possible to reserve more
than the current amount which is correct so the stock count for each item is reduced when
a basket is checked out but we know that but the point here is items can
continue to be added to the basket but shouldn't be possible to reserve more
than the available stock so therefore we need to track every time that's reserved
and the way we're going to do that going back to stock item is adjust the quantity in
stock
whereas in the past that was just returning quantity in stock we are gonna make a small
change.....
...because the actual quantity that's available for sale
effectively is the quantity that we got on hand less anything that is being reserved now at the moment the only way to
manipulate stock levels is to use the adjust stock method now you probably
recall that allows positive and negative quantities to be used the only checking
it does is to make sure that stock levels will not be negative after the
adjustment now to allow stocks to be reserved my solutions going to add a new
method called reserve stock and also an unreserved stock that is going to change the quantity reserved so let's go
ahead and do that so we are not gonna touch adjust stock now so let's start with reserves stock
....
.....
...so we are gonna add to the reserve count because obviously it's
enough quantity in stock
available for us to do that and then we are going to return the quantity in that case
otherwise if the execution of the code gets down to here we know that there
was a problem we didn't have enough quantity and we are going to return 0 so that's how you go about
reserving the stock in terms of on un-reserving it because that was also a requirement
...its is going to be slight different we need to quantities as well to know
how many times it was reserved we put.....so we are looking at the reserved
variable this time so that's the case we are going to put...
...and if execution comes to down to here then we know of course
we didn't have enough reserved to unreserved
we are gonna return 0 so that handle that quite nicely and again reserved in stock check that their is enough items are
available before increasing the reserved amount and the check could have been
written as if quantities is less than equal quantity in
brackets take reserved but we already created a method called quantity in stock
and the one I just made a small change to earlier this one here in line 32 so it made sense
to actually perform the subtraction using that method instead and un-reserved stock as I
mentioned reduces the number of items that are reserved so obviously
checks the quantity being reserved
is not greater that the quantity that are currently reserved in both cases the methods
either return the quantity that were reserved or unreserved if it's
successful it is or 0 if not if it is unsuccessful performing the operation and the reason we are doing is that it allows
the calling code to check that the reservation request was successful and
obviously that's important because we shouldn't update the customer's shopping
basket
if all the requested items couldn't be reserved now before I move on we should really
have a look at the quantity in stock field and the quantity in stock method
in Java this league will have a field and a method with the same name so this
code actually works fine
and what I'm referring to is here we've got quantity in stock on line 9 we also got a method
quantity in stock on line 32 thing to remember there is that wouldn't be legal
code in some other languages such as C++ but the Java compiler can work out which
one has been referenced and can cope quite happily with this that's probably not
a good idea to do this though users of the stock item class you know will not be
confused because if it was private so any code using a stock item will only see
the method because of course the field itself is the access modifiers
private but it's a very good way to confuse ourselve when we come back to working
on the code that is in our stock item class so what we could probably do for starters is comeback
to our reserved method do something like this just two remind us this one here.....
...so that would obviously confirm so that we don't get confuse as to whether we are accessing a
variable or method because obviously for access the variable quantity in stock
we get a different result because the quantity in stock method is performing
that additional calculation taking reserved into amount with that said even with that
comment there's a possibility for errors pretty great be quite easy to make that
mistake in a particular if you are in a hurry for argument sake and what I'm talking about is we
could if we actually change that and lets just say we forgot to use a method and we just change it here to use the
variable
quantity in stock lets just assume I have renamed this to be
exactly the same things so you can see in this case an actual fact lets just change that
because that's really what it should have been anyway so refactor that....
....that should always been quantity in stock anyway....ok so lets assume
if that was a case along with the field on line 9 was quantity in stock and on line 32
we had quantity in stock method so the point here that was that we could use the same
variable name and looking at the code it is obviously
going to compile its accepted by Java but what happens then it's going to
allow more stock to be reserved than it is available because of course not looking
at the reserved
field when taking into account the calculation in other words it's not
processing on line 33 anymore so that's obviously an gonna lead to dissatisfied
customers so the point is that using the same name was acceptable when the new
method was just returning the value of the field it was named after but now
that's actually doing something different it's actually performing an extra step
it's taking in reserved the value of the reserved field into account we should
really give it a different name so lets go back and change quantity in stock refactor
that and rename it to what it really is which is now available quantities
so we call it....and that is much clearer then and we come back here
we know that we're going to be using the available quantity method and as a result I'm able to get rid of that comment
so reserve stock method now now that its using methods a lot clearer and we're not going to run
into any difficulties accidentally using a field in instead of a method now
the last function change for the stock item class is to provide the method for
finalizing the stock level where items are actually sold so in other words when
the customer checks out the shopping basket because of course prior to that
they were only reserving the items now this is just a case of reducing the quantity
in stock by the amount sold and also reducing the reserve quantity because of
course the goods are no longer reserved they are being sold so what we're going to do is
create a new method called finalized stock down here
.......
.....
....and note now that there is no ambiguity here now
we are using the field quantity in stock.....so we are deducting the quantity to be
finalists and of course you also have to change re“served drop that by the same amount so.....
...so we are reducing the quantity in stock and reducing the
quantity reserved and I'm gonna return quantity to indicate to the calling process that
we've succeeded in how many items
that had their quantity that's has been specified has actually been reduced in
stock
so now we've done that we then return quantity and of course quantity was the number of stock items that
were reduced and also the reserve cap was reduced as well again if code execution gets down to here we
know there's a problem so we return 0
so we know the operation didn't work so again the finalize stock method check if there's
enough items are reserved to satisfy the request and of course it then reduces
as I mentioned both the stock level fields by the required amount as well as the
reserved field and again returning quantity if successful the original quantity that was
passed to the method was 0 if its not ok the last step isn't necessary but because we've added
another field to a class lets change our toString method to take that into
account pretty minor change but at the moment our toString method for stock item has only really listed
the name and the prices lets change this......
....
so we got that now in the output alright I'm gonna finish the video here now in the next video we'll come back and continue
and make some code changes to the stock list class so you see in that video