0

I have an assignment to create an address book. I created my program based on good usability. For instance when creating a new entry a user cannot move onto the next input until a valid one has been given or they choose to cancel. After I created the program I was reading through the colleges examples the tutor had given and instead of checking for valid inputs before the entry is added they send the .add request and then raise an exception if bad data is detected.

I asked my tutor if I was supposed to do it this way as even though I think mine is better design I dont want to lose marks because I didnt do it their way. He said I should stick to the examples as follows:

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

I tried it this way and changed the:

throw new RuntimeException(e.getMessage()); 

line to read

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

So that it wouldnt quit out of the program however doing it this way already adds the entry so after giving the appropriate error I need to remove that entry that has been added. How can I do this? does it have some sort of index? I dont know why the tutor would want you to do it this way or am I missing the point?

4
  • Don't add the entry? Catch the exception? Commented Mar 8, 2013 at 9:25
  • @bmorris591 thats what my original version did but when I talked to my tutor he said to stick to their examples an this is the way they have done it however rather than quit out like their example shows he said I need to handle the exception, display a message to the user then ask to re-enter. Dont see how this can be done AFTER the entry has been added like their example show. I think I might just submit my original program and risk losing a few marks Commented Mar 8, 2013 at 9:30
  • 1
    When an exception occurs in the constructor of the Entry , the object is not added to the Arraylist. Commented Mar 8, 2013 at 9:32
  • One reason that your tutor might have asked to do it is because, in this way a better Modularity can be achieved. Only Entry class can know when to throw an exception in case wrong inputs. Commented Mar 8, 2013 at 9:37

3 Answers 3

3

If an exception is thrown in the Entry constructor which is called here:

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

It won't be added to your list. Just leave the code as it was and catch the exception here:

try{
   addressBook.addPerson(new Entry(name, street, town, postcode));
catch(Exception e){
   //Tell the user what he did wrong and let him reenter
}
Sign up to request clarification or add additional context in comments.

5 Comments

@yeans Thank you, I will give this a try now. Still dont like how their making you catch it after giving ALL the fields rather than catching it one at a time but I suppose I can mention this in the report as improvements
You mean like a "on the fly" validation?
yes so rather than enter all fields get to the end and then be told something is wrong it will loop around until its valid before moving on - Which is what my original program does.
How did your original code implements this single field validation?
it just contained another method that would loop round until a valid input was given or the user cancelled out when either of these were true it returned back to the addCommand method the IF would check it was true an move to the next statement. PS got it working using your example. Thanks
0

Why not put your entry addition in 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.
}

Comments

0

You can declare a checked exception in your constructor, i.e. an exception that extends Exception rather than RuntimeException. You will then be forced to handle it in your addCommand method.

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

1 Comment

@bmorris591 I cant get this one working, just keep getting the error 'Exception BadDataException is never thrown in body of corresponding try statement' from the addCommand method

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.