So, in the previous video we started our challenge for the array list.
And what we did was we implemented the contact class and
also the mobile phone class as you can see.
In the mobile phone class we've implemented the add for
the add new contact while updating a contact.
We've also got the ability to actually return a contact.
We need to change this a little bit, cause at the moment we've got two methods for
finding contacts with their returning integers, but
of course we don't want to return integers back to the calling process.
So, we're gonna create another method that will expose publicly to allow other
functions to other calling classes I should say.
To access the record that we found.
And the other thing that we haven't implemented yet
in the MobilePhone class is the ability to remove a contact.
So, let's create the findContact first.
And again, we've got two parameters here, we've got a two methods I should say,
findContact, one that has been obviously overwritten.
So, I'm going to create a third one and the sole purpose of the third one is
really just going to be to return the data.
From another method.
So, returns it as a string instead of returning it as an integer.
If that makes sense.
There's different ways of doing it.
We could actually set it up to return it barely.
And if it wanted to.
The main thing is, we don't want to expose these private methods publicly.
Because they're using the int.
Which is used for our array.
So, we don't want the that are access this to know anything about that.
So, I'm going to create another method.
A public one and I'm just gonna call it, put the space here.
And we'll call this one query contact.
And return a string.
Query contact.
And it's going to accept the contact record.
And we're going to put if and
we're going to put pass on to the fine contact record so it's if fine contact,
contact if that is greater than what equal to zero, we're
going to return contact.getName.
Otherwise we're going to return null.
All we've done here is we've sort of sent a contact record, if we're looking for
that record, if the result of that which essentially is going to be calling this
method here if that returns a number greater than, or equal to zero,
that means that it is on the file, and if it is on file, we're going to return
the contact name to really indicate the calling method that yes, it is on file.
Otherwise, we got to return no,
and that signifies that it wasn't on file and it doesn't exist and
you'll see that in use when we go into our main method from the main class shortly.
So, that's that one.
And then lastly you want to create a remove contact.
Again put that up there cause it's close to the other methods.
And it's gonna be a public.
Gonna return a boolean.
And it's gonna be remove contact.
Same thing, contact, contact.
What I can actually do here is called a code and copy and paste it in this case.
Like so, so
our first test remove contact is if it's not on file we are just going to return,
false, we are going to put a message on the screen to say it wasn't found.
Assuming that it was found, what we really need to do is put.
Just type my context.remove, now in position.
And then we'll just put out a message to
say contact.getName was deleted.
And then we want to return true to say we were successful and
we did actually manage to delete it.
So that's our classes, our methods for the MobilePhone class now.
You should be good to go with that.
So, what it's time for now is to swing back over to our main class and our main
method, and actually do a test run to make sure it's all working as it should be.
So, to do that we're going to back in here and see if we can see into the main method
in the main class and we need to implement a few things we need a couple of fields we
need a scanner firstly so public or private I should say.
A private scanner, a private static scanner.
This is gonna be the variable and accept that import equals new scanner.
It's going to be system.in which was done in quite a few videos now.
The second thing we want is a MobilePhone class.
The one that we've just created.
So, mobile phone MobilePhone = new MobilePhone.
And I'm just making up a random number.
And that's the only reason that I added the field in the MobilePhone class for
number is so that we can actually pass it as a parameter.
Certainly optional and we're using it for any other purpose.
That's the basics.
So, the next thing we need to do is implement this menu so
that we've got these various options quick list,
add a new contact updating existing contact and also remove contact.
So, let's start on that.
So, boolean quit equals false.
Now, I'm gonna have some code to start phone.
Print actions while not quit.
So, while they're still having selected that command, we're
gonna start our wall and what we'll do is we'll implement these other methods first.
They're pretty simple methods.
[SOUND] startPhone.
[SOUND] Obviously, a very simplistic representation of starting a phone.
But, if you were writing the code to actually develop this.
You might have all sorts of other functions that you might be initializing
the memory and do all sorts of other things.
I just wanted to sort of get your mind thinking about the possibilities of how
you'd go about that if you were creating a class of this complexity.
In this case we're just gonna print out a message that says starting phone.
Next is our actions, static void printActions.
And if remember that from the previous video, you've seen that in use.
So, you're going to need to go available actions,
Press, and we'll do something like
0 to shut, whoops, put 9 there.
0 to shutdown.
[SOUND] And a one.
To print contacts.
This is another way, by the way, to avoid having to print out numerous print lines,
just to put all in one line like this.
Option two is gonna be to
update an existing contact.
[SOUND] Three actually that should have been
four because three should have been add.
Add's gonna be our first one.
Sorry. I'll get this right in a minute.
Two should be add.
So, to add a new contact.
Okay, wait a minute.
So, three should have been to update.
That was correct and the next one is four.
You can probably guess what's that gonna be, to remove a contact.
Remove an existing contact.
[SOUND] And five.
[SOUND] Query if an existing contact exists.
[SOUND] And number six, the last one, is going to be.
To print lists a list of a available actions.
Like that. And a little bit so
that's just our big print and then we'll just do another message.
Choose direction.
Like so. And that's L connection.
So, that's really just a menu.
A menu of what the pre-productions are.
So, let's go back now.
We've got print actions and they're both implemented.
So, we'll start implementing I should say, our wall link.
With our wall link we need to now go through and add a case using a switch for
each of those, but
before we do that of course we need to print a message on the screen.
But, also then to accept the appropriate option that the user is specifying.
So, we're back up here to our quit.
Or not quit, we're gonna put message in there.
Interaction.
Six to show available actions.
I'm putting that in there course because this menu will be shown over and over.
And you may actually miss it in the initial print actions, that was printed
before the loop started if you actually scrolled that all off the screen.
So, it's good just to have that there to remind the user.
So, let's get an integer.
Scanner.nextInt, and go to the next line as well,
clear input string, okay then a switch on the action,
saying for case 0 option 0 was shut down so
we're just gonna put something, we'll just put a message to that effect.
[SOUND] Shutting down.
[SOUND] Quit equals true and then break.
Case one or case one was printing our list of contacts so one.
PrintContacts, we're going to create a method for that and then break.
We might actually go down and actually implement that method first,
before, just to avoid there being lots of errors on the screen and
we'll do that above the startPhone because we'll be referring to that more.
So, it'll be private static void, print context.
Now, this does bring up an interesting question and the question is should we be
implementing the print contacts by writing out all the code to iterate through the,
to go through all the list of array list entries here or
should we be doing it in a MobilePhone?
So, first I'm going to create the content here.
Then we'll discuss that further.
Then we'll just type contact list and
you can probably guess probably what we're going to do.
We'll start something like 0 I or less than I mobile phone I should say.
MobilePhone.get and what we should do is think about getting
a getter to actually retrieve the array list but this is what I'm getting at.
So, what we want to decide is whether we should be adding this functionality here.
In other words, should we be going through the list and so forth.
My argument would be that we wouldn't be doing it here
in actual fact what we should be doing is implementing a print
context function with the mobile phone class self like so.
So, it will be something like mobilePhone.printContacts.
In other words we take this code out and we go back and
implement print contacts which is in the mobile phone class or will be in there.
That's what we're gonna do.
We're gonna add that down at the bottom.
And it's going to be public, not static, public void printContacts.
And it's going to be this.myContacts.size, i++.
And we're gonna do something like i + 1 +.
Get(i).getName + I find
out the phone number as well.
This.myContacts.get(i.getPhoneNumber).
Like I say, and that's literally it.
So, what we've done here is we've just navigated through the entire contents.
We know that it's a contact record, so we're retrieving the record and
because we have to find it up here as an array list, I've typed contact.
When we use a dot get and Java knows that this is automatically a contact class or
contact object that's in the array list subsequently the get name and
the get phone number work, the getters work.
Which are from the contact class itself.
The getters that we've actually established for getName and
getPhoneNumber.
So, that's again one of the beauties about using array lists and
defining the type is that it makes it a lot easier to do this type of code.
So, what we've now done is we've implemented a simple printContacts
function just to go through the list of array elements.
And to print them out.
So, you can see that by having to navigate using index positions and printing out
numbers and so forth, this really shouldn't have been in this code here.
We shouldn't have been implementing it in there,
because we don't want the class or the code that's called
calling the mobile phone code to know how the underlying array list works.
Or even that there's an array list in use.
As far as we're concerned we've just got a mechanism that we can add records to,
we can update, search etc.
And print and remove as well and that's all we need to know about it.
So, that's why we're implementing that in the mobile phone.
So, we could of actually just changed this,
in fact I might do that just to be a bit more simplistic.
And go back up to our print and
we can just actually literally just replace that line in there altogether and
then actually delete this cause we don't use that anymore.
But, I did actually wanna start during that just to show you that you should be
thinking when you're creating methods is this code that I'm creating
is in the right place or not.
It's very important to get that right and is to be thinking about that.
Cause it can be very useful.
Extremely useful if you figured that out, and
make sure that you're actually putting it in the right place.
Okay, so moving on.
We've done our print context.
The next option was option two from memory, which is this one here.
Which is to add a new contact.
So, we need to add another case for that.
Case two, and what I'm going to do is I'm going to just add a method for
each of these.
And you'll see why shortly, so
in case two, add new contact [SOUND] break.
Case three.
It's gonna be update existing contact or update contact.
[SOUND] break.
Case four.
Of course case four was removed so that's gonna be remove contact.
[SOUND] break.
Case five, that'll be queryContact, see that on the screen, query.
QueryContact.
Break.
And then of course case six was the print actions, so
it's gonna be print actions break so that's our switch and
the various cases that we need to implement or that we have implemented.
Next, we need to implement these methods, add, update, remove, and query.
So, I'm gonna do that just here.
So, starting at the first one, now we want to add is actually add new contact.
Now, the reason I want to make this a separate method is of course,
we need to actually ask for the name.
And then actually call the codes.
The few lines of code needed to get this all implemented.
So, we going to put private.
Private, static.
Void.
Add new contact.
Put out enter view contact name.
Dream name= scanner .nextline.
So, we're going to retrieve the contact name that we want to add.
But, we also need to add a phone number as well.
So, to do that we're going to put.
Enter phone number and
string phone = scanner .nextLine.
So we're gonna, phone number as well.
So, we've now got the name and
the phone number that we've read in from the keyboard.
Need to create a new contact, and we do Contact
newContact = Contact, and you'll notice this code here,
Contact.createContact(name, phone).
And you can see how we've used the reference to the contact class but we
haven't used an object, we haven't created a new instance we've used that directly so
the capital C equates to the actual name in the class and of course the reason we
can do that is because going back to the contact class it's a static method.
So, that's the reason we wanna use a static method there,
because it's a convenience method.
It's also known as a factory method as well.
That's another definition for it.
And it's just a simple way so you can see here we wanna create a new
contact record without having to create a new contact class.
Without necessarily having an instance of one object to call a method on.
And that's what the static allows to do is send that line there.
So, therefore, we've got our contact added, which of course we need to
pass onto the add new contact in the mobile phone class itself.
So, last thing we now need to do is,
put if(mobilePhone.addNewContact)
[SOUND] we're gonna put new contact added and
it's going to be, we can actually put name.
Name, phone, keep it simple here.
Phone.
Else.
Cannot add name, already on file [SOUND] so
we're taking advantage of the boolean return.
If it's true, then this line of code is checking to see if that's true or false.
Were we successful in adding in the new contact.
If we were, we're just gonna print out a message confirming what we've added.
Otherwise, if that returned false add new contact,
it means that it found a record for that name already.
And If that's the case, we're gonna print a message to say, cannot add,
the name is already on file.
So, what I'm gonna do is pause the video here and we'll come back and we'll
implement the remaining three classes and go ahead and actually run this.
I will see you in that next video.