Reputation: 369
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?
Upvotes: 0
Views: 268
Reputation: 61158
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);
}
}
Upvotes: 0
Reputation: 45070
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.
}
Upvotes: 0
Reputation: 167
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
}
Upvotes: 3