From 9c86003901ff2eb5f928e6e2779ba6d203199053 Mon Sep 17 00:00:00 2001 From: mechome <32713221+mechome@users.noreply.github.com> Date: Tue, 12 Feb 2019 21:58:14 +0800 Subject: [PATCH] DeleteCommand.java: add new delete command syntax --- docs/UserGuide.adoc | 7 ++-- .../addressbook/commands/DeleteCommand.java | 23 ++++++++++--- src/seedu/addressbook/parser/Parser.java | 33 +++++++++++++++---- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..3881a56db 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -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. **** @@ -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. + diff --git a/src/seedu/addressbook/commands/DeleteCommand.java b/src/seedu/addressbook/commands/DeleteCommand.java index 493d75da6..0dd60f935 100644 --- a/src/seedu/addressbook/commands/DeleteCommand.java +++ b/src/seedu/addressbook/commands/DeleteCommand.java @@ -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 { @@ -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)); diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..7b235f881 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -41,7 +41,7 @@ public class Parser { + " (?p?)a/(?
[^/]+)" + "(?(?: t/[^/]+)*)"); // variable number of tags - + public static final Pattern PERSON_NAME_ARGS_FORMAT = Pattern.compile("(?[^/]+)"); /** * Signals that the user input could not be parsed. */ @@ -166,11 +166,19 @@ private static Set 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)); + } } } @@ -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); @@ -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.