Welcome to Practice House, in this post i will share with you an ArrayList Challenge for Java. the challenge is Create a program that implements a simple mobile phone Contacts Option . So we will learn step by step how to Create a program that implements a simple mobile phone Contacts Option Using ArrayList in Java.
Step 1: Create Contacts.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.practice.house; import java.util.ArrayList; class Contacts { private String name; private String phoneNumber; public Contacts(String name, String phoneNumber) { this.name = name; this.phoneNumber = phoneNumber; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } // Calling constructor in createContact Method public static Contacts createContact(String name, String phoneNumber) { return new Contacts(name, phoneNumber); } ArrayList<String> contactList = new ArrayList<String>(); public ArrayList<String> getContactList() { return contactList; } public void storeContacts(String names) { contactList.add(names); } public void printContactList() { System.out.println("You have " + contactList.size() + " Contacts in your list."); for(int i = 0; i < contactList.size(); i++) { System.out.println(i + " - " + contactList.get(i)); } } } |
Step 2: Create MobilePhone.java
All the method implementation we will do in this class. We will create method to add , update, remove and for query our contact.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.practice.house; import java.util.ArrayList; public class MobilePhone { private String myNumber; private ArrayList<Contacts> myContacts; public MobilePhone(String myNumber) { this.myNumber = myNumber; this.myContacts = new ArrayList<Contacts>(); } // Adding a new contact public boolean addNewContact(Contacts contact) { // Before adding a contact we are checking that its exist or not if(findContact(contact.getName()) >= 0) { System.out.println("Contact is already on file"); } //If Not exist then add our contact myContacts.add(contact); return true; } //Update Contact using set method public boolean updateContact(Contacts oldContact , Contacts newContact) { int foundPosition = findContact(oldContact); if(foundPosition < 0) { System.out.println(oldContact.getName() + " , was not found"); return false; } this.myContacts.set(foundPosition, newContact); System.out.println(oldContact.getName() + ", was replaced with " + newContact.getName()); return true; } // Remove Contact public boolean removeContact(Contacts contact) { int foundPosition = findContact(contact); if(foundPosition < 0) { System.out.println(contact.getName() + " , was not found"); return false; } this.myContacts.remove(foundPosition); System.out.println(contact.getName() + " was deleted"); return true; } // Find Contact Position private int findContact(Contacts contact) { return this.myContacts.indexOf(contact); } // Find Contact Names private int findContact(String contactName) { for(int i =0; i < myContacts.size(); i++) { // Creating another temporary object to hold the name and compare Contacts contacts = this.myContacts.get(i); if(contacts.getName().equals(contactName)) { return i; } } return -1; } // query contact public String queryContact(Contacts contact) { if(findContact(contact) >= 0) { return contact.getName(); } return null; } public Contacts queryContact(String name) { int position = findContact(name); if(position >=0) { return this.myContacts.get(position); } return null; } public void printContacts() { System.out.println("Contact list"); for(int i = 0; i < this.myContacts.size(); i++) { System.out.println((i+1) + " ." + this.myContacts.get(i).getName() + " -> " + this.myContacts.get(i).getPhoneNumber()); } } } |
Step 3: Create Main.java
This Main.java will be our main class. so we will use all those method that we already created in MobilePhone.java . We will run our program also from this class because our main class will have our Main Function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
package com.practice.house; import java.util.Scanner; public class Main { private static Scanner scanner = new Scanner(System.in); // Creating an instant with a default Number private static MobilePhone mobilePhone = new MobilePhone("000 443 876"); public static void main(String[] args) { boolean quite = false; startPhone(); printAction(); while(!quite) { System.out.println("\n Enter action: (6 to show available actions)"); int action = scanner.nextInt(); scanner.nextLine(); switch (action) { case 0: System.out.println("\n Shuting down .. "); quite = true; break; case 1: mobilePhone.printContacts(); break; case 2: addNewContact(); break; case 3: updateContact(); break; case 4: removeContact(); break; case 5: queryContact(); break; case 6: printAction(); break; } } } private static void addNewContact() { System.out.println("Enter new Contact Name: "); String name = scanner.nextLine(); System.out.println("Enter phone Number: "); String phone = scanner.nextLine(); Contacts newContact = Contacts.createContact(name, phone); if(mobilePhone.addNewContact(newContact)) { System.out.println("New Contact Added: name = "+ name + " , phone" + phone ); }else { System.out.println("Can't add, " + name +" already on file"); } } private static void updateContact() { System.out.println("Enter existing contact name : "); String name = scanner.nextLine(); Contacts extContact = mobilePhone.queryContact(name); if(extContact == null) { System.out.println("Cannot found contact"); return; } System.out.println("Enter new Conatct Name: "); String newName = scanner.nextLine(); System.out.println("Enter new Phone Number: "); String newNumber = scanner.nextLine(); Contacts newContact = Contacts.createContact(newName, newNumber); if(mobilePhone.updateContact(extContact, newContact)) { System.out.println("Successfully updated"); }else { System.out.println("Error Updating Contact"); } } private static void removeContact() { System.out.println("Enter existing contact name : "); String name = scanner.nextLine(); Contacts extContact = mobilePhone.queryContact(name); if(extContact == null) { System.out.println("Cannot found contact"); return; } if(mobilePhone.removeContact(extContact)) { System.out.println("Successfully deleted"); }else { System.out.println("Error Deleting Contact"); } } private static void queryContact() { System.out.println("Enter existing contact name : "); String name = scanner.nextLine(); Contacts extContact = mobilePhone.queryContact(name); if(extContact == null) { System.out.println("Cannot found contact"); return; } System.out.println("Name : " + extContact.getName() + " Phone Number is: " + extContact.getPhoneNumber()); } private static void startPhone() { System.out.println("Starting Phone . . ."); } private static void printAction() { System.out.println("\nAvailable actions:\npress"); System.out.println("0 - to shutDown\n" + "1 - to print contacts\n"+ "2 - to add a new contact\n"+ "3 - to update an existing contact\n"+ "4 - to remove and existing contact\n"+ "5 - query for existing contact\n"+ "6 - to print a list of available actions."); System.out.println("Choose Your Action: "); } } |
After Complete our Main.Java Let’s Run our application and see what’ s the output.
OUTPUT SAMPLE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
Starting Phone . . . Available actions: press 0 - to shutDown 1 - to print contacts 2 - to add a new contact 3 - to update an existing contact 4 - to remove and existing contact 5 - query for existing contact 6 - to print a list of available actions. Choose Your Action: Enter action: (6 to show available actions) 2 Enter new Contact Name: Alif Enter phone Number: 123456 New Contact Added: name = Alif , phone123456 Enter action: (6 to show available actions) 2 Enter new Contact Name: Teddy Enter phone Number: 56789 New Contact Added: name = Teddy , phone56789 Enter action: (6 to show available actions) 1 Contact list 1 .Alif -> 123456 2 .Teddy -> 56789 Enter action: (6 to show available actions) 3 Enter existing contact name : Alif Enter new Conatct Name: Alex Enter new Phone Number: 123456 Alif, was replaced with Alex Successfully updated Enter action: (6 to show available actions) 4 Enter existing contact name : Teddy Teddy was deleted Successfully deleted Enter action: (6 to show available actions) 5 Enter existing contact name : Teddy Cannot found contact Enter action: (6 to show available actions) 5 Enter existing contact name : Alif Cannot found contact Enter action: (6 to show available actions) 5 Enter existing contact name : Alex Name : Alex Phone Number is: 123456 Enter action: (6 to show available actions) 6 Available actions: press 0 - to shutDown 1 - to print contacts 2 - to add a new contact 3 - to update an existing contact 4 - to remove and existing contact 5 - query for existing contact 6 - to print a list of available actions. Choose Your Action: Enter action: (6 to show available actions) 0 Shutting down .. |
Thank you for reading this post and i hope you got your solution about how to Create a program that implements a simple mobile phone Contacts Option Using ArrayList in Java. To get more solved Program please keep visiting our website.
If you think this post is good for your friends also please share this post with them and ask to join Practice house community. Let’s practice more learn more together.
HAPPY CODING .. HAPPY LIVING

Md Arifur Rahman, Nickname is Alif.
I?m not that much of a online reader to be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your site to come back later on. Many thanks
After exploring a number of the articles on your web page, I truly appreciate your technique
of blogging.
Wow! Thank you! I continually wanted to write on my site something like that.
Can I implement a part of your post to my website?
Great ? I should certainly pronounce, impressed with your web site.
I had no trouble navigating through all tabs and related
info ended up being truly simple to do to access. Nice task.