forked from se-edu/addressbook-level2
-
Notifications
You must be signed in to change notification settings - Fork 35
[W5] [T11-1] Windrich #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
windrichie
wants to merge
3
commits into
nusCS2113-AY1819S2:master
Choose a base branch
from
windrichie:W5
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package seedu.addressbook.commands; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| 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; | ||
|
|
||
| /* | ||
| Add a person to the addressbook and list down everyone in the addressbook right after | ||
| */ | ||
| public class AddListCommand extends Command{ | ||
| public static final String COMMAND_WORD = "add&list"; | ||
|
|
||
| public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book and list the whole address book after a successful add. " | ||
| + "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 = "New person added: %1$s"; | ||
| public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book"; | ||
|
|
||
| private final Person toAdd; | ||
|
|
||
| /** | ||
| * Convenience constructor using raw values. | ||
| * | ||
| * @throws IllegalValueException if any of the raw values are invalid | ||
| */ | ||
| public AddListCommand(String name, | ||
| String phone, boolean isPhonePrivate, | ||
| String email, boolean isEmailPrivate, | ||
| String address, boolean isAddressPrivate, | ||
| Set<String> tags) throws IllegalValueException { | ||
| final Set<Tag> tagSet = new HashSet<>(); | ||
| for (String tagName : tags) { | ||
| tagSet.add(new Tag(tagName)); | ||
| } | ||
| this.toAdd = new Person( | ||
| new Name(name), | ||
| new Phone(phone, isPhonePrivate), | ||
| new Email(email, isEmailPrivate), | ||
| new Address(address, isAddressPrivate), | ||
| tagSet | ||
| ); | ||
| } | ||
|
|
||
| public AddListCommand(Person toAdd) { | ||
| this.toAdd = toAdd; | ||
| } | ||
|
|
||
| public ReadOnlyPerson getPerson() { | ||
| return toAdd; | ||
| } | ||
|
|
||
| @Override | ||
| public CommandResult execute() { | ||
| try { | ||
| addressBook.addPerson(toAdd); | ||
| List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView(); | ||
| return new CommandResult(getMessageForPersonListShownSummary(allPersons), allPersons); | ||
| // return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an unnecessary inline comment. |
||
| } catch (UniquePersonList.DuplicatePersonException dpe) { | ||
| return new CommandResult(MESSAGE_DUPLICATE_PERSON); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import java.util.regex.Pattern; | ||
|
|
||
| import seedu.addressbook.commands.AddCommand; | ||
| import seedu.addressbook.commands.AddListCommand; | ||
| import seedu.addressbook.commands.ClearCommand; | ||
| import seedu.addressbook.commands.Command; | ||
| import seedu.addressbook.commands.DeleteCommand; | ||
|
|
@@ -76,6 +77,9 @@ public Command parseCommand(String userInput) { | |
| case AddCommand.COMMAND_WORD: | ||
| return prepareAdd(arguments); | ||
|
|
||
| case AddListCommand.COMMAND_WORD: | ||
| return prepareAddList(arguments); | ||
|
|
||
| case DeleteCommand.COMMAND_WORD: | ||
| return prepareDelete(arguments); | ||
|
|
||
|
|
@@ -135,6 +139,32 @@ private Command prepareAdd(String args) { | |
| } | ||
| } | ||
|
|
||
| private Command prepareAddList(String args) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing header comment. All non-trivial methods should have java doc format header comments. |
||
| 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, AddListCommand.MESSAGE_USAGE)); | ||
| } | ||
| try { | ||
| return new AddListCommand( | ||
| 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. | ||
| */ | ||
|
|
||
147 changes: 147 additions & 0 deletions
147
test/java/seedu/addressbook/commands/AddListCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| 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 AddListCommandTest { | ||
| private static final List<ReadOnlyPerson> EMPTY_PERSON_LIST = Collections.emptyList(); | ||
| private static final Set<String> EMPTY_STRING_SET = Collections.emptySet(); | ||
|
|
||
| @Test | ||
| public void addListCommand_invalidName_throwsException() { | ||
| final String[] invalidNames = { "", " ", "[]\\[;]" }; | ||
| for (String name : invalidNames) { | ||
| assertConstructingInvalidAddCmdThrowsException(name, Phone.EXAMPLE, true, Email.EXAMPLE, false, | ||
| Address.EXAMPLE, true, EMPTY_STRING_SET); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void addListCommand_invalidPhone_throwsException() { | ||
| final String[] invalidNumbers = { "", " ", "1234-5678", "[]\\[;]", "abc", "a123", "+651234" }; | ||
| for (String number : invalidNumbers) { | ||
| assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, number, false, Email.EXAMPLE, true, | ||
| Address.EXAMPLE, false, EMPTY_STRING_SET); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void addListCommand_invalidEmail_throwsException() { | ||
| final String[] invalidEmails = { "", " ", "def.com", "@", "@def", "@def.com", "abc@", | ||
| "@invalid@email", "invalid@email!", "!invalid@email" }; | ||
| for (String email : invalidEmails) { | ||
| assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, false, email, false, | ||
| Address.EXAMPLE, false, EMPTY_STRING_SET); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void addListCommand_invalidAddress_throwsException() { | ||
| final String[] invalidAddresses = { "", " " }; | ||
| for (String address : invalidAddresses) { | ||
| assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, | ||
| true, address, true, EMPTY_STRING_SET); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void addListCommand_invalidTags_throwsException() { | ||
| final String[][] invalidTags = { { "" }, { " " }, { "'" }, { "[]\\[;]" }, { "validTag", "" }, | ||
| { "", " " } }; | ||
| for (String[] tags : invalidTags) { | ||
| Set<String> tagsToAdd = new HashSet<>(Arrays.asList(tags)); | ||
| assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, | ||
| true, Address.EXAMPLE, false, tagsToAdd); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that attempting to construct an add command with the supplied | ||
| * invalid data throws an IllegalValueException | ||
| */ | ||
| private void assertConstructingInvalidAddCmdThrowsException(String name, String phone, | ||
| boolean isPhonePrivate, String email, boolean isEmailPrivate, String address, | ||
| boolean isAddressPrivate, Set<String> tags) { | ||
| try { | ||
| new AddListCommand(name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate, | ||
| tags); | ||
| } catch (IllegalValueException e) { | ||
| return; | ||
| } | ||
| String error = String.format( | ||
| "An add 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 addListCommand_validData_correctlyConstructed() throws Exception { | ||
| AddListCommand command = new AddListCommand(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, false, | ||
| Address.EXAMPLE, true, EMPTY_STRING_SET); | ||
| 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 addListCommand_emptyAddressBook_addressBookContainsPerson() { | ||
| Person p = TestUtil.generateTestPerson(); | ||
| AddListCommand command = new AddListCommand(p); | ||
| AddressBook book = new AddressBook(); | ||
| command.setData(book, EMPTY_PERSON_LIST); | ||
| CommandResult result = command.execute(); | ||
| UniquePersonList people = book.getAllPersons(); | ||
|
|
||
| assertTrue(people.contains(p)); | ||
| assertEquals(1, people.immutableListView().size()); | ||
| // assertFalse(result.getRelevantPersons().isPresent()); | ||
| // assertEquals(String.format(AddListCommand.MESSAGE_SUCCESS, p), result.feedbackToUser); | ||
| } | ||
|
|
||
| @Test | ||
| public void AddListCommand_addressBookAlreadyContainsPerson_addressBookUnmodified() throws Exception { | ||
| Person p = TestUtil.generateTestPerson(); | ||
| AddressBook book = new AddressBook(); | ||
| book.addPerson(p); | ||
| AddListCommand command = new AddListCommand(p); | ||
| command.setData(book, EMPTY_PERSON_LIST); | ||
| CommandResult result = command.execute(); | ||
|
|
||
| assertFalse(result.getRelevantPersons().isPresent()); | ||
| assertEquals(AddListCommand.MESSAGE_DUPLICATE_PERSON, result.feedbackToUser); | ||
| UniquePersonList people = book.getAllPersons(); | ||
| assertTrue(people.contains(p)); | ||
| assertEquals(1, people.immutableListView().size()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing header comment. All non-trivial methods should have java doc format header comments.