In this video, we're going to fix a small
bug that occurred in the challenge
solution. So after testing the code
perhaps you've noticed that we can
update the contact with a name that
already exists in the list, so what I'm
going to do is first demonstrate this
bug and after demonstrating it we're
going to continue on and fix the issue.
so let's run the application, I'm going
to run our main method. Alright, so what
we want to do is we want to have at
least two contacts, so I'm going to start
by entering your contact so we can add a
new contact rather John telephone number
one two three four five six seven eight
nine and we'll add another one, Jim this
time contact number nine eight seven six
five four three two one enter and we'll
one to print out the contacts to confirm
we've got two there. So you can see at
the moment we've got John and Jim both
listed. Now the main idea is to update
one of the contacts with the same name
that already exists to demonstrate that
bug, so what I'm going to do is choose
Jim for this, so we need to select
three to update an existing contact,
contact name, this is the existing
contact name, so we're gonna enter Jim into the
new contact name, we're going to enter
John and the new contact phone number
one two three four five six seven eight
nine and press Enter successfully
updated the record as you can see there
and now if we list the contents we'll
print them out. You can see now we've got
two John entries they're basically two
contacts with the same name and phone
number. Now we don't want to allow
contacts with the same name but do keep
in mind that real phone numbers do allow
our two placated contacts. Now the
problem is obviously in the update
contact method because of course that's
what we're calling to update, so that's
open the mobile phone class, they come
down to update content contact rather or
just close down the run window, so you
can see it a bit better, this is the code
here you can see on
screen, they're starting on line 29
this method firsts finds the position of
the existing contact in case it exists
and in the case where it does exist,
the contact is updated but before the
contact what we want to do is before
updating the contact, we want to check if
the contact or a contact with the same
name already exists. So all we really
have to do was add an else statement to
the if to check if the contact with that
particular name is already in existence.
so we come down here, if found
positions less than zero then we need to
add it else if parentheses
find contact parentheses again and it's
going to be new contact dot get name,
you can see opening and closing parentheses
there that's not equal to minus one,
closing parenthesis and the should have
been to another parenthesis there after
the dot get name function call, so that
closes that off, so if the phone contact
or we're passing a new contact name
returns always not equal to negative one
so it's a number other than negative one
that means we've already got a contact
with that name, so what we're going to do
is print out contact with name plus new
contact dot get name let's spend this on
to the next line plus double quotes
already exists update it's not successful
stop, then we'll close that line off.
Other thing we want to do at that point
is return false because of course the
update wasn't successful, then we don't
need to change any other code because of
course if the code falls through down to
here, it means the name wasn't found and
were able to go through and actually do
the update. Well, so let's go back now to
our main dot Java and what we might do
is also while I'm here, I'll look at the
menu they had a typo there in the menu,
so we come down here to the menu to
update existing an existing contact,
let's just fix that to update an
existing contact. Alright, so now that
we've put that change in let's test this
out, so let's run up ok so we're going to
start again we're going to do exactly
the same as we did last time, we do a
number two first add John telephone
number one two three four five six seven
eight nine and two again Jim nine eight
seven six five four three two one one
two print out our contacts and so you've
got two there to contact showing there
now. So we now need to perform an update
and of course that's number three in the
menu, we're going to update Jim so we
type in Jim enter your contact name,
try typing John and went to the contact
number one two three four five six seven
eight nine and you can see now we've got
some different output.
Contact with name John already exists
update was not successful error updating
record. So basically the console is
displaying a message that there was an
error updating the contact because John
already exists which is what we want,
so that's good but let's just do one other
thing, just to make sure that it is
working where we're choosing a different
name, so go through and do the update
again, you just in contact name is Jim
then we want a change Jim to another man
this time we'll go for the new contact
name Percy
one three five seven nine one two three
four. This time Jim was replaced with
Percy which is what we would expect
because the names are different, we can
do option one to print out a list and
we've now got John and Percy because of
course we've changed or updated Jim to
have a new name of a new phone number of
Percy and one three five seven nine one
two three four, so if you fix that bug
that allowed a duplicate to entry to be
basically updated to the same name and
we're done. So I'll see you in the next video.