1

Learning, be kind.

I have an address book and I need the user to be able to delete an entry based on their name, modify an entry based on their name, and search for an entry (you guessed it) based on their name.

I have looked at examples and such and I am lost as to how to do this. Any help would be much appreciated.

Here is my class file:

import java.util.Calendar;
import java.util.Date;
import java.text.*;

public class Person implements Comparable {


    private static int totalNumber;

    public static int getTotal() {

        //Returns total number of employees
        return totalNumber;
    }

private String fullName;
private String fName;
private String lName;
private Date lastModified;
private String address;
private String city;
private String state;
private String zip;
private String phone;

public Person(String fName, String lName, String address, String city, String state, String zip, String phone) {
    this.fName = fName;
    this.lName = lName;
    this.address = address;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.phone = phone;     
    Calendar calobj = Calendar.getInstance();
    this.lastModified = calobj.getTime();
}

public String getfullName() {
    return this.fullName;
}
public void setfullName(String fullName) {
    this.fullName = this.fName + this.lName;
}
public String getfName() {
    return this.fName;
}

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

public String getlName() {
    return this.lName;
}

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

public Date getLastModified() {
    return this.lastModified;
}

public void setLastModified(Date lastModified) {
    this.lastModified = lastModified;
}

public String getAddress() {
    return this.address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCity() {
    return this.city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return this.state;
}

public void setState(String state) {
    this.state = state;
}

public String getZip() {
    return this.zip;
}

public void setZip(String zip) {
    this.zip = zip;
}

public String getPhone() {
    return this.phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

@Override
public String toString() {
    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
    return "\n First Name= " + fName + 
            "\n Last Name= " + lName + 
            "\n Address= " + address + 
            "\n City= " + city + 
            "\n State= " + state + 
            "\n Zip= " + zip + 
            "\n Phone= " + phone + 
            "\n Last Modified= "
            + df.format(lastModified); 
}

@Override
public int compareTo(Object other) {
    // TODO Auto-generated method stub
    return this.lName.compareToIgnoreCase(((Person) other).lName);

}








}

Here is my test file: public class testAddressBook {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList<Person> addressBook = new ArrayList<Person>();
        Person newPerson = new Person(null, null, null, null, null, null, null);


        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);

        boolean switcher = true;
        do {
            System.out.println("\n\tAddress Book Menu");
            System.out.println("\n\t\tEnter A to (A)dd Person ");
            System.out.println("\t\tEnter D to (D)elete Person");
            System.out.println("\t\tEnter M to (M)odify Person");
            System.out.println("\t\tEnter S to (S)earch Address Book ");
            System.out.println("\t\tEnter L to (L)ist ALL (sorted) ");
            System.out.println("\t\tEnter Q to Quit");
            System.out.print("\n\tPlease enter your choice: ");
            char choice = sc.nextLine().toUpperCase().charAt(0);


            while ((choice != 'A') && (choice != 'D') && (choice != 'M')  && (choice != 'S') && (choice != 'L')&& (choice != 'Q')) {
                System.out.println("Invalid choice!  Please select (A)dd, (D)elete, (M)odify, (S)earch, (L)ist or (Q)uit: ");
                choice = sc.nextLine().toUpperCase().charAt(0);
            }


            switch (choice) {
            case 'A' :      
                System.out.println("\nTo add a person, follow the prompts.");
                newPerson = new Person(null, null, null, null, null, null, null);
                System.out.print("\nEnter First Name: ");
                newPerson.setfName(sc.nextLine());

                System.out.print("\nEnter Last Name: ");
                newPerson.setlName(sc.nextLine());

                System.out.print("Enter Address: ");
                newPerson.setAddress(sc.nextLine());

                System.out.print("Enter City: ");
                newPerson.setCity(sc.nextLine());

                System.out.print("Enter State: ");
                newPerson.setState(sc.nextLine());

                System.out.print("Enter Zip: ");
                newPerson.setZip(sc.nextLine());

                System.out.print("Enter Phone Number: ");
                newPerson.setPhone(sc.nextLine());

                        addressBook.add(newPerson);

                System.out.println("\nYou have successfully added a new person!");

                break;

            case 'D' :

                break;
            case 'M' :

                break;
            case 'S' :

                System.out.println("Please enter first & last name (ex. Bob Smith): ");
                String fullName = sc.nextLine();

                if (addressBook.contains(fullName)) {
                    System.out.println("Yes, this person exists!");

                    } else {
                        System.out.println("ALERT!  No record of that name exists in this address book.");
                    }

                        break;
            case 'L' :
                System.out.println("\nThere are " + addressBook.size() + " people in this address book.\n");
                Collections.sort(addressBook);


                for (int i = 0; i < addressBook.size(); i++) {
                    System.out.println(addressBook.get(i));


                }
                    System.out.println();


                break;
            case 'Q' :
                switcher = false;
                System.exit(0);
                break;
            default:

            }

        }
        while (switcher != false);


    }
}

3 Answers 3

3

For Everything over here you have to ask user to provide some input and then perform operation on that input

To Delete

You can either iterate throught the list or can get the index by passing an element from the User input.You can use the remove method like this:-

<Your Arraylist>.remove(<Index of Element to be remove>);

To Modify

Get that particular value using get(index) method of ArrayList and and then update the value using set.

To Search

Find it using get method If present Yes {Do Something} if not Present then No {Do Something}

Refer Methods of ArrayList over here http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Sign up to request clarification or add additional context in comments.

Comments

0

You need to work out exactly what you are trying to do, what data structures you have, and how the latter supports the former.

So for instance "search for an entry... based on their name." That's a typical user-level specification of program behaviour. How can we be more exact? Perhaps we might say "find a Person in the AddressBook whose name matches the input exactly, but ignore differences in upper/lowercase." That's still a long way above the precision of working code, but it gets your mind more focused on the details of the problem.

Your key datastructure is

 ArrayList<Person> addressBook = new ArrayList<Person>();

a list of Person objects. Hence the following line of code,

if (addressBook.contains(fullName)) {

while syntactically valid, isn't really asking what you think it is. You are asking "does this list of Persons contain this String"? When you phrase it like that, the answer is clearly no, because it doesn't have any Strings in it, only Persons. It would be better to declare a method like this:

private boolean findPersonWithName(List<Person> people, String name) {
  for (Person p : people) {
    if (p.getFullName().equalsIgnoreCase(name)) {
      return p;
    }
  }

  // didn't find anyone
  return null;
}

Comments

-1

Search

public List<Person> getEntryByName(String name){
    List<Person> foundList = new ArrayList<Person>()
    for(Person p: addressBook){
        if(p.getFullName().contains(name))
            foundList.add(p);
    }
}

Delete:

public void removeEntriesByName(String name){
    int count = 0;
    Iterator<Person> i = addressBook.iterator();
    while (i.hasNext()) {
        Person p = i.next();i.remove()
        if (p.getFullName().contains(name)){
            i.remove();
            addressBook.remove(index);
        }
    count++;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.