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?
Entry, the object is not added to the Arraylist.Modularitycan be achieved. OnlyEntryclass can know when to throw an exception in case wrong inputs.