so in this video what we are going to do is continue on with our setup and we're going to set
up a shopping basket because of course the real aim of this example which i
mentioned a few videos ago is to provide a shopping basket now I've used a map
for the stock list as you saw in the previous video and the reason I did that
was because this section is about the Java Collections Framework but the actual stock
list could be stored in a number of different ways and with the database
probably been the most likely one you go for when customers come to buy the
goods we need some way to store the items they're buying and of course the
quantity of each one so we store the quantity in stock in a field of the stock
item class that's fine because it only one stock level for each item
but when we come to the shopping basket though the program will have to keep track of
the quantities for each customer and their could be thousands of customers so
having a field for each one of the stock item classes is obviously not an option a
map is designed to store key value pairs so it makes a good candidate for
associating a quantity with a stock item and by creating a new shopping basket
for each customer the program can track how many of each item that the customers
are actually buying now the stock item is the key in the map in this situation and the
quantity purchased is stored as the value for the relevant key now I could have
just used the items name as the key but then the baseket class will have to look up
the items in the stock list to update the stock count or get the price so by
storing the actual items the basket class can retrieve any information that it
needs directly from the item which obviously is pretty convenient so let's go ahead
and create that baske class now so I'm going to create a new class basket just call it basket
need the name first.......
......
.....so we're setting up a
constructor
we are going to pass just the name or use that as a parameter I should say
and we are also going to initialize the map so that we remove that error so....
...so the next we need an add to basket and that's going to accept
the stock item or be past as parameters as a stock item in the quantity and
bit of checking to make sure that the parameters are valid so.....
....that is how many there actually ordering the
customers ordering some basic validation....
.....then we going to do check if we already have this
item in the basket I might actually be adding another one so.....
......
so we are actually checking to see whether that specific stock item is in
the basket already remembering that the key is the stock item that we are searching on
and that's passed to this method by the parameter and we are defaulting to 0 if in fact there
wasn't any previous item so that we don't sort of trying to tally up
incorrectly ok then
we also need to do.....
.....
....if their was a value remembering that it will default to 0 if their wasn't one otherwise it will be whatever
quantities already sort of the shopping basket for this customer.....
so that we can keep adding items if we choose to and we.....
...hope that its pretty self explanatory
and we now also want an item's method again as we did for the stock list so......
....and we returned that un-modifiable map again so....
......now we're going
to also write a toString method which is pretty similar to what we did for
stock list so we are going to override that during......
....
......
.....
......
.....
......and
that's our basic basket class so again the items in the basket are stores as a map
and the actual item is the key that we are using and the quantity purchased is the value in the
map add to basket check the list of items to see if an item is already
present in the shopping basket when a new one is being added a you saw me do
that on line 21 and by using get or default 0 we either get the quantity already present or 0
so this amount is added to the continuity you could say if you do that and handle the
scenario whether it's a new item or there is already a quantity in there now when the
item is put in the map obviously it replaces any quantity that may already be present
and that's of course reason we have to
potentially add whatever quantity was already existing
if there was any the items method is the same as the stock list class very
similar its the same actually
returning the un-modifiable map for our map for this particular class the basket class and of
course we overrode toString again ok so let's now go to our main method
and add some stock add some item to our stock and start selling them so go back main
we haven't been here before for this set of videos and we are going to create a stock item so....
......
.....
......
.....
......
.....
......
.....
......
.....
......
....now we should be able to do......
that should use our toString method that we overrode to give us an
overview of the actual stock list so all the items we've got so lets
go ahead and run that and see what happens
ok we see door, bread, car, phone, towel, cake, etc and total stock value is 5157 we also got issues here
with the values notice how the decimal points you get that sometimes with
doubles we can fix that
to fix up so that the output its a bit nicer lets go ahead and do that just
to be complete and and the simplest way of doing is a go back to the
stock item class or actually we are not using toString for that one we using the stock list so we are going stock list class
so we got item value in here but we can do if we want to format so its a bit nicer we
can put string .format do something like.....
if you do that we should find that they will only showing any with two decimal
points the .2f with the . means just to use 2 decimal points two
decimal places I should say and the f is just sort of saying it's a real
number a floating number that we're going to be using so if we go back to main of mine and run
this
you can see we are now nicely formatted now with just two decimal points so that's that
done so obviously that's working and we can see that its outputting and if you wantted to we
can go back to the individual prices here as well and format those but I
haven't done that I just worked on formatting the final values but note
looking at these here that the items themselves are in pretty random order so
even though we added them alphabetically so you will notice here these were actually typed in
alphabetical the actual items are still stored at the moment in a pretty random
order and the reason is because the stock list classes is using a HashMap to store the
items the orderings undefined for that reason so to maintain alphabetical order
that we added the items we are gonna change stock list to use the linked hash map instead easily test and
this is how easy it is to change so go back there now go back to stock list
obviously up here we had it defined fin our constructor as a Hash Map if I change that to
link to hash map just accept the import and obviously there be an extra one here we could probably delete that import because we aren't using
that anymore but that's the only change we made and if we go back down run our code we should find that
everything is now showing in alphabetical order
pretty cool as you can see that's working nicely and that's one of the big
advantages of when your coding just to use generic set ups like using maps that I've
used here because that's the interface we are able to specify the actual class we are
using here in our constructor and as you saw there's really only one line
of code that I needed to change and we have change the entire implementation pretty cool as
you can see and just in case you're wondering if I close this down
you might be wondering do we need to change something for our items method
well there is no need to change the map return by that method in fact there's no such thing as a
un-modifiable link hash map so remember that the un-modifiable collections objects and
just a view into the underlying collection so the map return by the items
method will have the same ordering a our main list and just out of interest
you can actually check this printing out the keys in main
if you go back to do that if we have added this code......
.....
so if we run this you can see that the items are stored the same way
as our map was actually stored the way its been configured then we can confirm that by
going back to our stock list changing it back to a hash map running that again
you can see
we got that unspecified order we are not necessarily in a alphabetical order but the
order specified returned by the modified map the un-modifiable map
is identical to the map order that's been returned by default so it's
basically just a view into the underlying collection and as a result
you have the same ordering from the un-modifiable map as you are from a
standard map so go back and change this back to a linked hash map again ok so I'm gonna stop
the video here
because time is ticking on the next video we are going to continue we're going to try selling some
items and just to check and make sure that adding an existing items works with a
different price point and just basically confirm that these other methods that
we have written are handing the stock in quantities correctly so I'll see you in the next
video