0

我有一个创建通讯录的任务。我基于良好的可用性创建了我的程序。例如,当创建一个新条目时,用户不能移动到下一个输入,直到给出一个有效的输入或者他们选择取消。在我创建程序后,我正在阅读导师给出的大学示例,而不是在添加条目之前检查有效输入,他们发送 .add 请求,然后在检测到错误数据时引发异常。

我问我的导师我是否应该这样做,因为即使我认为我的设计更好,我也不想失去分数,因为我没有按照他们的方式去做。他说我应该坚持以下例子:

public AddressBook()
{
    entries = new ArrayList < Entry >();
}

public void addPerson(Entry addPerson)
{
    entries.add(addPerson);
}

private void addCommand()
   {      

    System.out.print("Enter Full Name : ");
    String name = myScanner.nextLine();
    System.out.print("Enter House No and Street name: ");
    String street = myScanner.nextLine();
    System.out.print("Enter Town: ");
    String town = myScanner.nextLine();
    System.out.print("Enter Postcode: ");
    String postcode = myScanner.nextLine();
    postcode = postcode.toUpperCase();

    addressBook.addPerson(new Entry(name, street, town, postcode));        
   }

public Entry(String strName, String strStreet, String strTown, String strPostcode)
    {
       name = strName;
       street = strStreet;
       town = strTown;
       postcode = strPostcode;
       try
       {
          checkDetails();
       }
       catch ( BadDataException e)
        {
            throw new RuntimeException( e.getMessage());             }
    }

我尝试过这种方式并更改了:

throw new RuntimeException(e.getMessage()); 

读线

System.out.println( e.getMessage());

这样它就不会退出程序,但是这样做已经添加了条目,所以在给出适当的错误后,我需要删除已添加的条目。我怎样才能做到这一点?它有某种索引吗?我不知道导师为什么要你这样做,还是我没有抓住重点?

4

3 回答 3

3

如果在此处调用的 Entry 构造函数中抛出异常:

addressBook.addPerson(new Entry(name, street, town, postcode));

它不会被添加到您的列表中。只需保留代码原样并在此处捕获异常:

try{
   addressBook.addPerson(new Entry(name, street, town, postcode));
catch(Exception e){
   //Tell the user what he did wrong and let him reenter
}
于 2013-03-08T09:33:05.697 回答
0

您可以在构造函数中声明已检查异常,即扩展Exception而不是RuntimeException. 然后,您将被迫在您的addCommand方法中处理它。

public AddressBook() {
    entries = new ArrayList< Entry>();
}

public void addPerson(Entry addPerson) {
    entries.add(addPerson);
}

private void addCommand() {

    System.out.print("Enter Full Name : ");
    String name = myScanner.nextLine();
    System.out.print("Enter House No and Street name: ");
    String street = myScanner.nextLine();
    System.out.print("Enter Town: ");
    String town = myScanner.nextLine();
    System.out.print("Enter Postcode: ");
    String postcode = myScanner.nextLine();
    postcode = postcode.toUpperCase();
    final Entry entry;
    try {
        entry = new Entry(name, street, town, postcode);
    } catch  (BadDataException ex) {
        System.out.println(ex.getMessage());
        return;
    }        
    addressBook.addPerson(new Entry(name, street, town, postcode));
}


public Entry(String strName, String strStreet, String strTown, String strPostcode) throws BadDataException {
    name = strName;
    street = strStreet;
    town = strTown;
    postcode = strPostcode;
    try {
       checkDetails();
    } catch(Exception ex) {
       throw new BadDataException(ex);
    }
}

private static class BadDataException extends Exception {

    public BadDataException(final Throwable cause) {
         super(cause);
    }
}
于 2013-03-08T09:33:16.020 回答
0

为什么不将您的条目添加在 a 中try-catch

try{
    Entry entry = new Entry(name, street, town, postcode);
    addressBook.addPerson(entry);
}catch(Exception e){
    // Entry is not added into the List.
    // do something and make the user re-enter the details.
}
于 2013-03-08T09:33:24.330 回答