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][W15-4]Cho Ming En #154

Open
wants to merge 1 commit 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
7 changes: 5 additions & 2 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ Returns Any person having names `Betsy`, `Tim`, or `John`.
== Deleting a person : `delete`

Deletes the specified person from the address book. Irreversible. +
Format: `delete INDEX`
Format: `delete INDEX` OR 'delete NAME'

****
Deletes the person at the specified `INDEX`.
Deletes the person at the specified `INDEX` or of the specified 'NAME'.
The index refers to the index number shown in the most recent listing.
****

Expand All @@ -111,6 +111,9 @@ Deletes the 2nd person in the address book.
`delete 1` +
Deletes the 1st person in the results of the `find` command.

* 'delete Betsy'
Deletes the peron called Betsy in the address book.

== View non-private details of a person : `view`

Displays the non-private details of the specified person. +
Expand Down
23 changes: 19 additions & 4 deletions src/seedu/addressbook/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;


/**
* Deletes a person identified using it's last displayed index from the address book.
* Deletes a person identified using it's last displayed index or name from the address book.
*/
public class DeleteCommand extends Command {

Expand All @@ -15,20 +14,36 @@ public class DeleteCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the person identified by the index number used in the last person listing.\n"
+ "Parameters: INDEX\n"
+ "Example: " + COMMAND_WORD + " 1";
+ "Example: " + COMMAND_WORD + " 1\n"
+ "OR\n"
+ "deletes the person identified by their name.\n"
+ "Parameters: NAME\n"
+ "Example: " + COMMAND_WORD + " John Doe";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";

private String name;

public DeleteCommand(int targetVisibleIndex) {
super(targetVisibleIndex);
}

public DeleteCommand(String name) {this.name = name;}

@Override
public CommandResult execute() {
try {
final ReadOnlyPerson target = getTargetPerson();
ReadOnlyPerson target = null;
if (name == null) {
target = getTargetPerson();
} else {
for (ReadOnlyPerson person : addressBook.getAllPersons()) {
if (name.equals(person.getName())) {
target = person;
break;
}
}
}
addressBook.removePerson(target);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, target));

Expand Down
33 changes: 27 additions & 6 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Parser {
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)"
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags


public static final Pattern PERSON_NAME_ARGS_FORMAT = Pattern.compile("(?<name>[^/]+)");
/**
* Signals that the user input could not be parsed.
*/
Expand Down Expand Up @@ -166,11 +166,19 @@ private static Set<String> getTagsFromArgs(String tagArguments) throws IllegalVa
private Command prepareDelete(String args) {
try {
final int targetIndex = parseArgsAsDisplayedIndex(args);

return new DeleteCommand(targetIndex);
} catch (ParseException pe) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
} catch (NumberFormatException nfe) {
return new IncorrectCommand(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
} catch (Exception e) {
if (e instanceof NumberFormatException) {
return new IncorrectCommand(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
try {
final String name = parseArgsAsName(args);

return new DeleteCommand(name);
} catch (ParseException pe){
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
}
}

Expand All @@ -181,7 +189,6 @@ private Command prepareDelete(String args) {
* @return the prepared command
*/
private Command prepareView(String args) {

try {
final int targetIndex = parseArgsAsDisplayedIndex(args);
return new ViewCommand(targetIndex);
Expand Down Expand Up @@ -228,6 +235,20 @@ private int parseArgsAsDisplayedIndex(String args) throws ParseException, Number
return Integer.parseInt(matcher.group("targetIndex"));
}

/**
* Parses the given arguments string as a single name string.
*
* @param args arguments string to parse as name
* @return the parsed name
* @throws ParseException if there is an error in parsing a string
*/
private String parseArgsAsName(String args) throws ParseException {
final Matcher matcher = PERSON_NAME_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
throw new ParseException("Could not find name to parse");
}
return matcher.group();
}

/**
* Parses arguments in the context of the find person command.
Expand Down