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

JAVA: Create a class Contact to store the contact information. The Contact class

ID: 3831841 • Letter: J

Question

JAVA: Create a class Contact to store the contact information. The Contact class should store the contact’ first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Now write a program that uses an ArrayList of parameter type Contact to store a database of contacts. Your program should present a menu that allows the user to 1) add a contact to the list, 2) remove a contact from the list, 3) save all contacts to a file, 4) load all the contacts from a file, 5) display all contacts, 6) search for a specific contact and display it. 7) Exit from the program.

Contact Menu:

1)Add

2)Remove

3)Save

4)Load

5)Show All

6)Search

7)Exit

The searches should find any contact where any instance variable contains a target search string. For example, if “abc” is the search target, then any contact where the first name, last name, phone number, or email address contains “abc” should be shown on the display. Your program should keep running while performing these operations and only quit if the user selects Exit from the menu. In that case, you should save all the contacts from the list to the file (before you quit from the prorgram). When you restart your program you should retrieve all the contacts from the file. In case, you first time start your program (file does not exists!) you should create an empty file (contacts.txt). Note: You can use either text file or binary file.

Explanation / Answer

import java.util.*;
import java.io.*;
public class Contact implements java.io.Serializable { // implement Serializable for File Read and Write
private String fname;
private String lname;
private String phone;
private String email_addr;
public String getFname() // Input First Name
{
Scanner sc=new Scanner(System.in); // Scanning Input Console
System.out.println("Enter First Name");
this.fname=sc.nextLine();
return this.fname;
}
public String getLname() // Input Last Name
{
Scanner sc=new Scanner(System.in);// Scanning Input Console
System.out.println("Enter Last Name");
this.lname=sc.nextLine();
return this.lname;
}
public String getPhone() // Input phone no
{
Scanner sc=new Scanner(System.in);// Scanning Input Console
System.out.println("Enter Phone No");
this.phone=sc.nextLine();
return this.phone;
}
public String getEmail() // Input Email Address
{
Scanner sc=new Scanner(System.in);// Scanning Input Console
System.out.println("Enter Email Address");
this.email_addr=sc.nextLine();
return this.email_addr;
}
public void saveContact(String fileName,ArrayList<Contact> list)
{
ObjectOutputStream oos=null;
FileOutputStream fos=null;
try
{
fos=new FileOutputStream(fileName); // FileOutputStream object for the filename defined in main
oos=new ObjectOutputStream(fos);
oos.writeObject(list); // Write List Data to File
oos.close();
}
catch(Exception e)
{
e.printStackTrace();
}

}
public ArrayList loadContact(String fileName)
{
ObjectInputStream ois=null;
FileInputStream fis=null;
ArrayList<Contact> list_new = new ArrayList<Contact>();
try
{
fis = new FileInputStream(fileName); // FileInputStream object for the filename defined in main
ois = new ObjectInputStream(fis);
list_new = (ArrayList<Contact>)ois.readObject(); // Read List Data to File
ois.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return list_new; // Return List
}
public void addContact(ArrayList<Contact> l1) // add new contact to List
{
fname=this.getFname();
lname=this.getLname();
phone=this.getPhone();
email_addr=this.getEmail();
l1.add(this);
}
public static void main(String args[]) // Main Function
{
   ArrayList<Contact> list1 = new ArrayList<Contact>();
   int choice=0; // Choice for the menu
   do
   {
       Scanner sc1=new Scanner(System.in);
       System.out.println("-----------------Contact Menu--------------:");
       System.out.println("1)Add");
System.out.println("2)Remove");
System.out.println("3)Save");
System.out.println("4)Load");
System.out.println("5)Show All");
System.out.println("6)Search");
System.out.println("7)Exit");
       choice=sc1.nextInt(); // get choice input
       if(choice==1) // Add Contact
       {
Contact c=new Contact();
c.addContact(list1);
}
if(choice==3) // Save Contact List
       {
Contact c=new Contact();
c.saveContact("contacts.txt",list1);
System.out.println("-----------------Successfully Saved Data--------------:");
       }
if(choice==4) // Load Contact List
       {
Contact c=new Contact();
list1=c.loadContact("contacts.txt");
System.out.println("-----------------Data Loaded Successfully--------------:");
       }
if(choice==5) //Display All Contact Information
       {
System.out.println("------------------------------List of Contact------------------------------");
for(int i=0;i<list1.size();i++)
           {
System.out.println(""+(i+1)+" "+((Contact)list1.get(i)).lname+" "+((Contact)list1.get(i)).fname+" "+((Contact)list1.get(i)).phone+" "+((Contact)list1.get(i)).email_addr);
          }
           System.out.println("---------------------------------------------------------------------------");
       }
       if(choice==6) // Search Contact
       {
String dlt_name;
Scanner sc2=new Scanner(System.in);
System.out.println("Enter String to search");
dlt_name=sc2.nextLine(); // Take Input String for Search
for(int i=0;i<list1.size();i++)
{
Contact c2=new Contact();
c2=(Contact)list1.get(i);
// Convert all data of List into lowercase for the matching
if(c2.fname.toLowerCase().contains(dlt_name.toLowerCase()) ||c2.lname.toLowerCase().contains(dlt_name.toLowerCase()) || c2.phone.toLowerCase().contains(dlt_name.toLowerCase())|| c2.email_addr.toLowerCase().contains(dlt_name.toLowerCase()) )
{
System.out.println(c2.lname+" "+c2.fname+" "+c2.phone+" "+c2.email_addr);
}
}
}
if(choice==2) // remove Contact
       {
String dlt_name;
int dlt_sn=-1;
Scanner sc2=new Scanner(System.in);
System.out.println("Enter Contact Info to delete(First Name or Last Name or Contact No or Email Address)");
dlt_name=sc2.nextLine();// Take Input String for Search
int flag=0;
for(int i=0;i<list1.size();i++)
{
Contact c2=new Contact();
c2=(Contact)list1.get(i);
// Convert all data of List into lowercase for the matching
if(c2.fname.toLowerCase().contains(dlt_name.toLowerCase()) ||c2.lname.toLowerCase().contains(dlt_name.toLowerCase()) || c2.phone.toLowerCase().contains(dlt_name.toLowerCase())|| c2.email_addr.toLowerCase().contains(dlt_name.toLowerCase()) )
{
System.out.println(""+(i+1)+" "+c2.lname+" "+c2.fname+" "+c2.phone+" "+c2.email_addr);
flag=1;
}
}
if(flag==1) // if any record found
{
System.out.println("Type Serial No of The Record To Delete");
dlt_sn=sc2.nextInt(); // take serial no input for delete
if(dlt_sn>=1 && dlt_sn<=list1.size())
{
list1.remove(dlt_sn-1); // remove contact from the list of provided serial no
}
else
{
System.out.println("Wrong Index");
}
}
else
{
System.out.println("No Record Found");
}
}
       if(choice==7)
       {
           break;
       }
   }while(true);
}
}

-------------------------------------------Output-------------------------------------------------------

run:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
1
Enter First Name
ABC
Enter Last Name
DEF
Enter Phone No
GHI
Enter Email Address
JKL
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
1
Enter First Name
FGH
Enter Last Name
IJK
Enter Phone No
LMN
Enter Email Address
OPQ
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
1
Enter First Name
LMN
Enter Last Name
OPQ
Enter Phone No
RST
Enter Email Address
UVW
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
1
Enter First Name
UVW
Enter Last Name
XYZ
Enter Phone No
123
Enter Email Address
456
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
5
------------------------------List of Contact------------------------------
1       DEF           ABC           GHI           JKL
2       IJK           FGH           LMN           OPQ
3       OPQ           LMN           RST           UVW
4       XYZ           UVW           123           456
---------------------------------------------------------------------------
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
3
-----------------Successfully Saved Data--------------:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
7
BUILD SUCCESSFUL (total time: 1 minute 31 seconds)

run:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
4
-----------------Data Loaded Successfully--------------:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
5
------------------------------List of Contact------------------------------
1       DEF           ABC           GHI           JKL
2       IJK           FGH           LMN           OPQ
3       OPQ           LMN           RST           UVW
4       XYZ           UVW           123           456
---------------------------------------------------------------------------
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
6
Enter String to search
A
DEF           ABC           GHI           JKL
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
6
Enter String to search
G
DEF           ABC           GHI           JKL
IJK           FGH           LMN           OPQ
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
7
BUILD SUCCESSFUL (total time: 44 seconds)

run:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
4
-----------------Data Loaded Successfully--------------:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
5
------------------------------List of Contact------------------------------
1       DEF           ABC           GHI           JKL
2       IJK           FGH           LMN           OPQ
3       OPQ           LMN           RST           UVW
4       XYZ           UVW           123           456
---------------------------------------------------------------------------
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
2
Enter Contact Info to delete(First Name or Last Name or Contact No or Email Address)
O
2       IJK           FGH           LMN           OPQ
3       OPQ           LMN           RST           UVW
Type Serial No of The Record To Delete
3
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
5
------------------------------List of Contact------------------------------
1       DEF           ABC           GHI           JKL
2       IJK           FGH           LMN           OPQ
3       XYZ           UVW           123           456
---------------------------------------------------------------------------
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
3
-----------------Successfully Saved Data--------------:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit

7
BUILD SUCCESSFUL (total time: 54 seconds)

run:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
4
-----------------Data Loaded Successfully--------------:
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
5
------------------------------List of Contact------------------------------
1       DEF           ABC           GHI           JKL
2       IJK           FGH           LMN           OPQ
3       XYZ           UVW           123           456
---------------------------------------------------------------------------
-----------------Contact Menu--------------:
1)Add
2)Remove
3)Save
4)Load
5)Show All
6)Search
7)Exit
7
BUILD SUCCESSFUL (total time: 12 seconds)