ok so moving on now what we want to do is create a stock list class now
this is going to hold a list of all the stock items in a map and will need method in this
class to add stock to the stock list class also to remove it when the
actual items the stock item product is actually sold now
because the stock item class uses the name when overriding equals and
hash Code we can safety's use the name as the key for the map which is
going to make our life a lot easier so lets go ahead and do that and then just a note also to
make sure because of a current big with IntelliJ which may or may not be there
when you go to check this click on the actual project in IntelliJ right click
click on open module settings go into to project and just make sure that that's
set to 8 when I created a new project earlier it was 6 and I change it when I wasn't recording to 8
so make sure that is on 8 otherwise you can get some weird things happening and
mentioned that in other videos and this seems to be a bug with new projects even though we have set
that in our general defaults it doesn't always stick when we create a new projects so
actually on the project language level is on 8
ok let's go ahead and create that class so going to call it stock list and we
mentioned that we need a map and the map is gonna store stock items and we're
going to use a string as a primary key and that's ultimately going to be the stock item
name so I'm going to do......
.....
...and we are getting an error because it wants to be initialized and
we just confirmed that Verbal List my not been initialized because we marked that as final but
we're going to do then in the constructor now so....
.....
...next we want an add stock method and that's ultimately going to be used
to add items to our list of all stock items so.....
.....
.....and because if it is null an we got an item pass to us that is nulll we don't want to
do any processing on it so we wrapping this in a test for not
equal null so if its not equal to null we are gonna...
...and normally we would use get and do something like.....
and that will retrieved the stock item based on name but we are going to change that we're going
use something called getOrDefault and that needs to parameters the first one is the item name
to look up and the second parameter is going to be item and that method does
what you think it would do its get or default so its going to get the item if it
already exists in the map and if it doesn't exist its going to use this
item that we actually pass to this method as the item is going to be in
stocks so either way at the end of this line in stock is going to have an item
either the one that has been retrieved from the list or the one that has been passed to this
method and then what we can do is to check if we already have quantities of this
item and what I mean by that is was this already in the list so the way to do is.....
.....
......
...so by putting in stock not equal to item we are checking to see did we
actually get the list of the item out of our map and is that the same as in stock item
that has been pass so by checking to see whether they're separate we can
determine basically whether the item was already existing in the map because it
was already in the map then what we want to do is we want to adjust the stock quantity with
whatever the quantity that was passed to this method via the item variable here
on line 16 but if we haven't gone an item that existed in the past then we can just used the
quantity that's been passed in this so in this case if in stock is not equal to
item then we do.....so it basically what
we're saying there is if they're not the same in other words it already exists in
the map prior to the call of add stock then we are actually going to pass the
value of whatever was already in the map into out current item
item.adjustStock so that we actually got the quantity both that existed prior to the
call of this method and also the quantity that has been passed by the
stock item object and we are only doing that if of course if they are not the same but in either
case what we are gonna do is put....
so you can see either way whats going to happen
is we are going to get an add entry written to our map and if it already exists what automatically the .put method
will overwrite that is in their otherwise if it doesn't exist its just going to add it like normally it would
.....
outside of the if loop we are just going to return 0 indicating that their was no stop movement so hopefully that made sense
so we're adding a new stock items to the list and updating them essentially if
it was already a stocked item with that name now just out of interest if we
wanted to we could actually change his little bit we could have actually gone
ahead and done stock item just comment this out briefly just to show the old way of doing it it might
have been something along these lines....
.....
.....so you can see the difference here that's
really just the other way of writing is we are basically saying you know before we
actually go to add the item on line 26 or replace it we are just testing to see
whether there's already one in the map prior to us and inserting whatever is
passed to the method and if its not equal to null it means it found something then we
adjust that stock that way but we've done it slightly differently and I'm
undo those changes by using get or default it just saves that extra test for null
now because either way that is going to return a valid stock item but then we have
also got this additional check instead of checking for null we are checking to
see that they're not the same which means that the item already existed
in the map prior to the call of this method ok so that's add stock we also want sell stock
so obviously if the stock it's been sold we need to adjust the
levels accordingly so we put.....
going to be the item which is a string so we can look up our map and the quantity.....
so we are going to use the same thing here we put....we use it in a slightly different way.....
....
.....
....which remember this time item is just the name its a string whereas
in the add stock method on line 16 it was actually stock items so in this case
we just use item but we are gonna default it to null if it doesn't already exist because
we're just selling stock the assumption is that the stock item has to exist so
we aren't going to actually use the default and then the next line when we
actually check this will do a test to make sure it's not equal to null so put.....
.....
........and the last test we need to do is check that the
quantity that is passed to this method is greater than zero because we're not
going to allow any negatives or credits or anything like that so we are just gonna put.....
....so that is our test to make sure it is valid and first test should have been if stock
....so if in stock is not equal to null and if the get quantity in stock is
greater than the quantity that we are asking for a to be sold and the quantity
that we asked for it has to also be greater than 0 if all of that is true
then we are going to do adjustment so put.....
.....obviously quantity we determined is
going to be a number greater than 0 because we are deducting it we are putting a minus in front of the
variable to make sure that the negative amount gets adjusted and then we return
quantity to indicate that's how many items where taken out of stock and down here if we get this
far it means that one of those conditions wasn't true either we didn't
have it in stock or there wasn't sufficient quantity to take it
out of stock or the quantity that was passed is equal to 0 or less than 0 if that's
the case we are just gonna returns 0 to indicate that there was nothing that was
actually taken out of stock let's also create a getter so put a getter for our
stock item actually we have to create that what we area able to do is pass the name of a stock item and return the
stock item as an objects so we are going to return a stock item.....
.....
....and next we want to be able to return
the times so put....
....
....and come back in a minute talk about
un-modifiable map but it's probably thinking is that actually mean a map that can't
be modified you pretty well spot on so the other thing I wanted to do then we want to
override a toString so lets just do that as well.....
so we area going to write a toString method bearing in mind that this is the stock list class
so we are going to do something a little bit different here the to string is going to
return a list of all the items so in other words that is a complete stock list even
though it's just 1 methods in the course so far with to string we are
really use that for a single item but there's no reason we can't override
toString for a class like the stock list class that maintains a quantity of a given
number of objects and have it returned quite a complex bit of
output so we are gonna do that so put.....
.....
...so we are going to iterate through the entire map and we are going to get each item so....
.....
...this is going to be the string formatted...
....
.....
.....after we've been through the entire
list of items returned......
....ok and what I'm also going to do
I'm going to take the opportunity to come to this method I'm gonna go into their command click I'm
going to rename that because I don't like the name of that refactor rename
gonna call that quantity in stock which makes more sense
because the way we are using it and press ENTER and it should be changed everywhere so we should
find if we go back to our stock list that has been changed as quantity in stock and again
that IntelliJ smart refactoring has done that for us or automatically and that
makes a bit more sense and just a note about what we just done with to string
obviously we have overridden it now as I mentioned to print the full list of all the stock
together with the total value which is pretty cool but generally its probably not a good
idea to do this in practice because two strings really intended for debugging
and with a large stock list especially you know create a big log
files and we are gonna be covering this more in the section on debugging but for now we are
really doing this just to give us a quick way to display a stock list
and you wouldn't necessarily do that in production now the program using
the stock list will probably want to display the list in different ways and
so the stock class also has an items method that provides a map of all
items in stock and that's why we've created that on line 46 know what we
haven't discussed yet is the un-modifiable collections object now on lecture 107
on making classes immutable we returned a Hash Map and we did that by
creating a copy so that the calling code could have access to the
list but we couldn't change the exits in the game not sure if you remember that or not if not
it might be a good idea to go back and check that out now that was fine but the calling code could
also add new exits to a map or to the map so well that didn't bother our class at all
because the caller was working with a copy it's probably bit unkind to let
the code think it could make changes when it actually having no effect so the
collections framework provides a wrapper around the list
map and set collection to provide an un-modifiable collection so we control
click on un-modifiable map that's command click on a Mac do that now and just look
at the notes above it return an un-modifiable view of the specified map
this method allows modules to provide users with read only access to internal
maps
query operations on the return read through to the specified map and
attempts to modify the return map whether direct or via its collection
views result in an unsupported exception so what we are really returning is actually
a read only view into our map but this prevent other code from adding or removing
items and its faster than the previous way from lecture 107 and the reason its faster is because it
doesn't have to make a copy of the collection ok so time is ticking I'm
going to end this video here now but when we get back we are going to continue through
our example we're going to start building up a basket which was really
the idea of this series of lectures was to go through and create a shopping
basket so we'll start doing that in the next video