Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Contact that has a firstName, lastName, phone and email. A

ID: 3687897 • Letter: C

Question

Create a class called Contact that has a firstName, lastName, phone and email. Also create a PhoneBook class that will have an arrayList of contact. The PhoneBook class will have the following methods:

•addContact that takes a Contact object and adds is to the arrayList

•deleteContact that takes a Contact object and deletes it from the arrayList

•displayContacts that displays all contacts in the arrayList

•getSize that returns the number of contacts in the arrayList

•main method to run and test your program.

Explanation / Answer

Hello there,

Kindly find below code and its o/p.

You can take contact info from User as well using Scanner in java.

I have created contacts with hardcoded information in the main program .Also note that there is no validations on email or phone number . Let me know if you hav any queries.

===

/**
* It represents a contact class containing basic information.
* @author dipal.prajapati
*
*/
public class Contact {
String firstName;
String lastName;
String phoneNumber;
String email;
public Contact(String firstName, String lastName, String phoneNumber, String email) {
   super();
   this.firstName = firstName;
   this.lastName = lastName;
   this.phoneNumber = phoneNumber;
   this.email = email;
}
@Override
public String toString() {
   return "Contact [firstName=" + firstName + ", lastName=" + lastName + ", phoneNumber=" + phoneNumber + ", email=" + email + "]";
}

}

====

import java.util.ArrayList;
import java.util.List;

/**
* This is a PhoneBook class which stores all the contact in a list and provides
* functionalities for adding,deleting and displaying the contacts in it.
*
* @author dipal.prajapati
*
*/
public class PhoneBook {
   static List<Contact> listOfContacts = new ArrayList<Contact>();

   /**
   * Main method to test the functionalities.
   * @param args
   */
   public static void main(String args[]) {
       Contact contact1 = new Contact("Dipal", "Prajapati", "0987983292", "abc@xyz.com");
       Contact contact2 = new Contact("John", "Macculam", "1-800-633-3479", "pqr@xyz.com");
       Contact contact3 = new Contact("Alex", "Harde", "0987983292", "asds@xyz.com");
       Contact contact4 = new Contact("Roy", "Zee", "1-800-952-7900", "rtu@xyz.com");
       PhoneBook.addContact(contact1);
       PhoneBook.addContact(contact2);
       PhoneBook.addContact(contact3);
       PhoneBook.addContact(contact4);

       PhoneBook.displayContacts();
       System.out.println("Size of the PhoneBook(number of contacts) is:" + PhoneBook.getSize()+" ");
      
       PhoneBook.deleteContact(contact3);
      
       PhoneBook.displayContacts();
       System.out.println("Size of the PhoneBook(number of contacts) is:" + PhoneBook.getSize()+" ");
   }

   /**
   * It adds a contact in the phonebook.
   *
   * @param contact
   */
   public static void addContact(Contact contact) {
       listOfContacts.add(contact);
       System.out.println(contact.toString() + " has been added. ");
   }

   /**
   * It deletes the contact in the phonebook.
   *
   * @param contact
   */
   public static void deleteContact(Contact contact) {
       listOfContacts.remove(contact);
       System.out.println(contact.toString() + " has been deleted. ");
   }

   /**
   * It displays all the contacts of the phonebook.
   */
   public static void displayContacts() {
       System.out.println("Printing Contacts");
       for (int i = 0; i < listOfContacts.size(); i++) {
           System.out.println(listOfContacts.get(i));
       }
       System.out.println(" ");
   }

   /**
   * It returns the number of contacts of the phonebook.
   */
   public static int getSize() {
       return listOfContacts.size();
   }
}

===O/P===

Contact [firstName=Dipal, lastName=Prajapati, phoneNumber=0987983292, email=abc@xyz.com] has been added.

Contact [firstName=John, lastName=Macculam, phoneNumber=1-800-633-3479, email=pqr@xyz.com] has been added.

Contact [firstName=Alex, lastName=Harde, phoneNumber=0987983292, email=asds@xyz.com] has been added.

Contact [firstName=Roy, lastName=Zee, phoneNumber=1-800-952-7900, email=rtu@xyz.com] has been added.

Printing Contacts
Contact [firstName=Dipal, lastName=Prajapati, phoneNumber=0987983292, email=abc@xyz.com]
Contact [firstName=John, lastName=Macculam, phoneNumber=1-800-633-3479, email=pqr@xyz.com]
Contact [firstName=Alex, lastName=Harde, phoneNumber=0987983292, email=asds@xyz.com]
Contact [firstName=Roy, lastName=Zee, phoneNumber=1-800-952-7900, email=rtu@xyz.com]


Size of the PhoneBook(number of contacts) is:4

Contact [firstName=Alex, lastName=Harde, phoneNumber=0987983292, email=asds@xyz.com] has been deleted.

Printing Contacts
Contact [firstName=Dipal, lastName=Prajapati, phoneNumber=0987983292, email=abc@xyz.com]
Contact [firstName=John, lastName=Macculam, phoneNumber=1-800-633-3479, email=pqr@xyz.com]
Contact [firstName=Roy, lastName=Zee, phoneNumber=1-800-952-7900, email=rtu@xyz.com]


Size of the PhoneBook(number of contacts) is:3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote