in the previous video we got introduced to the list interface and we also
started talking about the ArrayList class so we are going to resume our grocery list
Java class we are going to finish that off and we are going to see how this all works in
operation so got the code to add a grocery items you can see on the screen
there we have the code to print the list of the items in the grocery grocery
list and also we can modify but one thing we haven't really done and we can also
remove as well but one thing we haven't added yet is the ability to query the
ArrayList to find an item in our
grocery list in other words so let's have a look at doing that we're going to create
a new method do that and the method is going to be find item so down here and
type.....so we are going to receive a string we are gonna search for that
in our ArrayList now what we could do one option would be to look at the code
similar to what's on line 15
we can actually use a for loop and go through each index entry and compare it
to search item and see whether it's a match and that would certainly work
but where possible especially when you're using built-in Java classes like
using the ArrayList you want to see whether there's an ability to shortcut
some functionality and to do things for you so there is one example of this
would be using the contains functions and I'm going to type the code and we'll talk
about it so...and if we have just a
look at the code I can hold down the command key and press or click on
contains you can see it returns if this list contains the specified element more
formally returns true if and only if the list contains at least one element I won't go
into the other details about equals and so forth because you'll be learning about that later
in the course the bottom line is that the code from ArrayList the code that comes
with ArrayList classes is part of Java has got the ability to do a fast search for
us so we could still write the code to do it manually going through and comparing
each element
why would you want to do that given that the code is being in built into the
ArrayList class to do that for you
so not only has it been added but has been optimized has been written by the people
who created Java and so you really want it where possible use that functionality
so this case all we have to do literally is type....select the name of our list
Grocery list.....and it'll return true or false true if it
can find the search item in the array list and false if not so with one line
of code where we are able to do that search so what we're going to do next is we are gonna leave
that line of code for the moment we are gonna type something a little bit different
because what we wanted do is return the item that's actually in the grocery list so
we don't want to just figure out that its in their we want to actually return the item back
because you can see that the method we've created is returning a string actually
what I'll do is comment out the exists because we won't need that now I wanted to show you how
that actually works because we are gonna use something similar though using the index
function or index of function so to do that you type....
....and what that does it does is a
similar thing in that it actually searches the array lists for you and it finds the
item that you've
passed in this case search item and returns the index position of our
array lists of that item if we have a look at the code again holding
command
press index it returns a negative one if this list does not contain the element
otherwise it returns the element so we can do check here to see if its equal
to negative one then we know that it didn't exist so what we can do there
after this line is we can put....
...and will put the code in here to
return null...
...so that's gonna
return a number corresponding to the index position in our ArrayList for the
search item now if that number returned is less than 0 we could actually put
less than its probably better to do that so... easier to read so if it is less than 0 sorry
if it's greater than
equal to 0 we need to return it otherwise if it's not greater than equal to 0 it
means it has to be less than zero it is going to return null its the code down here its going to
be executed so essentially we are saying here if it has found the element we want
to return the contents that's in that particular ArrayList entry back as the
return data otherwise we are going to return null now which means that we weren't able to
find at this point we now have a fully featured grocery list we got the ability
to add a grocery item we can print out the list we can modify an item in other words
update the contents we can remove an item from the list and we can now also
query the grocery list and figure out whether there's a particular item that
exists in their let's go back to our main class and write some code
now to start work on manipulating this list so go back to the main java and add
a couple of things we're gonna add a.....
...so that we can get some input from the keyboard and
we also want a grocery list so...
.....
...now that's not coming up and go back and have a look at the
grocery list class what I have actually done is actually when I created the class in
the previous video i put in the wrong place you can see what's happened so you
can see we've got the main class using com.timbuchalka but if we look at the grocery list there is
no package and that means that I put it in the wrong place what we can do
is click it once to selecte it and drag it into com.timbuchalka and release the
mouse and that actually says do you want to move so the good thing about doing this is
that any references to that code should be updated automatically so we
had an error in our main.Java but now if I click on refactor that error should go away
and the red underline disappeared and you can see that that fixed itself
and that's because we put it into the right class so again using the IntelliJ
tools where possible is a great way to save checking a lot of your code and
making sure you code is more accurate
so now to the main method we are going to start typing some code.....
...its gonna be instructions on how to use
the grocery list....so we are going to continue
processing until the user decides to exit we are going to type a prompt....
.....
....
.....
...and if I select number 1 that's
going to be the print outs so....
....
....all this little method we are going to write in this
class hopefully in this video and case...
....case six is a way to get out
.....and that's all we need to do so that our that's our main
method what we now need to do is write some of these other methods and
essentially just to be clear base on what the user has typed in we're going to perform
one of the following methods and you can see pretty well yourself it is self explanatory which
methods are which depending which option is selected and what I'm going to do is I'm
going to paste in some code here to save a bit of time for the print instructions so
come down here and paste that and you can see the list of options on the screen so option 0
print choice options which is the menu option option 1 is putting the list of items
2 is to add an item to the list 3
4 and 5 are modify, remove and search and number 6 is to quit the application so
that's pretty straight forward next to write is the addItem so let's
write that one so..... and what we
gonna do is we gonna do a......
.....
...
...so what ever they have type we are going to add that as an item so that's the addItem you
see it's pretty straightforward you can see that with one line scanner.nextLine its allowing us to
type in anything until return is pressed and it's going to take that whatever
their typed and add it to the grocery class using the addItem and of course that's going
to come back here and groceryList.add and add that to our array lists and notice as
I'm filling in some of these methods they are becoming highlighted and highlighted
as I mentioned in a previous is IntelliJ's way of saying it in use modify you can see
at the moment isn't being accessed so its dark gray so this can be a really handy
way just for you to see whether you've got some code that's not being used and
you can perhaps refactor your code and fix that code up to remove any
redundant code but we go back to main so lets now do modify and that's gonna do a similar thing
we're going to actually get the item into the item number and enter the new item
and allow you to update it so we're going to create that method so its going
to be.....
....
...actually what will do
to make it more sense will put....
.....
.....that will update it and if you go back to the grocery list
notice it that then highlighted because it's now in use we have now created that you might be wondering here
why are we deducting one from item number because you know arrays start counting from 0
but in the case of humans when printing out list and so forth we are starting the count from one
and that's why we need to deduct one of the number of the item number that has
been entered by the user to actually be accessing the correct element if you go
back to the grocery lists see here on line 18 we're actually printing an
item i+1 because its starting basically starting the list with one and that's what we need to
do deducted from what the user has selected because they would type the one
as the first item to go back and point to the right item ok next is the removeItem so....
.....
...so that is our removed item and next we only got one more to do
now search for item so let's implement that....
.....
......
....
.......
...and by the way in case your wondering I didn't add the -1
here to line 73 I'm gonna show you what happens with that not in there so
that's it now before we go through this one more time let's run and actually show
the output so run it you can see our menu options
so enter our choices so we can actually type 0 that should give a menu so it's actually
working nothing untowards there now if you go printout the list of grocery items
You have 0 items in your grocery list that's correct we haven't had any yet so
let's go ahead and add some items so I'm gonna do
option 2 to add an item to the list and what we'll do
is typed cheese for the first one and enter your choice again and we can 1 to
print the list to of items lets add another item so option 2 milk
print the list again we got two options that's working nicely and let's try adding one more so
2 bread ok and print the list out yes it is working so that's good so lets now
try and do a search so lets search for an item so option 5 item to search for lets type coke, coke is not in the shopping list
as we have expect option 5 again
bread found bread in our list so that is working nicely let's try to modify item
number so the first item number is cheese so change cheese let's type
cream cheese grocery item 1 has been modified so lets do a printout cream
cheese is now updated as you can see also got milk and bread still their so lets
now print out the list again to remind us what the menu options are let's try and
remove and see what happens now we should actually get something weird happening
so you can see the list on the screen option 1 cream cheese option 2 milk
options 3 bread so lets removed item three at the moment and look at
that we've got an error straight away and why we are getting the error well
the problem is we're basically deleting an entry that doesn't
exist if you look at the error index out of bounds problem is we've actually got
three elements starting from 0 1 2 in the array list and we're trying to access
position 3 item 3 because we actually chose item three here because we
didn't deduct one from it we're trying effectively to access element 4 of the
array list and we've actually got an error so we can't do that so what we need
to do is change the code and remove item to make sure that we pass a
negative 1 to and by the way what you could do there you can
come back to grocery list for the modified you can assume you could deduct
one of the position there instead bearing in mind the output is already showing a +1 and
likewise for the get here when you're removing we could have done that as well
we could have actually assumed and deducted the position of their but
generally we wouldn't want to do that because it's probably better
in this case to be updating the main class and because that's because we've
set up the mechanism on this page to print things in a certain order
and it does make sense to just sort of change this grocery lists class if
that's going to be the functionality but you see ordinarily you wouldn't be printing out the
list here you'd normally print out the print grocery list would be
something that probably you print out in the main class and consequently you wouldn't be
worrying about +1 so in other words you assuming anytime the addGroceryItem
modify or remove methods were called that the correct ArrayList entry
the correct number was already selected and you didn't need to add to take one off it
so just to make that clear you'd normally be doing that sort of coding
here so consequently I think it makes more sense at this stage with our program
to have the negative ones in here so ok I've got that now working so let's
go back and run a program again very quickly and will add
some entries in here
option 2 bread option 2 cream cheese again option 2 milk option 1 we got 3 items you will now be able to remove 4 item three
and now if we do a print-out we should find we only got bread and cream cheese left because
we've deleted milk bread and cream cheese so that's good that's actually working like
we should like we wanted to another thing just before I finished the video if you go back to the grocery
list is we didn't have to use
positions what you might want to do in an ideal scenario here is not force the
user to have to specify position but to actually use something along the lines
of what we done with findItem where they can just do a search if you wanna remove an item you type in the item
number and you'd use a mechanism similar to what we did in findItem to
find the position and delete it that way that's certainly something you could
do in actual fact in the next video we're gonna do just that we're going to
leave the existing capability of these methods to use positive position numbers
and the string but we are gonna add some new methods so you can just enter the item
and actually finds the item and updates it for you so it's just making things a lot
easier so you have seen the long winded way of doing it
it in the next video you will see the shortcut so I'll see you in the next video