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][W17-1]Chen Caijie #143
Open
rbth7e5
wants to merge
4
commits into
nus-cs2103-AY1819S2:master
Choose a base branch
from
rbth7e5:edit
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 3 commits
Commits
Show all changes
4 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 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,16 @@ 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` | ||
|
||
== Editing a person: `edit` | ||
|
||
Edits a person in the address book. + | ||
Format: `edit NAME n/NEW_NAME` | ||
|
||
Example: | ||
|
||
* `edit John Doe n/Johnny Doe` + | ||
Edits John Doe's name to Johnny Doe | ||
|
||
== 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.*; | ||
|
||
public class EditCommand extends Command { | ||
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. Including a javadoc comment for new classes would be good. |
||
public static final String COMMAND_WORD = "edit"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a person's name in the address book. " | ||
+ "Parameters: NAME n/NEW_NAME\n" | ||
+ "Example: " + COMMAND_WORD | ||
+ " John Doe n/Johnny Doe"; | ||
|
||
public static final String MESSAGE_SUCCESS = "%1$s is now called %2$s"; | ||
public static final String MESSAGE_MISSING_PERSON = "This person does not exist in the address book"; | ||
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book"; | ||
|
||
private final Name toEdit; | ||
private final Name toReplace; | ||
|
||
/** | ||
* Convenience constructor using raw values. | ||
* | ||
* @throws IllegalValueException if any of the raw values are invalid | ||
*/ | ||
public EditCommand(String name, String replacement) throws IllegalValueException { | ||
this.toEdit = new Name(name); | ||
this.toReplace = new Name(replacement); | ||
} | ||
|
||
public EditCommand(Name toEdit, Name toReplace) { | ||
this.toEdit = toEdit; | ||
this.toReplace = toReplace; | ||
} | ||
|
||
public Name getName() { | ||
return toEdit; | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
try { | ||
Person personToEdit = addressBook.getPerson(toEdit); | ||
Person editedPerson = new Person( | ||
toReplace, | ||
personToEdit.getPhone(), | ||
personToEdit.getEmail(), | ||
personToEdit.getAddress(), | ||
personToEdit.getTags() | ||
); | ||
addressBook.removePerson(personToEdit); | ||
addressBook.addPerson(editedPerson); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, toEdit, toReplace)); | ||
} catch (UniquePersonList.PersonNotFoundException pnfe) { | ||
return new CommandResult(MESSAGE_MISSING_PERSON); | ||
} catch (UniquePersonList.DuplicatePersonException dpe) { | ||
return new CommandResult(MESSAGE_DUPLICATE_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
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import java.util.regex.Pattern; | ||
|
||
import seedu.addressbook.commands.AddCommand; | ||
import seedu.addressbook.commands.EditCommand; | ||
import seedu.addressbook.commands.ClearCommand; | ||
import seedu.addressbook.commands.Command; | ||
import seedu.addressbook.commands.DeleteCommand; | ||
|
@@ -41,6 +42,10 @@ public class Parser { | |
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)" | ||
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags | ||
|
||
public static final Pattern PERSON_EDIT_ARGS_FORMAT = | ||
Pattern.compile("(?<name>[^/]+)" | ||
+ " n/(?<newName>[^/]+)"); | ||
|
||
|
||
/** | ||
* Signals that the user input could not be parsed. | ||
|
@@ -73,33 +78,36 @@ public Command parseCommand(String userInput) { | |
|
||
switch (commandWord) { | ||
|
||
case AddCommand.COMMAND_WORD: | ||
return prepareAdd(arguments); | ||
case AddCommand.COMMAND_WORD: | ||
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. Note that the coding style specifies that there shouldn't be indentation for case clauses. |
||
return prepareAdd(arguments); | ||
|
||
case DeleteCommand.COMMAND_WORD: | ||
return prepareDelete(arguments); | ||
case EditCommand.COMMAND_WORD: | ||
return prepareEdit(arguments); | ||
|
||
case ClearCommand.COMMAND_WORD: | ||
return new ClearCommand(); | ||
case DeleteCommand.COMMAND_WORD: | ||
return prepareDelete(arguments); | ||
|
||
case FindCommand.COMMAND_WORD: | ||
return prepareFind(arguments); | ||
case ClearCommand.COMMAND_WORD: | ||
return new ClearCommand(); | ||
|
||
case ListCommand.COMMAND_WORD: | ||
return new ListCommand(); | ||
case FindCommand.COMMAND_WORD: | ||
return prepareFind(arguments); | ||
|
||
case ViewCommand.COMMAND_WORD: | ||
return prepareView(arguments); | ||
case ListCommand.COMMAND_WORD: | ||
return new ListCommand(); | ||
|
||
case ViewAllCommand.COMMAND_WORD: | ||
return prepareViewAll(arguments); | ||
case ViewCommand.COMMAND_WORD: | ||
return prepareView(arguments); | ||
|
||
case ExitCommand.COMMAND_WORD: | ||
return new ExitCommand(); | ||
case ViewAllCommand.COMMAND_WORD: | ||
return prepareViewAll(arguments); | ||
|
||
case HelpCommand.COMMAND_WORD: // Fallthrough | ||
default: | ||
return new HelpCommand(); | ||
case ExitCommand.COMMAND_WORD: | ||
return new ExitCommand(); | ||
|
||
case HelpCommand.COMMAND_WORD: // Fallthrough | ||
default: | ||
return new HelpCommand(); | ||
} | ||
} | ||
|
||
|
@@ -156,6 +164,23 @@ private static Set<String> getTagsFromArgs(String tagArguments) throws IllegalVa | |
return new HashSet<>(tagStrings); | ||
} | ||
|
||
private Command prepareEdit(String args) { | ||
final Matcher matcher = PERSON_EDIT_ARGS_FORMAT.matcher(args.trim()); | ||
// Validate arg string format | ||
if (!matcher.matches()) { | ||
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE)); | ||
} | ||
try { | ||
return new EditCommand( | ||
matcher.group("name"), | ||
|
||
matcher.group("newName") | ||
); | ||
} catch (IllegalValueException ive) { | ||
return new IncorrectCommand(ive.getMessage()); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Parses arguments in the context of the delete person command. | ||
|
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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
|| 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/[email protected] a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney | ||
|| edit: Edits a person's name in the address book. Parameters: NAME n/NEW_NAME | ||
|| Example: edit John Doe n/Johnny Doe | ||
|| delete: Deletes the person identified by the index number used in the last person listing. | ||
|| Parameters: INDEX | ||
|| Example: delete 1 | ||
|
@@ -138,6 +140,43 @@ | |
|| Enter command: || [Command entered: add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy] | ||
|| This person already exists in the address book | ||
|| =================================================== | ||
|| Enter command: || [Command entered: edit wrong args wrong args] | ||
|| Invalid command format! | ||
|| edit: Edits a person's name in the address book. Parameters: NAME n/NEW_NAME | ||
|| Example: edit John Doe n/Johnny Doe | ||
|| =================================================== | ||
|| Enter command: || [Command entered: edit Valid Name Invalid Replacement] | ||
|| Invalid command format! | ||
|| edit: Edits a person's name in the address book. Parameters: NAME n/NEW_NAME | ||
|| Example: edit John Doe n/Johnny Doe | ||
|| =================================================== | ||
|| Enter command: || [Command entered: edit Esther Potato n/Esther Tomato] | ||
|| Esther Potato is now called Esther Tomato | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| 2. Betsy Choo Tags: [secretive] | ||
|| 3. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 5. Esther Tomato Phone: 555555 Email: [email protected] Tags: [tubers][starchy] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: edit Esther Tomato n/Esther Potato] | ||
|| Esther Tomato is now called Esther Potato | ||
|| =================================================== | ||
|| Enter command: || [Command entered: list] | ||
|| 1. Adam Brown Phone: 111111 Email: [email protected] Address: 111, alpha street Tags: | ||
|| 2. Betsy Choo Tags: [secretive] | ||
|| 3. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends] | ||
|| 4. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends] | ||
|| 5. Esther Potato Phone: 555555 Email: [email protected] Tags: [tubers][starchy] | ||
|| | ||
|| 5 persons listed! | ||
|| =================================================== | ||
|| Enter command: || [Command entered: edit Nonexistent Person n/Existent Person] | ||
|| This person does not exist in the address book | ||
|| =================================================== | ||
|| 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. | ||
|
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 |
---|---|---|
|
@@ -54,6 +54,23 @@ | |
# should not allow adding duplicate persons | ||
add Esther Potato p/555555 e/[email protected] pa/555, epsilon street t/tubers t/starchy | ||
|
||
########################################################## | ||
# test edit person command | ||
########################################################## | ||
|
||
# should catch invalid args format | ||
edit wrong args wrong args | ||
edit Valid Name Invalid Replacement | ||
|
||
# should edit correctly and list non private information | ||
edit Esther Potato n/Esther Tomato | ||
list | ||
edit Esther Tomato n/Esther Potato | ||
list | ||
|
||
# should not allow editing person that does not exist | ||
edit Nonexistent Person n/Existent Person | ||
|
||
########################################################## | ||
# test view/viewall persons command | ||
########################################################## | ||
|
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,41 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import org.junit.Test; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.Name; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
public class EditCommandTest { | ||
@Test | ||
public void editCommand_invalidName_throwsException() { | ||
final String[] invalidNames = { "", " ", "[]\\[;]" }; | ||
for (String name : invalidNames) { | ||
assertConstructingInvalidEditCmdThrowsException(name, Name.EXAMPLE); | ||
} | ||
} | ||
|
||
@Test | ||
public void editCommand_invalidReplacement_throwsException() { | ||
final String[] invalidNames = { "", " ", "[]\\[;]" }; | ||
for (String replacement : invalidNames) { | ||
assertConstructingInvalidEditCmdThrowsException(Name.EXAMPLE, replacement); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that attempting to construct an edit command with the supplied | ||
* invalid data throws an IllegalValueException | ||
*/ | ||
private void assertConstructingInvalidEditCmdThrowsException(String name, String replacement) { | ||
try { | ||
new EditCommand(name, replacement); | ||
} catch (IllegalValueException e) { | ||
return; | ||
} | ||
String error = String.format( | ||
"An edit command was successfully constructed with invalid input: %s %s", | ||
name, replacement); | ||
fail(error); | ||
} | ||
} |
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
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.
Try to be explicit in your imports, avoiding the use of
.*
.