Question

Java need help with searching for a contact Question:Write a program that uses an ArrayList of...

Java need help with searching for a contact

Question:Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact’s first and last
name, phone number, and email address. Add appropriate accessor and mutator methods. Your database 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 give the user the option to delete it. The searches should find any contact where any instance variable contains a target search string. For example, if “elmore” is the search target, then any contact where the first name, last name, phone number, or email address contains “elmore” should be returned for display or deletion. Use the “for-each” loop to iterate through the ArrayList.

Contact.java

import java.util.Scanner;

public class Contact
{
   private String fname;
   private String lname;
   private String phonenum;
   private String email;

   public Contact()
   {
       fname=" ";
       lname=" ";
       phonenum=" ";
       email=" ";
   }

   public Contact(String fn,String ln,String p,String e)
   {
       this.fname=fn;
       this.lname=ln;
       this.phonenum=p;
       this.email=e;
   }

   public void setfName(String fn)
   {
       this.fname=fn;
   }

   public void setlName(String ln)
   {
       this.lname=ln;
   }

   public void setNumber(String p)
   {
       this.phonenum=p;
   }

   public void setEmail(String e)
   {
       this.email=e;
   }

   public String getfName()
   {
       return lname;
   }

   public String getlName()
   {
       return lname;
   }

   public String getNumber()
   {
       return phonenum;
   }

   public String getEmail()
   {
       return email;
   }

   public String toString()
   {
       return "Name: "+fname+" "+lname+"\nPhone Number: "+phonenum+"\nEmail: "+email;
   }
}

ContactList.java

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

public class ContactList
{
private List<Contact> info=null;

public ContactList()
{
info = new ArrayList<Contact>();
}
public int addContact(Contact contact)
{
Contact n=findName(contact.getfName()+contact.getlName()+contact.getNumber()+contact.getEmail());

if(n==null)
{
info.add(contact);
return 1;
}
return -1;
}
public Contact findName(String s)
{
for (Contact c : info)
{
if (c.getfName().equalsIgnoreCase(s)&c.getfName().equalsIgnoreCase(s))
{
return c;
}
}   
return null;
}
}

ContactListDemo.java

import java.util.Scanner;

public class ContactListDemo
{
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);

       ContactList book = new ContactList();

       Contact c1=new Contact();

       c1.setfName("Andy");
       c1.setlName("Williams");
       c1.setNumber("585-279-0095");
       c1.setEmail("aWilliams@aol.com");

       book.addContact(c1);

       Contact c2=new Contact("Dean","Martin","616-223-3483","dean.martin@yahoo.com");

       book.addContact(c2);
  
       String choice = "";

       while (!choice.equalsIgnoreCase("done"))
       {
           System.out.println("\nWould you like to add or search for a contact from the phone book?\nIf you would like find a contact type \"search\" or if you would like to add a contact type \"add\" and if you are finished type \"done\" to end.");
           System.out.println("");

           choice = input.nextLine();

           if (choice.equalsIgnoreCase("search"))
           {
               System.out.print("Enter the the first or last name: ");
               String search = input.nextLine();

               Contact result = book.findName(search);

               if (result != null)
               {
                   System.out.println("\nThe name was found.\nHere is the contact information:");
                   System.out.println(result);
               }

               else
               {
               System.out.println("\nThe name couldn't be found.");
               }
           }
           if (choice.equalsIgnoreCase("add"))
           {
               ContactList bookNew = new ContactList();

               System.out.print("Enter the first name: ");
               String fname = input.nextLine();

               System.out.print("Enter the last name: ");
               String lname = input.nextLine();

               System.out.print("Enter the phone number: ");
               String num = input.nextLine();

               System.out.print("Enter the email: ");
               String email = input.nextLine();

               Contact newC = new Contact(fname,lname,num,email);

               bookNew.addContact(newC);

               System.out.println("\n\nThis is the contact you added:\n"+newC);
           }
       }
   }
}

Issue:

when i go to search for it it says it cant be found

everything compiles without errors but when i run it and i can add a a contact just the search isnt working

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.util.Scanner;

public class Contact {

    private String fname;

    private String lname;

    private String phonenum;

    private String email;

    public Contact() {

        fname = " ";

        lname = " ";

        phonenum = " ";

        email = " ";

    }

    public Contact(String fn, String ln, String p, String e) {

        this.fname = fn;

        this.lname = ln;

        this.phonenum = p;

        this.email = e;

    }

