diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5a158b67..22a21145 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -71,6 +71,7 @@ public class AddressBook { private static final String MESSAGE_COMMAND_HELP_PARAMETERS = "\tParameters: %1$s"; private static final String MESSAGE_COMMAND_HELP_EXAMPLE = "\tExample: %1$s"; private static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s"; + private static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person %1$s"; private static final String MESSAGE_DISPLAY_PERSON_DATA = "%1$s Phone Number: %2$s Email: %3$s"; private static final String MESSAGE_DISPLAY_LIST_ELEMENT_INDEX = "%1$d. "; private static final String MESSAGE_GOODBYE = "Exiting Address Book... Good bye!"; @@ -107,7 +108,7 @@ public class AddressBook { private static final String COMMAND_FIND_WORD = "find"; private static final String COMMAND_FIND_DESC = "Finds all persons whose names contain any of the specified " - + "keywords (case-sensitive) and displays them as a list with index numbers."; + + "keywords (case-insensitive) and displays them as a list with index numbers."; private static final String COMMAND_FIND_PARAMETERS = "KEYWORD [MORE_KEYWORDS]"; private static final String COMMAND_FIND_EXAMPLE = COMMAND_FIND_WORD + " alice bob charlie"; @@ -115,6 +116,17 @@ public class AddressBook { private static final String COMMAND_LIST_DESC = "Displays all persons as a list with index numbers."; private static final String COMMAND_LIST_EXAMPLE = COMMAND_LIST_WORD; + private static final String COMMAND_EDIT_WORD = "edit"; + private static final String COMMAND_EDIT_DESC = "Edit a person identified by the index number used in the last find/" + + "list call"; + private static final String COMMAND_EDIT_PARAMETER = "INDEX"; + private static final String COMMAND_EDIT_EXAMPLE = COMMAND_EDIT_WORD + " 1"; + private static final String EDIT_DESCRIPTION = LINE_PREFIX + LS + "Enter new values for each field. Leave it blank if you do not" + + " to want to update it"; + private static final String NAME_EDIT = LS + "New Name: "; + private static final String NUMBER_EDIT = LINE_PREFIX + "New phone number: "; + private static final String EMAIL_EDIT = LINE_PREFIX + "New email: "; + private static final String COMMAND_DELETE_WORD = "delete"; private static final String COMMAND_DELETE_DESC = "Deletes a person identified by the index number used in " + "the last find/list call."; @@ -369,23 +381,25 @@ 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()); + } } /** @@ -473,7 +487,7 @@ private static String getMessageForPersonsDisplayedSummary(ArrayList p * @return set of keywords as specified by args */ private static Set extractKeywordsFromFindPersonArgs(String findPersonCommandArgs) { - return new HashSet<>(splitByWhitespace(findPersonCommandArgs.trim())); + return new HashSet<>(splitByWhitespace(findPersonCommandArgs.trim().toLowerCase())); } /** @@ -485,7 +499,7 @@ private static Set extractKeywordsFromFindPersonArgs(String findPersonCo private static ArrayList getPersonsWithNameContainingAnyKeyword(Collection keywords) { final ArrayList matchedPersons = new ArrayList<>(); for (String[] person : getAllPersonsInAddressBook()) { - final Set wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person))); + final Set wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person).toLowerCase())); if (!Collections.disjoint(wordsInName, keywords)) { matchedPersons.add(person); } @@ -500,10 +514,10 @@ private static ArrayList getPersonsWithNameContainingAnyKeyword(Collec * @return feedback display message for the operation result */ private static String executeDeletePerson(String commandArgs) { - if (!isDeletePersonArgsValid(commandArgs)) { + if (!isEditPersonArgsValid(commandArgs)) { return getMessageForInvalidCommandInput(COMMAND_DELETE_WORD, getUsageInfoForDeleteCommand()); } - final int targetVisibleIndex = extractTargetIndexFromDeletePersonArgs(commandArgs); + final int targetVisibleIndex = extractTargetIndexFromEditPersonArgs(commandArgs); if (!isDisplayIndexValidForLastPersonListingView(targetVisibleIndex)) { return MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; } @@ -513,12 +527,12 @@ private static String executeDeletePerson(String commandArgs) { } /** - * Checks validity of delete person argument string's format. + * Checks validity of delete/edit person argument string's format. * - * @param rawArgs raw command args string for the delete person command + * @param rawArgs raw command args string for the delete/edit person command * @return whether the input args string is valid */ - private static boolean isDeletePersonArgsValid(String rawArgs) { + private static boolean isEditPersonArgsValid(String rawArgs) { try { final int extractedIndex = Integer.parseInt(rawArgs.trim()); // use standard libraries to parse return extractedIndex >= DISPLAYED_INDEX_OFFSET; @@ -528,12 +542,12 @@ private static boolean isDeletePersonArgsValid(String rawArgs) { } /** - * Extracts the target's index from the raw delete person args string + * Extracts the target's index from the raw delete/edit person args string * - * @param rawArgs raw command args string for the delete person command + * @param rawArgs raw command args string for the delete/edit person command * @return extracted index */ - private static int extractTargetIndexFromDeletePersonArgs(String rawArgs) { + private static int extractTargetIndexFromEditPersonArgs(String rawArgs) { return Integer.parseInt(rawArgs.trim()); } @@ -579,6 +593,51 @@ private static String executeListAllPersonsInAddressBook() { return getMessageForPersonsDisplayedSummary(toBeDisplayed); } + private static String executeEditPerson(String commandArgs) { + if (!isEditPersonArgsValid(commandArgs)) { + return getMessageForInvalidCommandInput(COMMAND_EDIT_WORD, getUsageInfoForEditCommand()); + } + final int targetVisibleIndex = extractTargetIndexFromEditPersonArgs(commandArgs); + if (!isDisplayIndexValidForLastPersonListingView(targetVisibleIndex)) { + return MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; + } + final String[] targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); + final String[] newParams = getEditParams(); + return editPersonFromAddressBook(targetInModel, newParams) ? getMessageForSuccessfulEdit(targetInModel) // success + : MESSAGE_PERSON_NOT_IN_ADDRESSBOOK; // not found + } + + /** + * Fetches the name, number and email after edit. If empty response for a field, store as null + * and interpret in executeEditPerson as not making a change for that particular field + * + * @return new values for a Person's fields + */ + private static String[] getEditParams() { + String[] newParams = new String[3]; + + System.out.print(EDIT_DESCRIPTION); + System.out.print(NAME_EDIT); + newParams[0] = SCANNER.nextLine(); + System.out.print(NUMBER_EDIT); + newParams[1] = SCANNER.nextLine(); + System.out.print(EMAIL_EDIT); + newParams[2] = SCANNER.nextLine(); + + return newParams; + } + + /** + * Constructs a feedback message for a successful delete person command execution. + * + * @see #executeEditPerson(String) + * @param editedPerson successfully deleted + * @return successful delete person feedback message + */ + private static String getMessageForSuccessfulEdit(String[] editedPerson) { + return String.format(MESSAGE_EDIT_PERSON_SUCCESS, getMessageForFormattedPersonData(editedPerson)); + } + /** * Requests to terminate the program. */ @@ -801,6 +860,22 @@ private static boolean deletePersonFromAddressBook(String[] exactPerson) { return changed; } + /** + * Edits the specified person from the addressbook if it is inside. Saves any changes to storage file. + * + * @param exactPerson the actual person inside the address book (exactPerson == the person to delete in the full list) + * @param newParams the new values for the Person's fields + * @return true if the given person was found and deleted in the model + */ + private static boolean editPersonFromAddressBook(String[] exactPerson, String[] newParams) { + final boolean exists = ALL_PERSONS.contains(exactPerson); + if (exists) { + replaceEditedFields(exactPerson, newParams); + savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); + } + return exists; + } + /** * Returns all persons in the address book */ @@ -1073,6 +1148,27 @@ private static boolean isPersonEmailValid(String email) { //TODO: implement a more permissive validation } + /** + * Does the one to one updating of the fields if it is not empty + * @param exactPerson the person to be updated + * @param newParams the new values for the fields + */ + private static void replaceEditedFields(String[] exactPerson, String[] newParams) { +// for (int i = 0; i < exactPerson.length; i++){ +// if (!newParams[i].isEmpty() ){ +// exactPerson[i] = newParams[i]; +// } +// } + if (!newParams[0].isEmpty() && isPersonNameValid(newParams[0])){ + exactPerson[0] = newParams[0]; + } + if (!newParams[1].isEmpty() && isPersonPhoneValid(newParams[1])){ + exactPerson[1] = newParams[1]; + } + if (!newParams[2].isEmpty() && isPersonEmailValid(newParams[2])){ + exactPerson[2] = newParams[2]; + } + } /* * =============================================== @@ -1086,6 +1182,7 @@ private static String getUsageInfoForAllCommands() { + getUsageInfoForFindCommand() + LS + getUsageInfoForViewCommand() + LS + getUsageInfoForDeleteCommand() + LS + + getUsageInfoForEditCommand() + LS + getUsageInfoForClearCommand() + LS + getUsageInfoForExitCommand() + LS + getUsageInfoForHelpCommand(); @@ -1112,6 +1209,13 @@ private static String getUsageInfoForDeleteCommand() { + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_DELETE_EXAMPLE) + LS; } + /** 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_PARAMETER) + LS + + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_EDIT_EXAMPLE) + LS; + } + /** Returns string for showing 'clear' command usage instruction */ private static String getUsageInfoForClearCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_CLEAR_WORD, COMMAND_CLEAR_DESC) + LS @@ -1164,4 +1268,6 @@ private static ArrayList splitByWhitespace(String toSplit) { return new ArrayList<>(Arrays.asList(toSplit.trim().split("\\s+"))); } + + } \ No newline at end of file