Advanced Program
package Mobile_phone_Contacts_Application; /* >>> Mobile phone Contacts Application You are an IT Support Administrator Specialist at SAU and are charged with the task of creating a program that implements a simple mobile phone contacts management with the following capabilities: • Add new contact • Modify contact • Search contact • Remove contact • Print all contact list information */ import java.io.*; import java.util.*; public class MobilePhone { private ArrayList<Contact> myContacts; private File f; private Scanner userInput = new Scanner(System.in); private Scanner fileInput; private PrintWriter pw; public MobilePhone() { super(); f = new File("Contacts.txt"); this.myContacts = new ArrayList<Contact>(); readFromFile(); } public void addNewContact() { String name, phoneNumber; boolean contact = true; System.out.println("Enter new contact name:"); name = userInput.nextLine(); for (int i = 0; i < myContacts.size(); i++) { if (myContacts.get(i).getName().equals(name)) { contact = false; System.out.println("Contact is already on file."); System.out.println("Cannot add, " + name + " already on file"); break; } } if (contact) { System.out.println("Enter phone number:"); phoneNumber = userInput.nextLine(); Contact newContact = new Contact(name, phoneNumber); myContacts.add(newContact); System.out.println("New contact added:"); System.out.println("Name = " + name + " , phone = " + phoneNumber); addToFile(); } } public void updateContact() { String phoneNumber; String newName; Contact newContact = new Contact(); boolean updateContact = true; System.out.println("Enter existing contact name:"); String oldName = userInput.nextLine(); for (int i = 0; i < myContacts.size(); i++) { if (myContacts.get(i).getName().equals(oldName)) { myContacts.remove(i); updateContact = false; System.out.println("Enter new contact name:"); newName = userInput.nextLine(); System.out.println("Enter new contact phone number:"); phoneNumber = userInput.nextLine(); newContact = new Contact(newName, phoneNumber); myContacts.add(i, newContact); System.out.println(oldName + " was replaced with " + newName); System.out.println("Successfully updated record."); addToFile(); break; } } if (updateContact) { System.out.println("Contact not found."); } } public void removeContact() { boolean removeContact = true; String oldName; System.out.println("Enter existing contact name:"); oldName = userInput.nextLine(); for (int i = 0; i < myContacts.size(); i++) { if (myContacts.get(i).getName().equals(oldName)) { myContacts.remove(i); System.out.println(oldName + " was deleted."); System.out.println("Successfully deleted."); removeContact = false; addToFile(); break; } } if (removeContact) { System.out.println("This name is not in the system"); } } public void searchContact() { String name; boolean searchContact = true; System.out.println("Enter existing contact name:"); name = userInput.nextLine(); for (int i = 0; i < myContacts.size(); i++) { if (myContacts.get(i).getName().equals(name)) { System.out.println("Name: " + name + ", phone number: " + myContacts.get(i).getPhoneNumber()); searchContact = false; break; } } if (searchContact) { System.out.println("Contact is not found in the list."); } } public void printContacts() { System.out.println("Contact List"); for (int i = 0; i < myContacts.size(); i++) { System.out.println((i + 1) + ". " + myContacts.get(i).getName() + " -> " + myContacts.get(i).getPhoneNumber()); } } private void readFromFile() { try { fileInput = new Scanner(f); while (fileInput.hasNext()) { Contact newContact = new Contact(fileInput.nextLine(), fileInput.nextLine()); myContacts.add(newContact); } fileInput.close(); } catch (FileNotFoundException ex) { } } private void addToFile() { try { pw = new PrintWriter(f); for (int i = 0; i < myContacts.size(); i++) { pw.println(myContacts.get(i).getName()); pw.println(myContacts.get(i).getPhoneNumber()); } pw.close(); } catch (IOException ex) { } } }