Search code examples
javavariable-assignmentaddressbookconditional-statements

How can I Enhance my AddressBook addEntry() method?


I have an Address Book Program that [1] adds Entry [2] delete entry [3] update/edit entry [4] view all entry and [5] view specific entry..

Entries were stored in an Array entry[] like:

entry[counter] = new AddressBookEntry();

It all runs properly but the thing is... i want there was a checking if the user's input name was already used or if the user doesn't typed anything when i ask "Enter Name: "

here's my addEntry() method:

public void addEntry() {

    entry[counter] = new AddressBookEntry();
    entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
    entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
    entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
    entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
    counter++;
}

It allows user input any Entry he likes... but no checking if the name is already used and if the user leave the "Enter Name: " blank it still allows the user to proceed to the "Enter Add.: "

here's my complete code in my main program:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class AddressBook {

    private AddressBookEntry entry[];
    private int counter;
    private String SName;
    private int notfound = 0;

    public static void main(String[] args) {
        AddressBook a = new AddressBook();
        a.entry = new AddressBookEntry[100];
        int option = 0;
        try {
            while (option != 5) {
                String content = "Choose an Option\n\n"
                        + "[1] Add an Entry\n"
                        + "[2] Delete an Entry\n"
                        + "[3] Update an Entry\n"
                        + "[4] View all Entries\n"
                        + "[5] View Specific Entry\n"
                        + "[6] Exit";
                option = Integer.parseInt(JOptionPane.showInputDialog(content));
                switch (option) {
                    case 1:
                        a.addEntry();
                        break;
                    case 2:
                        a.deleteEntry();
                        break;
                    case 3:
                        a.editEntry();
                        break;
                    case 4:
                        a.viewAll();
                        break;
                    case 5:
                        a.searchEntry();
                        break;
                    case 6:
                        System.exit(1);
                        break;
                    default:
                        JOptionPane.showMessageDialog(null, "Invalid Choice!");
                }
            }
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Please Choose a Number in the displayed Menu");
        }
    }

    public void addEntry() {
        entry[counter] = new AddressBookEntry();
        entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
        entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
        entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
        entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
        counter++;
    }

    public void viewAll() {
        String addText = "  NAME\tADDRESS\tPHONE NO.\tE-MAIL ADD\n\n";
        int nonNull = 0;
        for (int i = 0; i < entry.length; i++) {
            if (entry[i] != null) {
                addText = addText + entry[i].getInfo() + "\n";
                nonNull++;
            }
            if (nonNull == counter) {
                break;
            }
        }
        JOptionPane.showMessageDialog(null, new JTextArea(addText));
    }

    public void searchEntry() {
        SName = JOptionPane.showInputDialog("Enter Name to find: ");
        searchMethod();
    }

    public void searchMethod() {
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(SName)) {
                JOptionPane.showMessageDialog(null, entry[i].getInfo2());
                notfound = 0;
                break;
            } else {
                notfound++;
            }
        }
        if (notfound != 0) {
            JOptionPane.showMessageDialog(null, "Name Not Found!");
        }
    }

    public void editEntry() {
        SName = JOptionPane.showInputDialog("Enter Name to edit: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(SName)) {
                entry[i] = new AddressBookEntry();
                entry[i].setName(JOptionPane.showInputDialog("Enter new name: "));
                entry[i].setAdd(JOptionPane.showInputDialog("Enter new add: "));
                entry[i].setPhoneNo(JOptionPane.showInputDialog("Enter new Phone No.: "));
                entry[i].setEmail(JOptionPane.showInputDialog("Enter new E-mail: "));
                notfound = 0;
                break;
            } else {
                notfound++;
            }
        }
        if (notfound != 0) {
            JOptionPane.showMessageDialog(null, "Name Not Found!");
        }
    }

    public void deleteEntry() {
        SName = JOptionPane.showInputDialog("Enter Name to delete: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(SName)) {
                JOptionPane.showMessageDialog(null, "Found!");
                entry[i] = null;
                break;
            }
        }
    }
}

Hope you can can help me I am new in Java and I really don't what to add in my code for checking.
* What should I add in my addEntry() method to check if the name was already used or no name entered? * Do I need a condition like in my searchMethod()?


Solution

  • An address book tends to be a thing where given a name, you look up an address, and other information.

    so this falls under a general pattern where you have a Key (the name) and a value (the other information)

    When ever you see this, you want to think of using a Map

    So, i'd recommend instead of putting your AddressBookEntry's in an array, use a map like this

    Map<String, AddressBookEntry> addressBook = new HashMap<String, AddressBookEntry>();
    

    then when you want to add a new entry do

    addressBook.put("John", new AddressBookEntry());
    

    the nice thing about a map is that you can only have one entry for the same person.

    So you don't have to worry about what happens if you put John in the book twice. It will only allow one in there.