From 9eb9674023ca11ceb135efb4e2da6949476f0692 Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 13:53:28 +0800 Subject: [PATCH 1/8] new method setPhone, setEmail, setAddress in Person class, new class UpdateCommand --- src/seedu/addressbook/commands/UpdateCommand.java | 4 ++++ src/seedu/addressbook/data/person/Person.java | 13 +++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/seedu/addressbook/commands/UpdateCommand.java diff --git a/src/seedu/addressbook/commands/UpdateCommand.java b/src/seedu/addressbook/commands/UpdateCommand.java new file mode 100644 index 000000000..3a646288e --- /dev/null +++ b/src/seedu/addressbook/commands/UpdateCommand.java @@ -0,0 +1,4 @@ +package seedu.addressbook.commands; + +public class UpdateCommand { +} diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index 86561474e..b73ff7b09 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -34,6 +34,19 @@ public Person(ReadOnlyPerson source) { this(source.getName(), source.getPhone(), source.getEmail(), source.getAddress(), source.getTags()); } + public void setPhone(Phone phone) { + this.phone = phone; + } + + public void setEmail(Email email) { + this.email = email; + } + + public void setAddress(Address address) { + this.address = address; + } + + @Override public Name getName() { return name; From 80b5892cbe6be5ffe9644b19e533e1122df678bb Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 14:00:06 +0800 Subject: [PATCH 2/8] new method UpdatePerson in AddressBook --- .../addressbook/commands/UpdateCommand.java | 75 ++++++++++++++++++- src/seedu/addressbook/data/AddressBook.java | 35 +++++++++ src/seedu/addressbook/data/person/Person.java | 2 +- 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/src/seedu/addressbook/commands/UpdateCommand.java b/src/seedu/addressbook/commands/UpdateCommand.java index 3a646288e..c0229ad7a 100644 --- a/src/seedu/addressbook/commands/UpdateCommand.java +++ b/src/seedu/addressbook/commands/UpdateCommand.java @@ -1,4 +1,77 @@ package seedu.addressbook.commands; -public class UpdateCommand { +import java.util.HashSet; +import java.util.Set; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.data.person.Address; +import seedu.addressbook.data.person.Email; +import seedu.addressbook.data.person.Name; +import seedu.addressbook.data.person.Person; +import seedu.addressbook.data.person.Phone; +import seedu.addressbook.data.person.ReadOnlyPerson; +import seedu.addressbook.data.person.UniquePersonList; +import seedu.addressbook.data.tag.Tag; +import seedu.addressbook.data.tag.UniqueTagList; + +/** + * Update a person's info in the address book. + */ +public class UpdateCommand extends Command { + + public static final String COMMAND_WORD = "update"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": updates a person's info in the address book. " + + "Contact details can be marked private by prepending 'p' to the prefix.\n" + + "Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]...\n" + + "Example: " + COMMAND_WORD + + " John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney"; + + public static final String MESSAGE_SUCCESS = "person info updated added: %1$s"; + public static final String MESSAGE_NONEXIST_PERSON = "This person does not exist in the AddressBook"; + + private final Person toUpdate; + + /** + * Convenience constructor using raw values. + * + * @throws IllegalValueException if any of the raw values are invalid + */ + public UpdateCommand(String name, + String phone, boolean isPhonePrivate, + String email, boolean isEmailPrivate, + String address, boolean isAddressPrivate, + Set tags) throws IllegalValueException { + final Set tagSet = new HashSet<>(); + for (String tagName : tags) { + tagSet.add(new Tag(tagName)); + } + this.toUpdate = new Person( + new Name(name), + new Phone(phone, isPhonePrivate), + new Email(email, isEmailPrivate), + new Address(address, isAddressPrivate), + new UniqueTagList(tagSet) + ); + } + + public UpdateCommand(Person toUpdate) { + this.toUpdate = toUpdate; + } + + public ReadOnlyPerson getPerson() { + return toUpdate; + } + + @Override + public CommandResult execute() { + try { + addressBook.updatePerson(toUpdate); + return new CommandResult(String.format(MESSAGE_SUCCESS, toUpdate)); + } catch (AddressBook.PersonNonExistException pnee) { + return new CommandResult(MESSAGE_NONEXIST_PERSON); + } + } + } diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index a99a92f9f..f54b1a547 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -33,6 +33,16 @@ public AddressBook() { allTags = new UniqueTagList(); } + /** + * Signals that an operation that try's to update a person who does not exist in the list + */ + public static class PersonNonExistException extends Exception { + public PersonNonExistException() { + super("Person does not exist in AddressBook"); + } + } + + /** * Constructs an address book with the given data. * Also updates the tag list with any missing tags found in any person. @@ -83,6 +93,31 @@ public void addPerson(Person toAdd) throws DuplicatePersonException { syncTagsWithMasterList(toAdd); } + + /** + * Updates a person's info in the address book. + * Also checks the new person's tags and updates {@link #allTags} with any new tags found, + * and updates the Tag objects in the person to point to those in {@link #allTags}. + * + * @throws PersonNonExistException if the person does not exist in the addressbook. + */ + public void updatePerson(Person toUpdate) throws PersonNonExistException{ + + if (!containsPerson(toUpdate)) { + throw new PersonNonExistException(); + } + + for (Person each : allPersons) { + if (each.getName().equals(toUpdate.getName())) { + each.setAddress(toUpdate.getAddress()); + each.setEmail(toUpdate.getEmail()); + each.setPhone(toUpdate.getPhone()); + } + } + syncTagsWithMasterList(toUpdate); + + } + /** * Returns true if an equivalent person exists in the address book. */ diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index b73ff7b09..eefbb3771 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -42,7 +42,7 @@ public void setEmail(Email email) { this.email = email; } - public void setAddress(Address address) { + public void setAddress(Address address) { this.address = address; } From 85e68696249a86d233f8fd62c4194dffe3b25736 Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 14:05:19 +0800 Subject: [PATCH 3/8] Update Parser to support update command --- .../addressbook/commands/UpdateCommand.java | 2 +- src/seedu/addressbook/parser/Parser.java | 47 ++++++++++++++----- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/seedu/addressbook/commands/UpdateCommand.java b/src/seedu/addressbook/commands/UpdateCommand.java index c0229ad7a..095efcc43 100644 --- a/src/seedu/addressbook/commands/UpdateCommand.java +++ b/src/seedu/addressbook/commands/UpdateCommand.java @@ -28,7 +28,7 @@ public class UpdateCommand extends Command { + "Example: " + COMMAND_WORD + " John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney"; - public static final String MESSAGE_SUCCESS = "person info updated added: %1$s"; + public static final String MESSAGE_SUCCESS = "person info updated: %1$s"; public static final String MESSAGE_NONEXIST_PERSON = "This person does not exist in the AddressBook"; private final Person toUpdate; diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index 25c412f6b..cdcb66dc3 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -11,17 +11,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import seedu.addressbook.commands.AddCommand; -import seedu.addressbook.commands.ClearCommand; -import seedu.addressbook.commands.Command; -import seedu.addressbook.commands.DeleteCommand; -import seedu.addressbook.commands.ExitCommand; -import seedu.addressbook.commands.FindCommand; -import seedu.addressbook.commands.HelpCommand; -import seedu.addressbook.commands.IncorrectCommand; -import seedu.addressbook.commands.ListCommand; -import seedu.addressbook.commands.ViewAllCommand; -import seedu.addressbook.commands.ViewCommand; +import seedu.addressbook.commands.*; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -78,6 +68,9 @@ public Command parseCommand(String userInput) { case AddCommand.COMMAND_WORD: return prepareAdd(arguments); + case UpdateCommand.COMMAND_WORD: + return prepareUpdate(arguments); + case DeleteCommand.COMMAND_WORD: return prepareDelete(arguments); @@ -137,6 +130,38 @@ private Command prepareAdd(String args) { } } + /** + * Parses arguments in the context of the update person command. + * + * @param args full command args string + * @return the prepared command + */ + private Command prepareUpdate(String args) { + final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(args.trim()); + // Validate arg string format + if (!matcher.matches()) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); + } + try { + return new UpdateCommand( + matcher.group("name"), + + matcher.group("phone"), + isPrivatePrefixPresent(matcher.group("isPhonePrivate")), + + matcher.group("email"), + isPrivatePrefixPresent(matcher.group("isEmailPrivate")), + + matcher.group("address"), + isPrivatePrefixPresent(matcher.group("isAddressPrivate")), + + getTagsFromArgs(matcher.group("tagArguments")) + ); + } catch (IllegalValueException ive) { + return new IncorrectCommand(ive.getMessage()); + } + } + /** * Returns true if the private prefix is present for a contact detail in the add command's arguments string. */ From 0d5569f5d97cb2b3d791e3bcce15b56ea3d79584 Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 14:22:57 +0800 Subject: [PATCH 4/8] correcting error message --- src/seedu/addressbook/data/AddressBook.java | 10 +++++++--- src/seedu/addressbook/parser/Parser.java | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index f54b1a547..12b8482ef 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -103,17 +103,21 @@ public void addPerson(Person toAdd) throws DuplicatePersonException { */ public void updatePerson(Person toUpdate) throws PersonNonExistException{ - if (!containsPerson(toUpdate)) { - throw new PersonNonExistException(); - } + boolean isPersonExist = false; for (Person each : allPersons) { if (each.getName().equals(toUpdate.getName())) { each.setAddress(toUpdate.getAddress()); each.setEmail(toUpdate.getEmail()); each.setPhone(toUpdate.getPhone()); + isPersonExist = true; } } + + if (!isPersonExist) { + throw new PersonNonExistException(); + } + syncTagsWithMasterList(toUpdate); } diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index cdcb66dc3..7ad0a8195 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -139,8 +139,8 @@ private Command prepareAdd(String args) { private Command prepareUpdate(String args) { final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(args.trim()); // Validate arg string format - if (!matcher.matches()) { - return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); + if (!matcher.matches()) { ; + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UpdateCommand.MESSAGE_USAGE)); } try { return new UpdateCommand( From 01003353faef1b8767110f2a74adf036aaad4aee Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 14:45:43 +0800 Subject: [PATCH 5/8] Update help command to include update command --- src/seedu/addressbook/commands/HelpCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index 8a36e8337..8854c4590 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -17,6 +17,7 @@ public HelpCommand() {} public CommandResult execute() { return new CommandResult( AddCommand.MESSAGE_USAGE + + "\n" + UpdateCommand.MESSAGE_USAGE + "\n" + DeleteCommand.MESSAGE_USAGE + "\n" + ClearCommand.MESSAGE_USAGE + "\n" + FindCommand.MESSAGE_USAGE From 161c838a10bc3efd996b202abbb1671397668dff Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 14:48:51 +0800 Subject: [PATCH 6/8] Update UserGuide to include update --- doc/UserGuide.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/UserGuide.md b/doc/UserGuide.md index fcd5a7121..e8d41a4b1 100644 --- a/doc/UserGuide.md +++ b/doc/UserGuide.md @@ -49,6 +49,22 @@ Examples: * `add John Doe p/98765432 e/johnd@gmail.com a/John street, block 123, #01-01` * `add Betsy Crowe pp/1234567 e/betsycrowe@gmail.com pa/Newgate Prison t/criminal t/friend` +### Adding a person: `update` +Updates a person's info in the address book
+Format: `update NAME [p]p/PHONE_NUMBER [p]e/EMAIL [p]a/ADDRESS [t/TAG]...` + +> Words in `UPPER_CASE` are the parameters, items in `SQUARE_BRACKETS` are optional, +> items with `...` after them can have multiple instances. Order of parameters are fixed. +> +> Put a `p` before the phone / email / address prefixes to mark it as `private`. `private` details can only +> be seen using the `viewall` command. +> +> Persons can have any number of tags (including 0) + +Examples: +* `Update John Doe p/98765432 e/johnd@gmail.com a/John street, block 123, #01-01` +* `Update Betsy Crowe pp/1234567 e/betsycrowe@gmail.com pa/Newgate Prison t/criminal t/friend` + ### Listing all persons : `list` Shows a list of all persons in the address book.
Format: `list` From 94045d167120cf8635a52b126679eb043eac52bb Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 15:44:23 +0800 Subject: [PATCH 7/8] update junit test with update --- src/seedu/addressbook/data/AddressBook.java | 1 + .../commands/UpdateCommandTest.java | 128 ++++++++++++++++++ .../addressbook/data/AddressBookTest.java | 22 +++ .../seedu/addressbook/parser/ParserTest.java | 83 ++++++++++-- 4 files changed, 223 insertions(+), 11 deletions(-) create mode 100644 test/java/seedu/addressbook/commands/UpdateCommandTest.java diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 12b8482ef..f70a84d7e 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -110,6 +110,7 @@ public void updatePerson(Person toUpdate) throws PersonNonExistException{ each.setAddress(toUpdate.getAddress()); each.setEmail(toUpdate.getEmail()); each.setPhone(toUpdate.getPhone()); + each.setTags(toUpdate.getTags()); isPersonExist = true; } } diff --git a/test/java/seedu/addressbook/commands/UpdateCommandTest.java b/test/java/seedu/addressbook/commands/UpdateCommandTest.java new file mode 100644 index 000000000..41a17522b --- /dev/null +++ b/test/java/seedu/addressbook/commands/UpdateCommandTest.java @@ -0,0 +1,128 @@ +package seedu.addressbook.commands; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.data.person.Address; +import seedu.addressbook.data.person.Email; +import seedu.addressbook.data.person.Name; +import seedu.addressbook.data.person.Person; +import seedu.addressbook.data.person.Phone; +import seedu.addressbook.data.person.ReadOnlyPerson; +import seedu.addressbook.data.person.UniquePersonList; +import seedu.addressbook.util.TestUtil; + +public class UpdateCommandTest { + private static final List EMPTY_PERSON_LIST = Collections.emptyList(); + private static final Set EMPTY_STRING_LIST = Collections.emptySet(); + + @Test + public void updateCommand_invalidName_throwsException() { + final String[] invalidNames = { "", " ", "[]\\[;]" }; + for (String name : invalidNames) { + assertConstructingInvalidUpdateCmdThrowsException(name, Phone.EXAMPLE, true, Email.EXAMPLE, false, + Address.EXAMPLE, true, EMPTY_STRING_LIST); + } + } + + @Test + public void updateCommand_invalidPhone_throwsException() { + final String[] invalidNumbers = { "", " ", "1234-5678", "[]\\[;]", "abc", "a123", "+651234" }; + for (String number : invalidNumbers) { + assertConstructingInvalidUpdateCmdThrowsException(Name.EXAMPLE, number, false, Email.EXAMPLE, true, + Address.EXAMPLE, false, EMPTY_STRING_LIST); + } + } + + @Test + public void updateCommand_invalidEmail_throwsException() { + final String[] invalidEmails = { "", " ", "def.com", "@", "@def", "@def.com", "abc@", + "@invalid@email", "invalid@email!", "!invalid@email" }; + for (String email : invalidEmails) { + assertConstructingInvalidUpdateCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, false, email, false, + Address.EXAMPLE, false, EMPTY_STRING_LIST); + } + } + + @Test + public void updateCommand_invalidAddress_throwsException() { + final String[] invalidAddresses = { "", " " }; + for (String address : invalidAddresses) { + assertConstructingInvalidUpdateCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, + true, address, true, EMPTY_STRING_LIST); + } + } + + @Test + public void updateCommand_invalidTags_throwsException() { + final String[][] invalidTags = { { "" }, { " " }, { "'" }, { "[]\\[;]" }, { "validTag", "" }, + { "", " " } }; + for (String[] tags : invalidTags) { + Set tagsToAdd = new HashSet<>(Arrays.asList(tags)); + assertConstructingInvalidUpdateCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, + true, Address.EXAMPLE, false, tagsToAdd); + } + } + + /** + * Asserts that attempting to construct an update command with the supplied + * invalid data throws an IllegalValueException + */ + private void assertConstructingInvalidUpdateCmdThrowsException(String name, String phone, + boolean isPhonePrivate, String email, boolean isEmailPrivate, String address, + boolean isAddressPrivate, Set tags) { + try { + new UpdateCommand(name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate, + tags); + } catch (IllegalValueException e) { + return; + } + String error = String.format( + "An Update command was successfully constructed with invalid input: %s %s %s %s %s %s %s %s", + name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate, tags); + fail(error); + } + + @Test + public void updateCommand_validData_correctlyConstructed() throws Exception { + UpdateCommand command = new UpdateCommand(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, false, + Address.EXAMPLE, true, EMPTY_STRING_LIST); + ReadOnlyPerson p = command.getPerson(); + + // TODO: add comparison of tags to person.equals and equality methods to + // individual fields that compare privacy to simplify this + assertEquals(Name.EXAMPLE, p.getName().fullName); + assertEquals(Phone.EXAMPLE, p.getPhone().value); + assertTrue(p.getPhone().isPrivate()); + assertEquals(Email.EXAMPLE, p.getEmail().value); + assertFalse(p.getEmail().isPrivate()); + assertEquals(Address.EXAMPLE, p.getAddress().value); + assertTrue(p.getAddress().isPrivate()); + boolean isTagListEmpty = !p.getTags().iterator().hasNext(); + assertTrue(isTagListEmpty); + } + + @Test + public void updateCommand_emptyAddressBook() { + Person p = TestUtil.generateTestPerson(); + UpdateCommand command = new UpdateCommand(p); + AddressBook book = new AddressBook(); + command.setData(book, EMPTY_PERSON_LIST); + CommandResult result = command.execute(); + + assertEquals(String.format(UpdateCommand.MESSAGE_NONEXIST_PERSON), result.feedbackToUser); + } + +} diff --git a/test/java/seedu/addressbook/data/AddressBookTest.java b/test/java/seedu/addressbook/data/AddressBookTest.java index 79808d80b..1f01ebe63 100644 --- a/test/java/seedu/addressbook/data/AddressBookTest.java +++ b/test/java/seedu/addressbook/data/AddressBookTest.java @@ -117,6 +117,28 @@ public void addPerson_personAlreadyInListButHasTagNotInList_tagNotAdded() throws assertFalse(isTagObjectInAddressBookList(tagPrizeWinner, defaultAddressBook)); } + + @Test + public void updatePerson_someTagsNotInTagList() throws Exception { + + davidElliot.setTags(new UniqueTagList(tagMathematician)); + defaultAddressBook.addPerson(davidElliot); + assertFalse(isTagObjectInAddressBookList(tagEconomist, defaultAddressBook)); + assertFalse(isTagObjectInAddressBookList(tagPrizeWinner, defaultAddressBook)); + + davidElliot.setTags(new UniqueTagList(tagEconomist, tagPrizeWinner)); + defaultAddressBook.updatePerson(davidElliot); + + assertTrue(isTagObjectInAddressBookList(tagEconomist, defaultAddressBook)); + assertTrue(isTagObjectInAddressBookList(tagPrizeWinner, defaultAddressBook)); + } + + @Test + public void updatePerson_personNotExistInList_throwsPersonNonExistException() throws Exception { + thrown.expect(AddressBook.PersonNonExistException.class); + emptyAddressBook.updatePerson(aliceBetsy); + } + @Test public void containsPerson() throws Exception { UniquePersonList personsWhoShouldBeIn = new UniquePersonList(aliceBetsy, bobChaplin); diff --git a/test/java/seedu/addressbook/parser/ParserTest.java b/test/java/seedu/addressbook/parser/ParserTest.java index 0704b23d2..1168e7ed8 100644 --- a/test/java/seedu/addressbook/parser/ParserTest.java +++ b/test/java/seedu/addressbook/parser/ParserTest.java @@ -12,17 +12,7 @@ import org.junit.Before; import org.junit.Test; -import seedu.addressbook.commands.AddCommand; -import seedu.addressbook.commands.ClearCommand; -import seedu.addressbook.commands.Command; -import seedu.addressbook.commands.DeleteCommand; -import seedu.addressbook.commands.ExitCommand; -import seedu.addressbook.commands.FindCommand; -import seedu.addressbook.commands.HelpCommand; -import seedu.addressbook.commands.IncorrectCommand; -import seedu.addressbook.commands.ListCommand; -import seedu.addressbook.commands.ViewAllCommand; -import seedu.addressbook.commands.ViewCommand; +import seedu.addressbook.commands.*; import seedu.addressbook.data.exception.IllegalValueException; import seedu.addressbook.data.person.Address; import seedu.addressbook.data.person.Email; @@ -295,6 +285,77 @@ private static String convertPersonToAddCommandString(ReadOnlyPerson person) { return addCommand; } + /* + * Tests for update person command ============================================================================== + */ + + @Test + public void parse_updateCommandInvalidArgs_errorMessage() { + final String[] inputs = { + "update", + "update ", + "update wrong args format", + // no phone prefix + String.format("update $s $s e/$s a/$s", Name.EXAMPLE, Phone.EXAMPLE, Email.EXAMPLE, Address.EXAMPLE), + // no email prefix + String.format("update $s p/$s $s a/$s", Name.EXAMPLE, Phone.EXAMPLE, Email.EXAMPLE, Address.EXAMPLE), + // no address prefix + String.format("update $s p/$s e/$s $s", Name.EXAMPLE, Phone.EXAMPLE, Email.EXAMPLE, Address.EXAMPLE) + }; + final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, UpdateCommand.MESSAGE_USAGE); + parseAndAssertIncorrectWithMessage(resultMessage, inputs); + } + + @Test + public void parse_updateCommandInvalidPersonDataInArgs_errorMessge() { + final String invalidName = "[]\\[;]"; + final String validName = Name.EXAMPLE; + final String invalidPhoneArg = "p/not__numbers"; + final String validPhoneArg = "p/" + Phone.EXAMPLE; + final String invalidEmailArg = "e/notAnEmail123"; + final String validEmailArg = "e/" + Email.EXAMPLE; + final String invalidTagArg = "t/invalid_-[.tag"; + + // address can be any string, so no invalid address + final String updateCommandFormatString = "update $s $s $s a/" + Address.EXAMPLE; + + // test each incorrect person data field argument individually + final String[] inputs = { + // invalid name + String.format(updateCommandFormatString, invalidName, validPhoneArg, validEmailArg), + // invalid phone + String.format(updateCommandFormatString, validName, invalidPhoneArg, validEmailArg), + // invalid email + String.format(updateCommandFormatString, validName, validPhoneArg, invalidEmailArg), + // invalid tag + String.format(updateCommandFormatString, validName, validPhoneArg, validEmailArg) + " " + invalidTagArg + }; + for (String input : inputs) { + parseAndAssertCommandType(input, IncorrectCommand.class); + } + } + + @Test + public void parse_updateCommandValidPersonData_parsedCorrectly() { + final Person testPerson = generateTestPerson(); + final String input = convertPersonToUpdateCommandString(testPerson); + final UpdateCommand result = parseAndAssertCommandType(input, UpdateCommand.class); + assertEquals(result.getPerson(), testPerson); + } + + + private static String convertPersonToUpdateCommandString(ReadOnlyPerson person) { + String addCommand = "update " + + person.getName().fullName + + (person.getPhone().isPrivate() ? " pp/" : " p/") + person.getPhone().value + + (person.getEmail().isPrivate() ? " pe/" : " e/") + person.getEmail().value + + (person.getAddress().isPrivate() ? " pa/" : " a/") + person.getAddress().value; + for (Tag tag : person.getTags()) { + addCommand += " t/" + tag.tagName; + } + return addCommand; + } + /* * Utility methods ==================================================================================== */ From d00255e99e4d9317da39a812059e42b3a94f990b Mon Sep 17 00:00:00 2001 From: Wang Junming Date: Sun, 10 Sep 2017 15:55:26 +0800 Subject: [PATCH 8/8] Include updatetesting in I/O tests and update corresponding expected output --- test/expected.txt | 145 +++++++++++++++++++++++++++++++++++++++------- test/input.txt | 33 +++++++++++ 2 files changed, 158 insertions(+), 20 deletions(-) diff --git a/test/expected.txt b/test/expected.txt index 84f7f8bf3..bbf0f99c0 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -9,6 +9,9 @@ || add: Adds a person to the address book. Contact details can be marked private by prepending 'p' to the prefix. || Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... || Example: add John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney +|| update: updates a person's info in the address book. Contact details can be marked private by prepending 'p' to the prefix. +|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... +|| Example: update John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney || delete: Deletes the person identified by the index number used in the last person listing. || Parameters: INDEX || Example: delete 1 @@ -138,6 +141,108 @@ || Enter command: || [Command entered: add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy] || This person already exists in the address book || =================================================== +|| Enter command: || [Command entered: update wrong args wrong args] +|| Invalid command format! +|| update: updates a person's info in the address book. Contact details can be marked private by prepending 'p' to the prefix. +|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... +|| Example: update John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney +|| =================================================== +|| Enter command: || [Command entered: update Valid Name 12345 e/valid@email.butNoPhonePrefix a/valid, address] +|| Invalid command format! +|| update: updates a person's info in the address book. Contact details can be marked private by prepending 'p' to the prefix. +|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... +|| Example: update John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney +|| =================================================== +|| Enter command: || [Command entered: update Valid Name p/12345 valid@email.butNoPrefix a/valid, address] +|| Invalid command format! +|| update: updates a person's info in the address book. Contact details can be marked private by prepending 'p' to the prefix. +|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... +|| Example: update John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney +|| =================================================== +|| Enter command: || [Command entered: update Valid Name p/12345 e/valid@email.butNoAddressPrefix valid, address] +|| Invalid command format! +|| update: updates a person's info in the address book. Contact details can be marked private by prepending 'p' to the prefix. +|| Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]... +|| Example: update John Doe p/98765432 e/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney +|| =================================================== +|| Enter command: || [Command entered: update Valid Name p/12345 e/valid@email.butNoTagPrefix a/valid, address t/goodTag noPrefixTag] +|| Tags names should be alphanumeric +|| =================================================== +|| Enter command: || [Command entered: update []\[;] p/12345 e/valid@e.mail a/valid, address] +|| Person names should be spaces or alphabetic characters +|| =================================================== +|| Enter command: || [Command entered: update Valid Name p/not_numbers e/valid@e.mail a/valid, address] +|| Person phone numbers should only contain numbers +|| =================================================== +|| Enter command: || [Command entered: update Valid Name p/12345 e/notAnEmail a/valid, address] +|| Person emails should be 2 alphanumeric/period strings separated by '@' +|| =================================================== +|| Enter command: || [Command entered: update Valid Name p/12345 e/valid@e.mail a/valid, address t/invalid_-[.tag] +|| Tags names should be alphanumeric +|| =================================================== +|| Enter command: || [Command entered: update Adam Brown p/121111 e/adamchanged@gmail.com a/111, alpha street] +|| person info updated: Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: +|| =================================================== +|| Enter command: || [Command entered: list] +|| 1. Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: +|| 2. Betsy Choo Tags: [secretive] +|| 3. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 5. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| +|| 5 persons listed! +|| =================================================== +|| Enter command: || [Command entered: update Betsy Choo pp/212222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive] +|| person info updated: Betsy Choo Phone: (private) 212222 Email: (private) benchoo@nus.edu.sg Address: (private) 222, beta street Tags: [secretive] +|| =================================================== +|| Enter command: || [Command entered: list] +|| 1. Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: +|| 2. Betsy Choo Tags: [secretive] +|| 3. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 5. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| +|| 5 persons listed! +|| =================================================== +|| Enter command: || [Command entered: update Charlie Dickson pp/333233 e/charlie.d@nus.edu.sg a/3323, gamma street t/friends t/school] +|| person info updated: Charlie Dickson Phone: (private) 333233 Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] +|| =================================================== +|| Enter command: || [Command entered: list] +|| 1. Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: +|| 2. Betsy Choo Tags: [secretive] +|| 3. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] +|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 5. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| +|| 5 persons listed! +|| =================================================== +|| Enter command: || [Command entered: update Dickson Ee p/544444 pe/dickson@nus.edu.sg pa/444, delta street t/friends] +|| person info updated: Dickson Ee Phone: 544444 Email: (private) dickson@nus.edu.sg Address: (private) 444, delta street Tags: [friends] +|| =================================================== +|| Enter command: || [Command entered: list] +|| 1. Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: +|| 2. Betsy Choo Tags: [secretive] +|| 3. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] +|| 4. Dickson Ee Phone: 544444 Tags: [friends] +|| 5. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| +|| 5 persons listed! +|| =================================================== +|| Enter command: || [Command entered: update Esther Potato p/955555 e/esther@not.a.real.potato pa/5515, epsilon street t/tubers t/starchy] +|| person info updated: Esther Potato Phone: 955555 Email: esther@not.a.real.potato Address: (private) 5515, epsilon street Tags: [tubers][starchy] +|| =================================================== +|| Enter command: || [Command entered: list] +|| 1. Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: +|| 2. Betsy Choo Tags: [secretive] +|| 3. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] +|| 4. Dickson Ee Phone: 544444 Tags: [friends] +|| 5. Esther Potato Phone: 955555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| +|| 5 persons listed! +|| =================================================== +|| Enter command: || [Command entered: update unknown Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy] +|| This person does not exist in the AddressBook +|| =================================================== || Enter command: || [Command entered: view] || Invalid command format! || view: Views the non-private details of the person identified by the index number in the last shown person listing. @@ -175,28 +280,28 @@ || The person index provided is invalid || =================================================== || Enter command: || [Command entered: view 1] -|| Viewing person: Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| Viewing person: Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: || =================================================== || Enter command: || [Command entered: viewall 1] -|| Viewing person: Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| Viewing person: Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: || =================================================== || Enter command: || [Command entered: view 3] -|| Viewing person: Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| Viewing person: Charlie Dickson Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] || =================================================== || Enter command: || [Command entered: view 4] -|| Viewing person: Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| Viewing person: Dickson Ee Phone: 544444 Tags: [friends] || =================================================== || Enter command: || [Command entered: view 5] -|| Viewing person: Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| Viewing person: Esther Potato Phone: 955555 Email: esther@not.a.real.potato Tags: [tubers][starchy] || =================================================== || Enter command: || [Command entered: viewall 3] -|| Viewing person: Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| Viewing person: Charlie Dickson Phone: (private) 333233 Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] || =================================================== || Enter command: || [Command entered: viewall 4] -|| Viewing person: Dickson Ee Phone: 444444 Email: (private) dickson@nus.edu.sg Address: 444, delta street Tags: [friends] +|| Viewing person: Dickson Ee Phone: 544444 Email: (private) dickson@nus.edu.sg Address: (private) 444, delta street Tags: [friends] || =================================================== || Enter command: || [Command entered: viewall 5] -|| Viewing person: Esther Potato Phone: 555555 Email: esther@not.a.real.potato Address: (private) 555, epsilon street Tags: [tubers][starchy] +|| Viewing person: Esther Potato Phone: 955555 Email: esther@not.a.real.potato Address: (private) 5515, epsilon street Tags: [tubers][starchy] || =================================================== || Enter command: || [Command entered: find] || Invalid command format! @@ -222,14 +327,14 @@ || 1 persons listed! || =================================================== || Enter command: || [Command entered: find Dickson] -|| 1. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] -|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 1. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] +|| 2. Dickson Ee Phone: 544444 Tags: [friends] || || 2 persons listed! || =================================================== || Enter command: || [Command entered: find Charlie Betsy] || 1. Betsy Choo Tags: [secretive] -|| 2. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| 2. Charlie Dickson Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] || || 2 persons listed! || =================================================== @@ -252,7 +357,7 @@ || The person index provided is invalid || =================================================== || Enter command: || [Command entered: delete 2] -|| Deleted Person: Charlie Dickson Phone: (private) 333333 Email: charlie.d@nus.edu.sg Address: 333, gamma street Tags: [school][friends] +|| Deleted Person: Charlie Dickson Phone: (private) 333233 Email: charlie.d@nus.edu.sg Address: 3323, gamma street Tags: [school][friends] || =================================================== || Enter command: || [Command entered: delete 2] || Person could not be found in address book @@ -264,29 +369,29 @@ || Person could not be found in address book || =================================================== || Enter command: || [Command entered: list] -|| 1. Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| 1. Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: || 2. Betsy Choo Tags: [secretive] -|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] -|| 4. Esther Potato Phone: 555555 Email: esther@not.a.real.potato Tags: [tubers][starchy] +|| 3. Dickson Ee Phone: 544444 Tags: [friends] +|| 4. Esther Potato Phone: 955555 Email: esther@not.a.real.potato Tags: [tubers][starchy] || || 4 persons listed! || =================================================== || Enter command: || [Command entered: delete 4] -|| Deleted Person: Esther Potato Phone: 555555 Email: esther@not.a.real.potato Address: (private) 555, epsilon street Tags: [tubers][starchy] +|| Deleted Person: Esther Potato Phone: 955555 Email: esther@not.a.real.potato Address: (private) 5515, epsilon street Tags: [tubers][starchy] || =================================================== || Enter command: || [Command entered: list] -|| 1. Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| 1. Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: || 2. Betsy Choo Tags: [secretive] -|| 3. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 3. Dickson Ee Phone: 544444 Tags: [friends] || || 3 persons listed! || =================================================== || Enter command: || [Command entered: delete 1] -|| Deleted Person: Adam Brown Phone: 111111 Email: adam@gmail.com Address: 111, alpha street Tags: +|| Deleted Person: Adam Brown Phone: 121111 Email: adamchanged@gmail.com Address: 111, alpha street Tags: || =================================================== || Enter command: || [Command entered: list] || 1. Betsy Choo Tags: [secretive] -|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] +|| 2. Dickson Ee Phone: 544444 Tags: [friends] || || 2 persons listed! || =================================================== diff --git a/test/input.txt b/test/input.txt index 5ecae97f8..a936e70c5 100644 --- a/test/input.txt +++ b/test/input.txt @@ -54,6 +54,39 @@ # should not allow adding duplicate persons add Esther Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy +########################################################## +# test update person command, setup state for futuer tests +########################################################## + + # should catch invalid args format + update wrong args wrong args + update Valid Name 12345 e/valid@email.butNoPhonePrefix a/valid, address + update Valid Name p/12345 valid@email.butNoPrefix a/valid, address + update Valid Name p/12345 e/valid@email.butNoAddressPrefix valid, address + update Valid Name p/12345 e/valid@email.butNoTagPrefix a/valid, address t/goodTag noPrefixTag + + # should catch invalid person data + update []\[;] p/12345 e/valid@e.mail a/valid, address + update Valid Name p/not_numbers e/valid@e.mail a/valid, address + update Valid Name p/12345 e/notAnEmail a/valid, address + update Valid Name p/12345 e/valid@e.mail a/valid, address t/invalid_-[.tag + + # should update correctly and list non private information + update Adam Brown p/121111 e/adamchanged@gmail.com a/111, alpha street + list + update Betsy Choo pp/212222 pe/benchoo@nus.edu.sg pa/222, beta street t/secretive + list + update Charlie Dickson pp/333233 e/charlie.d@nus.edu.sg a/3323, gamma street t/friends t/school + list + update Dickson Ee p/544444 pe/dickson@nus.edu.sg pa/444, delta street t/friends + list + update Esther Potato p/955555 e/esther@not.a.real.potato pa/5515, epsilon street t/tubers t/starchy + list + + # should not allow updating Non-exist persons + update unknown Potato p/555555 e/esther@not.a.real.potato pa/555, epsilon street t/tubers t/starchy + + ########################################################## # test view/viewall persons command ##########################################################