Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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. +
Expand Down
60 changes: 60 additions & 0 deletions src/seedu/addressbook/commands/EditCommand.java
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.*;
Copy link

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 .*.


public class EditCommand extends Command {
Copy link

Choose a reason for hiding this comment

The 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);
}
}
}
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class HelpCommand extends Command {
public CommandResult execute() {
return new CommandResult(
AddCommand.MESSAGE_USAGE
+ "\n" + EditCommand.MESSAGE_USAGE
+ "\n" + DeleteCommand.MESSAGE_USAGE
+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.addressbook.data;

import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
Expand Down Expand Up @@ -38,6 +39,10 @@ public void addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
}

public Person getPerson(Name name) throws PersonNotFoundException {
return allPersons.get(name);
}

/**
* Returns true if an equivalent person exists in the address book.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public void add(Person toAdd) throws DuplicatePersonException {
internalList.add(toAdd);
}

/**
* Get a person matching the input name
*
* @param name the name to be found
* @return the person found
* @throws PersonNotFoundException if no such person could be found in the list
*/
public Person get(Name name) throws PersonNotFoundException {
for (Person person: internalList) {
if (person.getName().equals(name)) {
return person;
}
}
throw new PersonNotFoundException();
}

/**
* Removes the equivalent person from the list.
*
Expand Down
63 changes: 44 additions & 19 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -73,33 +78,36 @@ public Command parseCommand(String userInput) {

switch (commandWord) {

case AddCommand.COMMAND_WORD:
return prepareAdd(arguments);
case AddCommand.COMMAND_WORD:
Copy link

Choose a reason for hiding this comment

The 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();
}
}

Expand Down Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
##########################################################
Expand Down
41 changes: 41 additions & 0 deletions test/java/seedu/addressbook/commands/EditCommandTest.java
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);
}
}
18 changes: 18 additions & 0 deletions test/java/seedu/addressbook/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.Test;

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;
Expand Down Expand Up @@ -294,6 +295,23 @@ private static String convertPersonToAddCommandString(ReadOnlyPerson person) {
return addCommand;
}

/*
* Tests for edit person command ==============================================================================
*/

@Test
public void parse_editCommandInvalidArgs_errorMessage() {
final String[] inputs = {
"edit",
"edit ",
"edit wrong args format",
// no name prefix
String.format("edit $s $s", Name.EXAMPLE, Name.EXAMPLE),
};
final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}

/*
* Utility methods ====================================================================================
*/
Expand Down