如何增强Addressbook addEntry()方法?

时间:2011-02-19 04:38:18

标签: java variable-assignment addressbook conditional-statements

我有一个地址簿程序,[1]添加条目[2]删除条目[3]更新/编辑条目[4]查看所有条目和[5]查看特定条目..

条目存储在数组entry[]中,如:

entry[counter] = new AddressBookEntry();

这一切都正常运行但事情是......我想要检查用户的输入名称是否已被使用,或者当我询问“输入姓名:”时用户没有输入任何内容

这是我的addEntry()方法:

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++;
}

它允许用户输入他喜欢的任何条目...但不检查名称是否已被使用,如果用户将“输入名称:”留空,它仍允许用户进入“输入添加:”。

这是我在主程序中的完整代码:

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;
            }
        }
    }
}

希望你能帮助我,我是Java的新手,我真的不在我的代码中添加什么来检查  *我应该在addEntry()方法中添加什么来检查名称是否已被使用或未输入名称?  *我需要searchMethod()中的条件吗?

1 个答案:

答案 0 :(得分:1)

地址簿往往是一个给出姓名,查找地址和其他信息的东西。

所以这属于一般模式,你有一个Key(名字)和一个值(其他信息)

当你看到这个时,你想要考虑使用Map

所以,我建议不要把你的AddressBookEntry放在一个数组中,使用这样的地图

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

然后当你想添加新条目时

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

关于地图的好处是你只能为同一个人创建一个条目。

所以如果你把约翰放在书中两次,你不必担心会发生什么。它只会允许一个人在那里。