So in the previous videos, we went ahead and found out about the ArrayList, and
how useful it is, and how to use that in Java.
It's now time for a challenge.
What I'm going to do is type out a challenge
to help you understand how the ArrayList works.
So let's get into what the challenge is all about.
Okay, so here is the challenge.
We want to create a program that implements a very simple mobile phone
with the following capabilities.
The ability to store, to modify, to remove, and to query contact names.
The contact names are those records for
people who have been added as a contact on the mobile phone.
So what you'll wanna do is create a separate class for contacts.
And the actual fields we want would be name and phone number.
So you create that separate class.
Then you create a master class,
which we'll call mobile phone, and that holds the ArrayList of contacts.
And the mobile phone class itself has the functionality listed above.
And in addition, when we're running the mobile phone application,
you wanna add a menu of options that are available to quit,
to print a list of contacts, to add a new contact, update an existing contact,
to remove a contact, and also the search slash find a contact.
Be sure, when you're adding or
updating, that you check if a contact already exists, by using the name.
So you'll just check to see whether that name is already on file,
in a contact record that's in the ArrayList.
And also, be sure not to expose any of the inner workings of the ArrayList to
any class that's accessing this.
So in other words, no direct ints.
No using .get(i) in the ArrayList.
So you'll create a contact object containing the name and
the phone number, and then you'll parse that through.
And that will do all the processing.
So in other words,
with the mobile phone class we're only dealing with contact objects only.
So we're not exposing any of that functionality.
The private methods may indeed do that.
And probably, you will want to do that, so that you can update appropriately.
But in terms of what you're showing from a public method point of view,
you shouldn't be showing integers and using .get(i).
So in other words, the people using that class from the main method, from
the public static void main method that you're gonna be running your example code,
you shouldn't be using ints or anything like that to access particular elements.
And that's all as we talked about in the previous video, if anything is uncertain.
So that's the challenge now.
So go away and try that out, and see how you get on.
And pause the video now, and when you're finished, come back and
check your results with what I've come up with.
Okay, how did you get on?
Did you figure it out and get it all working?
It's probably the most complex application that we've actually done in the past.
There's quite a few little factors here to deal with.
So let's make a start.
So step one was to create that program which we talked about.
But specifically, we want to create a separate class for contacts, and
that's the name and the phone number.
So what I'm going to do is just open up, and select or click on my package.
Click on and select New, then Java class.
I'm just going to call it contact.
And the contact record itself is going to be very simple.
It's going to have two fields.
Private string name.
Private string, and it'll be phone number.
We're going to create a constructor with both of those fields.
And we're also going to create just getters.
We won't actually need to do setters.
The reason we won't use setters is any time we're dealing with a contact record,
we're gonna use the constructor.
But we do want to use the getter,
so that we can actually print out that information.
So I'm just gonna select those.
Now the other thing we want to do, which you may not have thought about, is we want
to expose a public static method, so that we can create a new contact record.
And this will be very useful when we are accessing
the functions that are built into mobile phone.
So instead of having to parse the parameter for the name and
the phone number, we can create a contact record.
And we can also create it without creating a new separate object, and
I'll show you as we move through.
But for now, let's create that method.
And it's really just a one-liner, so it's public.
Static is the important thing here, cuz that means that we don't have to create
a new object instance for this contact class.
It's gonna return a contact record.
We're gonna do create contact.
Now, this will accept two parameters, string and the phone number.
And we're going to return new
contact, name, phone number.
So you can see what's happening there.
It's really just calling a constructor to create a new contact record.
Pretty simple, and it returns that contact record.
And you'll see this in use later on in the application.
Okay, next we need to create our mobile phone class.
We'll just go back and have a look again.
A mobile phone class has the functionality listed above.
The various abilities to add, modify, or remove and query the contact names.
And it also contains an ArrayList of contacts.
So let's create this mobile phone class.
New Class > New Java Class > Mobile Phone.
And what we're gonna do there, I didn't actually tell you to do this, but
I'm gonna actually create an option for it to store the mobile phone number,
just to put in a constructor.
It's certainly optional, and you won't lose points if haven't got it, but
I'll show you how it works shortly.
But the important thing here is the ArrayList.
And in this case, it's gonna be ArrayList, and it's going to be of contact records.
So we're selecting contact instead of string, which you've seen previously.
We're going to accept that import, and we're gonna call that myContacts.
We're gonna create a constructor.
And the constructor is going to accept as a parameter the myNumber,
and that's gonna be the mobile phone number.
And the other thing we should do in here is we should initialize our ArrayList.
And normally, when you've got an ArrayList that you're using in a class like this,
you want to initialize it In the constructor, normally.
In this case, we're gonna initialize it with an empty
list because we're gonna start off with a clean slate, so to speak.
So it's new ArrayList, like so.
And it's just creating an empty list, as you can see there.
So moving on, what we need to do now is start implementing some of the methods.
And the first one I'm gonna start with is the Add New Contact.
So this is where we're actually adding a new contact to our mobile phone.
So to do that, we're gonna create a public method.
Public, and we've got to return Boolean to see whether it was successful or
not, cuz the hint to returning Boolean was in the message that I left here.
When adding or updating, be sure to check if the contact already exists.
And to check, we're gonna be using the name.
If we're checking to see whether it exists, that implies that we want to tell
the process that's calling the methods whether it successfully added or updated.
So what we're doing here is we're gonna return Boolean.
True would mean that it did, in this case, get added successfully,
because we're creating the add new contact method.
And false would mean that there was a problem with it,
that it was already on file.
So I'm going to select Boolean, and it's gonna be called Add New Contact.
Now the input parameter is gonna be Contact contact.
So we're not sending the string and the phone number here.
We're actually sending it a contact record,
or contact object, which has been created outside of this method.
Then what we need to do then is test to see whether it's on file, so
we're gonna put if(FindContact(contact.getName())
>=0).
And FindContact hasn't been implemented yet.
We're gonna put "Contact is already on file".
And we're going to return false.
Otherwise, if it gets down here,
then what we're going to do is we're going to put myContacts.add(Contact).
We're gonna add that to the list of records.
And a contact that has been parsed in this method already contains the details,
and the name and the phone number.
The last line is going to return true,
which means that we successfully added the record.
So that's our add method.
And what we'll do next, now, just to get it out of the way,
is we're going to implement the find contact.
And what we need to do is, as I was showing you before,
we're going to implement two find contact methods.
In other words, we're going to overload one of the methods.
We're going to have one that returns the index position, and we'll need that so
we can actually edit that within the ArrayList.
But we're also going to return one, that in this case,
we can just parse the actual name, and for it to find that in the list.
So the first one we're going to implement is the one that returns the integer.
And we're going to make that a private because it's not gonna
be exposed publicly.
It's only used within this class.
And it's going to be one contact.
It's still going to receive a contact record itself.
And we're gonna return this.myContacts.index(Contact).
And that's going to return an integer showing us what the element
position is in the array for that particular contact object.
Now keep in mind that the number that will be returned will be zero or
greater if it exists in the ArrayList, and
it will return a number less than zero if it doesn't exist.
So we can just use that number to determine whether it actually exists or
not by doing it that way.
So that's the first one.
Again, that's returning an integer.
And the second one we want to return is also a private.
We're going to overload it this time.
It's going to be findContact, but
it's going to be accepting a string, so it's contactName.
And what we're going to do is loop through all the records.
And we're gonna extract the name out, and then compare that to the name that's been
parsed, and return the index position if it's actually on file.
So to do that, we're gonna put for(int i=0)
i<this.myContacts.size, i++.
We're gonna create a new object, so
that's Contact contact = this.myContacts.get(i).
So we're creating a new contact object,
which is basically the contact object that's in the ArrayList.
Then we're doing a test.
We're gonna put ifcontact.getname.
So we get the getname field from the contact record.
If that.= the contact name that was parsed through this method.
If it does, we're gonna return RA.
RA being the element,
the array position number in the ArrayLlist that we found this record in.
And we're getting that number so we can decide to actually update it, if we want
to, when we write the modifier method, or in the case of the add new contact, above,
we're testing to see whether that number is greater than or
equal to zero, to determine whether it's on file or not.
So the last thing we wanna do is put a return -1 here.
That's the last line, and what that means is that it wasn't found, and to return -1.
So getting back to add new contact,
what that means is we're actually doing a test here.
If it's greater than or equal to zero, the contact is on file.
And because it's already on file, we can't add it again.
We should return an error because it would be a duplicate.
So we're returning false.
So that's calling this method here parsing the parameter of the name.
Then again, we're just looping through all the elements in the ArrayList, checking
each one, checking the name to see whether it's equal to the name that's been parsed.
If it is, we're gonna return that position.
If it's not there, we're gonna return negative one.
So that's it.
That's the find.
So the next method we want to implement now that we've done the add new contact is
the update content.
And you'll find it's very similar to the start of the add new contact,
so we need to actually check what's there first.
So we're gonna do a public cuz this is something that we do want to be exposed.
Public void update contact.
Now, this is gonna have two parameters.
Contact oldcontact,
contact newcontact.
So we've got the old contact that we're searching for,
which we're seeing is on file.
And the new contact is the one that we're gonna actually update it to.
So what we're going to do first is we're going to do a test.
Int FoundPosition = findContact(oldContact), like so.
Then if(foundPosition <0).
We actually want to return the message, and the message should
say allContact.getName was not found, because
we couldn't find it because the actual position returned was less than zero.
And what we should have done there is returned false.
That shouldn't have been a void.
That should have actually been a Boolean,
because we're returning a state whether we succeeded updating it or not.
Return false.
And so if you've got to here, we know that the foundPosition is greater than or
equal to 0.
And if it is, all we need to do is to update the ArrayList.
And to update it, it's .myContacts.set.
Parse the found position.
So, in other words, the element we wanna update.
And then we also wanna parse the new item.
So it's the new contact,
and that's going to replace the old record with the new one.
Then we'll print out a message to put oldContact.getname
was replaced with newContact.getName, and then returned true.
So that's the update contact method, developed.
So what I'm going to do is I'm gonna pause the video here now,
cuz it's getting a little bit long.
And we'll come back and hopefully finish this off, and
actually run this in the next video.