From 50369905fe33e22db5edea39f657aa419129906e Mon Sep 17 00:00:00 2001 From: "[warheade]" <[wangjiannan124@live.com]> Date: Sun, 10 Feb 2019 16:32:06 +0800 Subject: [PATCH 1/2] 20190210 --- src/seedu/addressbook/commands/EditCommand.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/seedu/addressbook/commands/EditCommand.java diff --git a/src/seedu/addressbook/commands/EditCommand.java b/src/seedu/addressbook/commands/EditCommand.java new file mode 100644 index 000000000..48b0db6dc --- /dev/null +++ b/src/seedu/addressbook/commands/EditCommand.java @@ -0,0 +1,4 @@ +package seedu.addressbook.commands; + +public class EditCommand { +} From 3148665eddbaae155e11318ece985414b9f72bfb Mon Sep 17 00:00:00 2001 From: "[warheade]" <[wangjiannan124@live.com]> Date: Sun, 10 Feb 2019 19:38:57 +0800 Subject: [PATCH 2/2] Added an edit function that enables edition of existing contacts and add when it does not exist It might be useful in real life scenarios --- .../addressbook/commands/EditCommand.java | 75 ++++++++++++++++- src/seedu/addressbook/parser/Parser.java | 81 ++++++++++++++----- 2 files changed, 136 insertions(+), 20 deletions(-) diff --git a/src/seedu/addressbook/commands/EditCommand.java b/src/seedu/addressbook/commands/EditCommand.java index 48b0db6dc..f293d4ecf 100644 --- a/src/seedu/addressbook/commands/EditCommand.java +++ b/src/seedu/addressbook/commands/EditCommand.java @@ -1,4 +1,77 @@ package seedu.addressbook.commands; -public class EditCommand { +import seedu.addressbook.common.Messages; +import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.data.person.*; +import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException; +import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException; +import seedu.addressbook.data.tag.Tag; + +import java.util.HashSet; +import java.util.Set; + +/** + *Edit the particulars of a contact in the addressBook + */ +public class EditCommand extends Command{ + public static final String COMMAND_WORD = "edit"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a contact in the address book. " + + "Enter the index of the contact to be edited,followed by a '/ '.\n" + + "If the contact exists, enter the new particulars of this contact in the correct format.\n" + + "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/johnd@gmail.com a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney"; + + public static final String MESSAGE_SUCCESS = "Contact is edited: %1$s"; + //public static final String MESSAGE_NONEXISTENT_PERSON = "This person does not exist in the address book"; + + private final Person toEdit; + + /** + * Convenience constructor using raw values. + * + * @throws IllegalValueException if any of the raw values are invalid + */ + + public EditCommand(int targetVisibleIndex,String name, + String phone, boolean isPhonePrivate, + String email, boolean isEmailPrivate, + String address, boolean isAddressPrivate, + Set tags) throws IllegalValueException { + + final Set tagSet = new HashSet<>(); + for (String tagName : tags) { + tagSet.add(new Tag(tagName)); + } + setTargetIndex(targetVisibleIndex); + + this.toEdit = new Person( + new Name(name), + new Phone(phone, isPhonePrivate), + new Email(email, isEmailPrivate), + new Address(address, isAddressPrivate), + tagSet + ); + } + + @Override + public CommandResult execute() { + try { + + final ReadOnlyPerson target = getTargetPerson(); + System.out.println("fk"); + addressBook.removePerson(target); + + addressBook.addPerson(toEdit); + return new CommandResult(MESSAGE_SUCCESS); + } catch (IndexOutOfBoundsException ie) { + return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } catch (PersonNotFoundException pnfe) { + return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK); + } + catch (DuplicatePersonException dpethisisuseless){ + return new CommandResult("DuplicationPersonException thrown,never gonna happen though"); + } + } } diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..00c171c20 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -1,28 +1,14 @@ package seedu.addressbook.parser; -import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; +import seedu.addressbook.commands.*; +import seedu.addressbook.data.exception.IllegalValueException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; -import seedu.addressbook.commands.AddCommand; -import seedu.addressbook.commands.ClearCommand; -import seedu.addressbook.commands.Command; -import seedu.addressbook.commands.DeleteCommand; -import seedu.addressbook.commands.ExitCommand; -import seedu.addressbook.commands.FindCommand; -import seedu.addressbook.commands.HelpCommand; -import seedu.addressbook.commands.IncorrectCommand; -import seedu.addressbook.commands.ListCommand; -import seedu.addressbook.commands.ViewAllCommand; -import seedu.addressbook.commands.ViewCommand; -import seedu.addressbook.data.exception.IllegalValueException; +import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; /** * Parses user input. @@ -79,6 +65,9 @@ public Command parseCommand(String userInput) { case DeleteCommand.COMMAND_WORD: return prepareDelete(arguments); + case EditCommand.COMMAND_WORD: + return prepareEdit(arguments); + case ClearCommand.COMMAND_WORD: return new ClearCommand(); @@ -174,6 +163,60 @@ private Command prepareDelete(String args) { } } + private Command prepareEdit (String args){ + if(args.split(" ").length<=4){ + System.out.println("args not long enough"); + } + + args=args.trim(); + + // args here is 1 J D p/98984988 e/jd@gmail.com a/Clementi t/mad t/friend + int position = args.indexOf(" "); + + String indexNum = args.substring(0,position); + String info = args.substring(position); + System.out.println(indexNum); + System.out.println(info); + final int targetIndex; + + try { + targetIndex = parseArgsAsDisplayedIndex(indexNum); + // targetIndex is 1 here + + } catch (ParseException pe) { + return new IncorrectCommand("Wrong format"); + } catch (NumberFormatException nfe){ + return new IncorrectCommand("Wrong number format"); + } + + final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(info.trim()); + // Validate arg string format + if (!matcher.matches()) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); + } + try { + return new EditCommand( + targetIndex, + + matcher.group("name"), + + matcher.group("phone"), + isPrivatePrefixPresent(matcher.group("isPhonePrivate")), + + matcher.group("email"), + isPrivatePrefixPresent(matcher.group("isEmailPrivate")), + + matcher.group("address"), + isPrivatePrefixPresent(matcher.group("isAddressPrivate")), + + getTagsFromArgs(matcher.group("tagArguments")) + + ); + } catch (IllegalValueException ive) { + return new IncorrectCommand(ive.getMessage()); + } + } + /** * Parses arguments in the context of the view command. *