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

This assignment is on ArrayLists. You are to design a class called AddressBook t

ID: 3640245 • Letter: T

Question

This assignment is on ArrayLists. You are to design a class called AddressBook that uses an ArrayList to store a database of contacts. Start by designing the Contact class that holds a person’s last name, first name, and phone number:

public class Contact
{
private String lname;
private String fname;
private String phone;
//complete the class by adding appropriate constructors, get, set methods, etc.
}

Next design the AddressBook class that uses an ArrayList of parameter type Contact:

public class AddressBook
{
private ArrayList<Contact> list;

//rest of the code
}

The AddressBook class should have methods to add a contact, display all contacts, search for a specific contact and display it, or search and delete a specific contact. The searches should find any contact where any member variable contains a target search. For example, if “elmore” is the search target, then any contact where elmore is the first name or last name is elmore should be displayed. Similarly, if “555-1234” is the search target, all contacts having this number should be displayed.

Demo the AddressBook class:
public class AddressBookDemo
{
public static void main(String[] args)
{
//rest of the code
}
}

The demo program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and delete it.

A sample screen dialog is given below:
Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 1
Enter last name: Adams
Enter first name: John
Enter phone number: 555-1234

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 1
Enter last name: Smith
Enter first name: Amy
Enter phone number: 555-4567

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 1
Enter last name: Brown
Enter first name: Adam
Enter phone number: 555-9812

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 1
Enter last name: Adams
Enter first name: Quincy
Enter phone number: 555-5512

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 2
Adams, John 555-1234
Smith, Amy 555-4567
Brown, Adam 555-9812
Adams, Quincy 555-5512

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 3
Search what: Adams
Adams, John 555-1234
Adams, Quincy 555-5512

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 4
Delete what: James
James not found

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 4
Delete what: Adams
Adams, John 555-1234 deleted
Adams, Quincy 555-5512 deleted

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 2
Smith, Amy 555-4567
Brown, Adam 555-9812

Enter 1 to add contact, 2 to display, 3 to search, 4 to delete, 5 to quit: 5
Bye!




public

class AddressBook
{
private ArrayList<Contact> list;


public AddressBook()
{
list=new ArrayList<Contact>();
}
public addContact(String l,String f,String p)
{
Contact c= new Contact(l,f,p);
list.add(c);
}
public removeContact(String searchStr)
{
for(int i=0; i<=list.size(); i++)
{
// take the contact located at i in the "list"
// see if it matches the search string
if(c.equals(searchStr)


System.out.print(list.remove(i));

//if contact matches
//remove it from the list and print it
}
}

public searchContact(String searchStr)
{
for(int i=0; i<=list.size(); i++)
{
if(c==i)
System.out.print(list.contains(c));
return true;

}

public displayContact(String



}
}


this is what i have done, and i want you to complete address class and make the address demo class

public

class Contact
{
private String lname;
private String fname;
private String phone;

}
public class Contact(String l,String f,String p)
{
lname=l;
fname=f;
phone=p;
}
public void setLname(String l)
{
lname=l;
}
public void setFname(String f)
{
fname=f;
}
public void setPhone(String p)
{

phone=p;
}
public String getLname()
{
return lname;
}
public String getFname()
{
return fname;

}
public String getphone()
{
return phone;

}
public String toString()
{
System.out.println(lanme+","+fname+" "+phone);

}

Explanation / Answer




import java.util.ArrayList;

public class AddressBookDemo {

    AddressBook address;

    public AddressBookDemo() {
        address = new AddressBook();
        userInput(new java.util.Scanner(System.in));
    }

    public static void menu() {
        System.out.print(" Contact Menu : [");
        System.out.print("1. Add, ");
        System.out.print("2. Display ");
        System.out.print("3. Search, ");
        System.out.print("4. Delete, ");
        System.out.print("5. Quit] ");
        System.out.print("Enter option : ");
    }

    public void userInput(java.util.Scanner sc) {
        menu();
        String input = "";
        while (!(input = sc.nextLine()).equals("5")) {
            if (input.equals("1")) {
                System.out.print("Enter last name    : ");
                String lname = sc.nextLine();                
                System.out.print("Enter first name   : ");
                String fname = sc.nextLine();
                System.out.print("Enter phone number : ");
                String phone = sc.nextLine();
                address.addContact(lname, fname, phone);
            } else if(input.equals("2")) {
                address.displayContacts();
            } else if(input.equals("3")) {
                System.out.print("Search what : ");
                address.searchContact(sc.nextLine());
            } else if(input.equals("4")) {
                System.out.print("Delete what : ");
                address.removeContact(sc.nextLine());
            }
            menu();
        }
        System.out.println("bye...");
    }

    public static void main(String[] args) {

        AddressBookDemo demo = new AddressBookDemo();
    }
}

class AddressBook {

    private ArrayList<Contact> list;

    public AddressBook() {
        list = new ArrayList<Contact>();
    }

    public void addContact(String l, String f, String p) {
        Contact c = new Contact(l, f, p);
        list.add(c);
    }

    public void displayContacts() {
        System.out.println("AddressBook Contacts : ");
        System.out.println("=================================================");
        java.util.Iterator it = list.iterator();
        while (it.hasNext()) {
            Contact c = (Contact) it.next();
            System.out.println(c.toString());
        }
    }

    public void removeContact(String searchStr) {        
        java.util.Iterator it = list.iterator();
        while (it.hasNext()) {
            Contact c = (Contact) it.next();
            if (c.getLname().equals(searchStr)) {
                System.out.println(c.toString() + " deleted.");
            }
        }
    }

    public void searchContact(String searchStr) {
        boolean found = false;        
        for (int i = 0; i < list.size(); i++) {
            Contact c = (Contact) list.get(i);
            if (c.getLname().equals(searchStr)) {
                System.out.println(c.toString());
                found=true;
            }
        }
        if (!found) {
            System.out.println(searchStr + " not found.");
        }        
    }
}

public class Contact {

    private String lname;
    private String fname;
    private String phone;

    public Contact(String l, String f, String p) {
        lname = l;
        fname = f;
        phone = p;
    }

    public void setLname(String l) {
        lname = l;
    }

    public void setFname(String f) {
        fname = f;
    }

    public void setPhone(String p) {

        phone = p;
    }

    public String getLname() {
        return lname;
    }

    public String getFname() {
        return fname;

    }

    public String getphone() {
        return phone;

    }

    public String toString() {
        return (lname + "," + fname + " " + phone);
    }    
    
}


import java.util.ArrayList;

public class AddressBookDemo {

    AddressBook address;

    public AddressBookDemo() {
        address = new AddressBook();
        userInput(new java.util.Scanner(System.in));
    }

    public static void menu() {
        System.out.print(" Contact Menu : [");
        System.out.print("1. Add, ");
        System.out.print("2. Display ");
        System.out.print("3. Search, ");
        System.out.print("4. Delete, ");
        System.out.print("5. Quit] ");
        System.out.print("Enter option : ");
    }

    public void userInput(java.util.Scanner sc) {
        menu();
        String input = "";
        while (!(input = sc.nextLine()).equals("5")) {
            if (input.equals("1")) {
                System.out.print("Enter last name    : ");
                String lname = sc.nextLine();                
                System.out.print("Enter first name   : ");
                String fname = sc.nextLine();
                System.out.print("Enter phone number : ");
                String phone = sc.nextLine();
                address.addContact(lname, fname, phone);
            } else if(input.equals("2")) {
                address.displayContacts();
            } else if(input.equals("3")) {
                System.out.print("Search what : ");
                address.searchContact(sc.nextLine());
            } else if(input.equals("4")) {
                System.out.print("Delete what : ");
                address.removeContact(sc.nextLine());
            }
            menu();
        }
        System.out.println("bye...");
    }

    public static void main(String[] args) {

        AddressBookDemo demo = new AddressBookDemo();
    }
}

class AddressBook {

    private ArrayList<Contact> list;

    public AddressBook() {
        list = new ArrayList<Contact>();
    }

    public void addContact(String l, String f, String p) {
        Contact c = new Contact(l, f, p);
        list.add(c);
    }

    public void displayContacts() {
        System.out.println("AddressBook Contacts : ");
        System.out.println("=================================================");
        java.util.Iterator it = list.iterator();
        while (it.hasNext()) {
            Contact c = (Contact) it.next();
            System.out.println(c.toString());
        }
    }

    public void removeContact(String searchStr) {        
        java.util.Iterator it = list.iterator();
        while (it.hasNext()) {
            Contact c = (Contact) it.next();
            if (c.getLname().equals(searchStr)) {
                System.out.println(c.toString() + " deleted.");
            }
        }
    }

    public void searchContact(String searchStr) {
        boolean found = false;        
        for (int i = 0; i < list.size(); i++) {
            Contact c = (Contact) list.get(i);
            if (c.getLname().equals(searchStr)) {
                System.out.println(c.toString());
                found=true;
            }
        }
        if (!found) {
            System.out.println(searchStr + " not found.");
        }        
    }
}

public class Contact {

    private String lname;
    private String fname;
    private String phone;

    public Contact(String l, String f, String p) {
        lname = l;
        fname = f;
        phone = p;
    }

    public void setLname(String l) {
        lname = l;
    }

    public void setFname(String f) {
        fname = f;
    }

    public void setPhone(String p) {

        phone = p;
    }

    public String getLname() {
        return lname;
    }

    public String getFname() {
        return fname;

    }

    public String getphone() {
        return phone;

    }

    public String toString() {
        return (lname + "," + fname + " " + phone);
    }    
    
}