forked from se-edu/addressbook-level2
-
Notifications
You must be signed in to change notification settings - Fork 113
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
[W5][W13-4]Chua Eng Soon #148
Open
chuaes
wants to merge
10
commits into
nus-cs2103-AY1819S2:master
Choose a base branch
from
chuaes: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
10 commits
Select commit
Hold shift + click to select a range
7fc3868
create necessary class variables for updateCommand
chuaes 5da60d2
add the execute method in UpdateCommand to facilitate update
chuaes aba4b5e
provides updatePerson method so that allPersons are updated
chuaes a1e82cb
modify the help command list to include the update feature
chuaes 156feb0
remove unwanted import statement
chuaes 8c2fa4f
add update method to allows the updated info to reflected in list
chuaes db49267
add update format
chuaes 24cd89f
add the update feature in the user guide
chuaes 281b545
add the test for update command
chuaes 8c355ca
minor change to import statements
chuaes 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 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 |
---|---|---|
|
@@ -67,6 +67,26 @@ Examples: | |
* `add John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01` | ||
* `add Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/criminal t/friend` | ||
|
||
== Updates a person's info: `update` | ||
|
||
Updates a person 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/[email protected] a/John street, block 123, #01-01` | ||
* `update Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/criminal t/friend` | ||
|
||
== Listing all persons : `list` | ||
|
||
Shows a list of all persons, along with their non-private details, in the address book. + | ||
|
This file contains 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 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,78 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import java.util.HashSet; | ||
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; | ||
|
||
/** | ||
* Updates existing person to the address book. | ||
*/ | ||
public class UpdateCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "update"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Updates existing person to 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/[email protected] a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Person updated: %1$s"; | ||
public static final String MESSAGE_NO_PERSON = "This person does not exist in the address book"; | ||
|
||
private final Person toUpdate; | ||
private final String NAME; | ||
|
||
/** | ||
* 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<String> tags) throws IllegalValueException { | ||
final Set<Tag> tagSet = new HashSet<>(); | ||
for (String tagName : tags) { | ||
tagSet.add(new Tag(tagName)); | ||
} | ||
this.NAME = name; | ||
this.toUpdate = new Person( | ||
new Name(name), | ||
new Phone(phone, isPhonePrivate), | ||
new Email(email, isEmailPrivate), | ||
new Address(address, isAddressPrivate), | ||
tagSet | ||
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. Indent 4 spaces each time? |
||
); | ||
} | ||
|
||
public UpdateCommand(Person toUpdate) { | ||
this.toUpdate = toUpdate; | ||
this.NAME = toUpdate.getName().toString(); | ||
} | ||
|
||
public ReadOnlyPerson getPerson() { | ||
return toUpdate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
try { | ||
addressBook.updatePerson(toUpdate); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, toUpdate)); | ||
} catch (UniquePersonList.PersonNotFoundException npe) { | ||
return new CommandResult(MESSAGE_NO_PERSON); | ||
} | ||
} | ||
|
||
} |
This file contains 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 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 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
116 changes: 116 additions & 0 deletions
116
test/java/seedu/addressbook/commands/UpdateCommandTest.java
This file contains 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,116 @@ | ||
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<ReadOnlyPerson> EMPTY_PERSON_LIST = Collections.emptyList(); | ||
private static final Set<String> EMPTY_STRING_SET = 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_SET); | ||
} | ||
} | ||
|
||
@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_SET); | ||
} | ||
} | ||
|
||
@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_SET); | ||
} | ||
} | ||
|
||
@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_SET); | ||
} | ||
} | ||
|
||
@Test | ||
public void updateCommand_invalidTags_throwsException() { | ||
final String[][] invalidTags = {{""}, {" "}, {"'"}, {"[]\\[;]"}, {"validTag", ""}, | ||
{"", " "}}; | ||
for (String[] tags : invalidTags) { | ||
Set<String> 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<String> tags) { | ||
try { | ||
new AddCommand(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 updateCommand_validData_correctlyConstructed() throws Exception { | ||
UpdateCommand command = new UpdateCommand(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); | ||
} | ||
} |
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.
Indentation is a bit strange here - either align the parameters vertically or keep everything to the left one indent in from the start of the declaration, maybe.