0

I have a phone book program that populates an ArrayList with Entry objects with data from a text file.

The data is loaded into the ArrayList when one is made using a constructor and is then sorted alphabetically by surname. What I need to be able to do is type in a name or telephone extension number that will then delete that specific Entry from the phone book. I also need to be able to search for an extension number by typing in a name.

So far I have tried tinkering with the equals method and created the deleteByName method in my Entry class however it leads to a lot of errors when I run the program (ConcurrentModificationException at several different lines)

public class Entry implements Comparable<Entry> {

    private String firstName = null;
    private String surname = null;
    private String extension = null;

    public Entry(String firstName, String surname, String extension) {
        super();
        this.firstName = firstName;
        this.surname = surname;
        this.extension = extension;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getSurname() {
        return surname;
    }

    public String getExtension() {
        return extension;
    }

    @Override
    public String toString() {
        return "\nFirst Name: " + getFirstName() + "\nSurname: " + getSurname() + "\nExtension: " + getExtension();
    }

    @Override
    public int compareTo(Entry e) {
        return surname.compareTo(e.getSurname());
    }

    @Override
    public boolean equals(Object arg0) {
        return ((Entry) arg0).firstName.equalsIgnoreCase(this.firstName);
    }

}

I have a class named ArrayDirectory that has the ArrayList and all operation methods.

public class ArrayDirectory implements Directory {

    // Instantiate variables for program
    private final static int MAX_ENTRIES = 20;
    private static Scanner input = new Scanner(System.in);
    DirectoryFile file = new DirectoryFile("C:\\Users\\John\\Documents\\Phonebook.txt");
    private ArrayList<Entry> phonebook = new ArrayList<>(MAX_ENTRIES);

    public ArrayDirectory() throws IOException {
        loadEntries(file.getFile());
        runMenu();
    }

    public void runMenu() {
        Scanner sc = new Scanner(System.in);
        int choice;
        do {
            System.out.println("\nStaff Phonebook Program");
            System.out.println("Select an option: (1-6)");
            System.out.println("1) Add new record");
            System.out.println("2) Delete record");
            System.out.println("3) Search for record");
            System.out.println("4) Change a number");
            System.out.println("5) Display all records");
            System.out.println("6) Exit program");

            choice = sc.nextInt();

            switch (choice) {
            case 1:
                System.out.println("\nEnter first name: ");
                String fname = input.nextLine();
                System.out.println("Enter surname: ");
                String sname = input.nextLine();
                System.out.println("Enter telephone extension: ");
                String telephone = input.nextLine();
                addEntry(fname, sname, telephone);
                break;
            case 2:
                printDirectory();
                System.out.println("Enter name or number: ");
                String nameOrNum = input.nextLine();
                System.out.println("Removed staff member: " + deleteByName(nameOrNum, phonebook));

                break;
            case 3:

                break;
            case 4:

                break;
            case 5:
                printDirectory();
                break;
            case 6:
                System.out.println("Program closed");
                break;
            default:

                break;
            }
        } while (choice != 6);
    }

    // This method will allow the user to add a new entry to the phone book. The
    // phone book will be
    // alphabetically sorted.
    public void addEntry(String firstName, String surname, String extension) {
        Entry entry = new Entry(firstName, surname, extension);
        phonebook.add(entry);
        System.out.println("\nAdded staff member: " + firstName + "\t" + surname + "\t" + extension);

    } // End of addEntry()

    public ArrayList<Entry> loadEntries(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        try {
            String line = br.readLine();

            while (line != null) {
                String arr[] = line.split("\\s");
                // Entry(firstName, surname, extension)
                Entry entry = new Entry(arr[0], arr[1], arr[2]);
                phonebook.add(entry);
                line = br.readLine();
                if (line.isEmpty()) {
                    break;
                }

            }
        } finally {
            br.close();
        }
        sortBySurname(phonebook);
        return phonebook;
    } // End of loadEntries()

    // This method will allow the user to delete a specific entry
    // From the directory either by name or number
    public void deleteEntry(String fullName, String phoneNumber) {
        // TODO
    }

    public void printDirectory() {
        System.out.println("Surname \tFirst Name \tExtension Number");
        for (Entry e : phonebook) {
            System.out.println(e.getFirstName() + "\t" + e.getSurname() + "\t" + e.getExtension());
        }
    }

    public ArrayList<Entry> sortBySurname(ArrayList<Entry> entries) {
        Collections.sort(entries);
        return entries;
    }

    public boolean deleteByName(String name, ArrayList<Entry> entries) {
        for (Entry e : entries) {
            if (e.getFirstName().equals(name)) {
                entries.remove(e);
            }
        }
        return false;
    }
}

If possible, I'd like to get the equals method overridden correctly so it can maybe be used for the other operations. I think I am struggling to fully understand how the equals method is working. Will this be the best way to go about the problem and can it also be used to help with the other operations I need to perform?

3
  • Wall-of-code questions are frowned upon. Commented Apr 28, 2019 at 14:55
  • Some thoughts: The ConcurrentModificationException happens because you modify entries in the loop with entries.remove(e). To resolve this use phonebook.removeIf(e -> e.getFirstName().equals(name)) (Java 8) in the delete method. Do not pass the List as a parameter but access the attribute directly. The equals-method and compareTo-method in Entry is probably not what you want. Include all relevant attributes like first name, last name and extension. Call to super() in Entry is not necessary. Commented Apr 28, 2019 at 15:20
  • Possible duplicate of How to remove element from ArrayList by checking its value? Commented Apr 28, 2019 at 15:24

0

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.