    public void setfName(String fn) {

        this.fname = fn;

    }

    public void setlName(String ln) {

        this.lname = ln;

    }

    public void setNumber(String p) {

        this.phonenum = p;

    }

    public void setEmail(String e) {

        this.email = e;

    }

    public String getfName() {

       return fname;

    }

    public String getlName() {

        return lname;

    }

    public String getNumber() {

        return phonenum;

    }

    public String getEmail() {

        return email;

    }

    public String toString() {

        return "Name: " + fname + " " + lname + "\nPhone Number: " + phonenum + "\nEmail: " + email;

    }

}

ContactList.java

import java.util.Scanner;

import java.util.List;

import java.util.ArrayList;

public class ContactList {

    private List<Contact> info = null;

    public ContactList() {

        info = new ArrayList<Contact>();

    }

    public int addContact(Contact contact) {

        Contact n = findName(contact.getfName() + contact.getlName() + contact.getNumber() + contact.getEmail());

        if (n == null) {

            info.add(contact);

            return 1;

        }

        return -1;

    }

    public Contact findName(String s) {

        for (Contact c : info) {

           if (c.getfName().equalsIgnoreCase(s) || c.getlName().equalsIgnoreCase(s)) {

               return c;

            }

        }

        return null;

    }

}

ContactListDemo.java

import java.util.Scanner;

public class ContactListDemo {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        ContactList book = new ContactList();

        Contact c1 = new Contact();

        c1.setfName("Andy");

        c1.setlName("Williams");

        c1.setNumber("585-279-0095");

        c1.setEmail("aWilliams@aol.com");

        book.addContact(c1);

        Contact c2 = new Contact("Dean", "Martin", "616-223-3483", "dean.martin@yahoo.com");

        book.addContact(c2);

        String choice = "";

        while (!choice.equalsIgnoreCase("done")) {

            System.out.println(

                    "\nWould you like to add or search for a contact from the phone book?\nIf you would like find a contact type \"search\" or if you would like to add a contact type \"add\" and if you are finished type \"done\" to end.");

            System.out.println("");

            choice = input.nextLine();

            if (choice.equalsIgnoreCase("search")) {

                System.out.print("Enter the the first or last name: ");

                String search = input.nextLine();

                Contact result = book.findName(search);

                if (result != null) {

                    System.out.println("\nThe name was found.\nHere is the contact information:");

                    System.out.println(result);

                } else {

                    System.out.println("\nThe name couldn't be found.");

                }

            }

            if (choice.equalsIgnoreCase("add")) {

                ContactList bookNew = new ContactList();

                System.out.print("Enter the first name: ");

                String fname = input.nextLine();

                System.out.print("Enter the last name: ");

                String lname = input.nextLine();

                System.out.print("Enter the phone number: ");

                String num = input.nextLine();

                System.out.print("Enter the email: ");

                String email = input.nextLine();

                Contact newC = new Contact(fname, lname, num, email);

                bookNew.addContact(newC);

                System.out.println("\n\nThis is the contact you added:\n" + newC);

            }

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Java need help with searching for a contact Question:Write a program that uses an ArrayList of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Hello Can you help to fix the program. When running, it still show Exception in thread...

    Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap    at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • This one is a little tricky for me. Please put the following pseudocode into C++. Below...

    This one is a little tricky for me. Please put the following pseudocode into C++. Below the pseudocode is a header file as well as a cpp file that needs to be included. // Start // Declarations // TermPaper paper1 // string firstName // string lastName // string subject // character grade // output "Please enter first name. " // input firstName // output "Please enter last name. " // input lastName // output "Please enter subject. " // input...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

  • Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode ha...

    Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode has its unique function member GetEmail() which returns the string variable "studentEmail". TtuStudentNode has its overriding "PrintContactNode()" which has the following definition. void TtuStudentNode::PrintContactNode() { cout << "Name: " << this->contactName << endl; cout << "Phone number: " << this->contactPhoneNum << endl; cout << "Email : " << this->studentEmail << endl; return; } ContactNode.h needs to have the function...

  • // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12...

    // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12 #define NAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'" namespace Errors {     struct InvalidName { }; } class Name { public:     Name        ( string first_name = "", string last_name = "");     string first() const;     string last() const;     void   set_first( string fname );     void   set_last( string lname);     friend ostream& operator<< ( ostream & os, Name name );     friend bool operator== (Name name1, Name...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT