diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5a158b67..f6d0df93 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -66,6 +66,7 @@ public class AddressBook { * ========================================================================= */ private static final String MESSAGE_ADDED = "New person added: %1$s, Phone: %2$s, Email: %3$s"; + private static final String MESSAGE_EDITED = "Person edited: %1$s, Phone: %2$s, Email: %3$s"; private static final String MESSAGE_ADDRESSBOOK_CLEARED = "Address book has been cleared!"; private static final String MESSAGE_COMMAND_HELP = "%1$s: %2$s"; private static final String MESSAGE_COMMAND_HELP_PARAMETERS = "\tParameters: %1$s"; @@ -129,6 +130,13 @@ public class AddressBook { private static final String COMMAND_HELP_DESC = "Shows program usage instructions."; private static final String COMMAND_HELP_EXAMPLE = COMMAND_HELP_WORD; + private static final String COMMAND_EDIT_WORD = "edit"; + private static final String COMMAND_EDIT_DESC = "Edit the details of a person identified by the index number" + + "used in the last find/list call."; + private static final String COMMAND_EDIT_EXAMPLE = COMMAND_EDIT_WORD + " 1 John Doe p/98765432 e/johnd@gmail.com"; + private static final String COMMAND_EDIT_PARAMETERS = "INDEX " + "NAME " + PERSON_DATA_PREFIX_PHONE + "PHONE_NUMBER " + + PERSON_DATA_PREFIX_EMAIL + "EMAIL"; + private static final String COMMAND_EXIT_WORD = "exit"; private static final String COMMAND_EXIT_DESC = "Exits the program."; private static final String COMMAND_EXIT_EXAMPLE = COMMAND_EXIT_WORD; @@ -369,22 +377,24 @@ private static String executeCommand(String userInputString) { final String commandType = commandTypeAndParams[0]; final String commandArgs = commandTypeAndParams[1]; switch (commandType) { - case COMMAND_ADD_WORD: - return executeAddPerson(commandArgs); - case COMMAND_FIND_WORD: - return executeFindPersons(commandArgs); - case COMMAND_LIST_WORD: - return executeListAllPersonsInAddressBook(); - case COMMAND_DELETE_WORD: - return executeDeletePerson(commandArgs); - case COMMAND_CLEAR_WORD: - return executeClearAddressBook(); - case COMMAND_HELP_WORD: - return getUsageInfoForAllCommands(); - case COMMAND_EXIT_WORD: - executeExitProgramRequest(); - default: - return getMessageForInvalidCommandInput(commandType, getUsageInfoForAllCommands()); + case COMMAND_EDIT_WORD: + return executeEditPerson(commandArgs); + case COMMAND_ADD_WORD: + return executeAddPerson(commandArgs); + case COMMAND_FIND_WORD: + return executeFindPersons(commandArgs); + case COMMAND_LIST_WORD: + return executeListAllPersonsInAddressBook(); + case COMMAND_DELETE_WORD: + return executeDeletePerson(commandArgs); + case COMMAND_CLEAR_WORD: + return executeClearAddressBook(); + case COMMAND_HELP_WORD: + return getUsageInfoForAllCommands(); + case COMMAND_EXIT_WORD: + executeExitProgramRequest(); + default: + return getMessageForInvalidCommandInput(commandType, getUsageInfoForAllCommands()); } } @@ -408,6 +418,41 @@ private static String getMessageForInvalidCommandInput(String userCommand, Strin return String.format(MESSAGE_INVALID_COMMAND_FORMAT, userCommand, correctUsageInfo); } + /** + * Edit the details on a person identified using last displayed index. + * + * @param commandArgs full command args string from the user + * @return feedback display message for the operation result + */ + private static String executeEditPerson(String commandArgs) { + final String[] commandIndexAndParams = splitCommandWordAndArgs(commandArgs); + final String commandIndex = commandIndexAndParams[0]; + final String commandPrarms = commandIndexAndParams[1]; + if (!isDeletePersonArgsValid(commandIndex)) { + return getMessageForInvalidCommandInput(COMMAND_EDIT_WORD, getUsageInfoForEditCommand()); + } + final int targetVisibleIndex = extractTargetIndexFromDeletePersonArgs(commandIndex); + if (!isDisplayIndexValidForLastPersonListingView(targetVisibleIndex)) { + return MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; + } + + // try decoding a person from the raw args + final Optional decodeResult = decodePersonFromString(commandPrarms); + + // checks if args are valid (decode result will not be present if the person is invalid) + if (!decodeResult.isPresent()) { + return getMessageForInvalidCommandInput(COMMAND_EDIT_WORD, getUsageInfoForEditCommand()); + } + + final String[] targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); + deletePersonFromAddressBook(targetInModel); + + // add the person as specified + final String[] personToAdd = decodeResult.get(); + addPersonToAddressBook(personToAdd); + return getMessageForSuccessfulEdit(personToAdd); + } + /** * Adds a person (specified by the command args) to the address book. * The entire command arguments string is treated as a string representation of the person to add. @@ -430,6 +475,18 @@ private static String executeAddPerson(String commandArgs) { return getMessageForSuccessfulAddPerson(personToAdd); } + /** + * Constructs a feedback message for a successful edit command execution. + * + * @see #executeEditPerson(String) + * @param editedPerson person who was successfully edited + * @return successful edited person feedback message + */ + private static String getMessageForSuccessfulEdit(String[] editedPerson) { + return String.format(MESSAGE_EDITED, + getNameFromPerson(editedPerson), getPhoneFromPerson(editedPerson), getEmailFromPerson(editedPerson)); + } + /** * Constructs a feedback message for a successful add person command execution. * @@ -1083,6 +1140,7 @@ private static boolean isPersonEmailValid(String email) { /** Returns usage info for all commands */ private static String getUsageInfoForAllCommands() { return getUsageInfoForAddCommand() + LS + + getUsageInfoForEditCommand() + LS + getUsageInfoForFindCommand() + LS + getUsageInfoForViewCommand() + LS + getUsageInfoForDeleteCommand() + LS @@ -1091,6 +1149,13 @@ private static String getUsageInfoForAllCommands() { + getUsageInfoForHelpCommand(); } + /** Returns the string for showing 'edit' command usage instruction */ + private static String getUsageInfoForEditCommand() { + return String.format(MESSAGE_COMMAND_HELP, COMMAND_EDIT_WORD, COMMAND_EDIT_DESC) + LS + + String.format(MESSAGE_COMMAND_HELP_PARAMETERS, COMMAND_EDIT_PARAMETERS) + LS + + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_EDIT_EXAMPLE) + LS; + } + /** Returns the string for showing 'add' command usage instruction */ private static String getUsageInfoForAddCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_ADD_WORD, COMMAND_ADD_DESC) + LS diff --git a/test/expected.txt b/test/expected.txt index f18922ac..a59ef412 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -55,6 +55,10 @@ || Parameters: NAME p/PHONE_NUMBER e/EMAIL || Example: add John Doe p/98765432 e/johnd@gmail.com || +|| edit: Edit the details of a person identified by the index numberused in the last find/list call. +|| Parameters: INDEX NAME p/PHONE_NUMBER e/EMAIL +|| Example: edit 1 John Doe p/98765432 e/johnd@gmail.com +|| || find: Finds all persons whose names contain any of the specified keywords (case-sensitive) and displays them as a list with index numbers. || Parameters: KEYWORD [MORE_KEYWORDS] || Example: find alice bob charlie @@ -268,6 +272,39 @@ || || 2 persons found! || =================================================== +|| Enter command: || [Command entered: edit] +|| Invalid command format: edit +|| edit: Edit the details of a person identified by the index numberused in the last find/list call. +|| Parameters: INDEX NAME p/PHONE_NUMBER e/EMAIL +|| Example: edit 1 John Doe p/98765432 e/johnd@gmail.com +|| +|| =================================================== +|| Enter command: || [Command entered: edit -1] +|| Invalid command format: edit +|| edit: Edit the details of a person identified by the index numberused in the last find/list call. +|| Parameters: INDEX NAME p/PHONE_NUMBER e/EMAIL +|| Example: edit 1 John Doe p/98765432 e/johnd@gmail.com +|| +|| =================================================== +|| Enter command: || [Command entered: edit 0] +|| Invalid command format: edit +|| edit: Edit the details of a person identified by the index numberused in the last find/list call. +|| Parameters: INDEX NAME p/PHONE_NUMBER e/EMAIL +|| Example: edit 1 John Doe p/98765432 e/johnd@gmail.com +|| +|| =================================================== +|| Enter command: || [Command entered: edit 3] +|| The person index provided is invalid +|| =================================================== +|| Enter command: || [Command entered: edit 2 Adam Brown p/111111 e/adam@gmail.com] +|| Person edited: Adam Brown, Phone: 111111, Email: adam@gmail.com +|| =================================================== +|| Enter command: || [Command entered: list] +|| 1. Betsy Choo Phone Number: 222222 Email: benchoo@nus.edu.sg +|| 2. Adam Brown Phone Number: 111111 Email: adam@gmail.com +|| +|| 2 persons found! +|| =================================================== || Enter command: || [Command entered: clear] || Address book has been cleared! || =================================================== diff --git a/test/input.txt b/test/input.txt index 0b99df54..f22744b5 100644 --- a/test/input.txt +++ b/test/input.txt @@ -91,6 +91,22 @@ delete 1 list +########################################################## +# test edit command +########################################################## + + # should catch invalid args format + edit + + # should catch invalid index + edit -1 + edit 0 + edit 3 + + # edits correct index + edit 2 Adam Brown p/111111 e/adam@gmail.com + list + ########################################################## # test clear command ##########################################################