ArrayList Challenge Part 3
A free video tutorial from Tim Buchalka
Java Python Android and C# Expert Developer - 1M+ students
4.5 instructor rating • 12 courses • 1,039,031 students
Final video of the ArrayList challenge.
Learn more from the full courseJava Programming Masterclass covering Java 11 & Java 17
Learn Java In This Course And Become a Computer Programmer. Obtain valuable Core Java Skills And Java Certification
80:09:25 of on-demand video • Updated August 2021
- Learn the core Java skills needed to apply for Java developer positions in just 14 hours.
- Be able to sit for and pass the Oracle Java Certificate exam if you choose.
- Be able to demonstrate your understanding of Java to future employers.
- Learn industry "best practices" in Java software development from a professional Java developer who has worked in the language for 18 years.
- Acquire essential java basics for transitioning to the Spring Framework, Java EE, Android development and more.
- Obtain proficiency in Java 8 and Java 11.
English So let's now continue on. We've got three methods we need to create, updateContact, removeContact, and queryContact so we can finish off our challenge. So the first one, update. We are gonna do a private static void. UpdateContact. So first thing's first, we need to get the name you wanna search for, enter existing contact name like so. And we're gonna put String name = scanner.nextLine. Like so, we're gonna retrieve the one we want. Now, the next part, what we could do, is we wanna use something like the mobile phone. .query and in this case you can see that we've got an option for query contact that accepts a contact, but in this case what we wanna also do, we wanna implement another version of query contact. We wanna pass just the name and to return the record, the entire record, the contact record for that name. And the reason for tha will become apparent. So what I wanna do is go back to mobile phone and looking at our query, we want to do something similar, we wanna do a public contact, query contact, string name. Like so. So we wanna return, we wanna have a look for any record that matches the name and actually then return the contact record for that because we're gonna need that in the next line. We can actually use the internal method we created the find contacts to do so. So we can do something like int position = findContact, and pass the name to that. Then we can put if(position >= 0), that means we've found an entry. And what we wanna do at that point is return. This.myContacts.get at position. So we want to return the contact, or object I should say, that's in that position in the array list. Failing that, if we weren't able to find it we're going to return null. So that's our method, so again we're just getting the position number based on the name to see whether it exists. If it does exist in the array list, we are gonna return the entry from that array list which is that contact record, otherwise we're going to return null. So going back to our main class again. So, what we can now do is, use the query. We can put .query and we can use the string one like name, and we can do Contact existingContactRecord, like so. And then what we can do is, we can put if existing contact records == null, we can say contact not found. And then we can return, just return cuz there's no return type. So basically if we don't find the entry we can't update it. So we are going to return null. But the good thing about is now that we've done that is we can now easily proceed because what we can do now is just we can actually send that contact record ultimately when we're going to update the record. But the first thing we need to do because we're replacing the existing contact name with a new one, we need to actually accept that as input. So we're going to actually print, enter new contact name, like so. And string, new name = scanner.next line. Accept the new contact name that we're going to assign and this could be in the case when someone perhaps got married and their name changed. And likewise mobile phone number may be the same but may not be, enter new contact phone number, and string new number equals scanner.nextLine like so. So we've got the new number and the new name so we're going to create a new record for that so Contact newContact equals we want to use the Contact static method and pass the new name and the new number. Like so. So we've now got the two entries. We've got the existing record as a contact object and also the new contact as an object so, we can just call our update contact by doing mobilephone.updatecontact and we pass the old one first, old existing contact record and new contact. And then what we really could is put if, surround that in an if, so because that's returning a true or false we can put. Successfully updated record. Otherwise we do an else error updating record. Now we shouldn't actually get an error with the update because we've already done a test beforehand to see whether it existed, but in a multi-user type world for example the record could have been deleted since the time that we retrieved, determined whether it was on file or not so it would be good to actually still test for that anyway. In this case, we've now implemented the update contact. So thinking about it, the remove contact is actually gonna be very similar. We wanna do the same thing, we wanna just check to see whether it exists. If it does exist then try to delete it. So I'm gonna copy some of this. I'm gonna copy this part and paste that. Like so. But we're gonna call it remove contact. And we're going to add another bracket there, so at this point the code prior to that, we're actually testing to see whether it exists or not so we've got to here, we know that it does exist, so we've got the existing record so we all we really need to do is just call the method so we can put mobile phone dot remove and it's asking for the parameter which is of course the existing contact record. And again we can wrap that in an if. So if, successfully deleted, or else, error deleting record contact to be more precise. So that's our removeContact. And the last thing we wanna do now is just do a query, and the queryContact code again is pretty is pretty similar. So we could do something similar, I'm just gonna copy that code. Gonna be queryContact. QueryContact enter existing name, and contact not found. We wouldn't really need to do it because we've already queried it once. We could actually just print out a message where's the effect of name, plus the existing contact record .getName, phone number is existingRecord.getPhoneNumber and that will sort of confirmed that we're able to actually retrieve the number because obviously we already asked for the name and then from the name we're able to determine obviously the phone number because the information that was returned to us also returned that in a string. So I think that's all our functions now. So I think we're ready now to actually try running this code and seeing whether shock, horror, it actually works so let's try that now. Okay, so there's our function. So let's make a start on this. So firstly, 1, print contacts. Shouldn't be any, which is correct. So let's add a contact. Contact, we'll say it's Tim. Phone number 12345, pretty original phone number there. New contact added, name equals Tim, phone equals 12345, and if we print the contacts, we've now got a contact list with me on there. Just one record. Let's add another one. Bob, 23890, and just print that list out again. That seems to be working okay, add one more shall we? Okay and print the list again. It's definitely working and option six so that we can see our list of options again. All right, so let's go ahead and do a query, and we'll go option five, we'll try the queries out. So let's try one that we know is on file Tim. Name Tim phone number equals 12345, so that's good that it found the number. And, we'll just do one more, so number five for Bob. 23890, which is correct for Bob, you can see that on the screen there. Let's do one that's not on file. Roy contact not found, which is correct. That's good. So let's do an update. So do an update, firstly we'll enter something that doesn't exist. Derek. Contact not found, which is good. And we'll try option three again. This time we'll try Tim. This time it's actually come up and said, what's the new name? So it's found it, so we'll put Tom. And we'll put 8900000. Tim was replaced with Tom, successfully updated. And the list of options again, and we'll just print the contacts out. You can see element one has been updated to Tom, 8900000. And remove is the other thing we need to check, so let's do that. Let's try and remove, say, Bob. Bob was deleted but lets confirm that was the case. Tom and Mark remain, so yes it has been deleted. So I think that's all the functions and it seems to be working nicely. So that's my solution. I hope you've got a lot out of this and that your solution was perhaps similar. I know that I changed a couple of things I didn't talk about specifically. But, now if you've done something very similar and you've got the results that were similar to this, you've actually done very well. No matter what, hopefully you've got a lot out of this, and important is just to start thinking about when you're implementing some of this code. So I'll just close this down, it's just to decide where that code was. I think it was important when we talked about getting back to the mobile phone when we talked about the print, print contacts whether that code effectively could have been in the mobile phone, or whether it should have been there or in here, I think we made the right decision to actually put that code in the mobile phone because it was in the mobile phone class. Because it was related to the mobile phone. So that's it. We're finally done with array list. I know there were a lot of videos that we did for that but it was very important to cover a lot of that because we're gonna be going into different types of lists and we're gonna assume at this point as we move forward that you understand the basics of lists. We'll just be talking about the specifics and the different types of lists that are in Java. So I'll see you in that next video.