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

[W3.10][W14-A1]K Muruges #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
162 changes: 134 additions & 28 deletions src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down Expand Up @@ -107,14 +108,25 @@ 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";

private static final String COMMAND_LIST_WORD = "list";
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.";
Expand Down Expand Up @@ -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());
}
}

/**
Expand Down Expand Up @@ -473,7 +487,7 @@ private static String getMessageForPersonsDisplayedSummary(ArrayList<String[]> p
* @return set of keywords as specified by args
*/
private static Set<String> extractKeywordsFromFindPersonArgs(String findPersonCommandArgs) {
return new HashSet<>(splitByWhitespace(findPersonCommandArgs.trim()));
return new HashSet<>(splitByWhitespace(findPersonCommandArgs.trim().toLowerCase()));
}

/**
Expand All @@ -485,7 +499,7 @@ private static Set<String> extractKeywordsFromFindPersonArgs(String findPersonCo
private static ArrayList<String[]> getPersonsWithNameContainingAnyKeyword(Collection<String> keywords) {
final ArrayList<String[]> matchedPersons = new ArrayList<>();
for (String[] person : getAllPersonsInAddressBook()) {
final Set<String> wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person)));
final Set<String> wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person).toLowerCase()));
if (!Collections.disjoint(wordsInName, keywords)) {
matchedPersons.add(person);
}
Expand All @@ -500,10 +514,10 @@ private static ArrayList<String[]> 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;
}
Expand All @@ -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;
Expand All @@ -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());
}

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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];
}
}

/*
* ===============================================
Expand All @@ -1086,6 +1182,7 @@ private static String getUsageInfoForAllCommands() {
+ getUsageInfoForFindCommand() + LS
+ getUsageInfoForViewCommand() + LS
+ getUsageInfoForDeleteCommand() + LS
+ getUsageInfoForEditCommand() + LS
+ getUsageInfoForClearCommand() + LS
+ getUsageInfoForExitCommand() + LS
+ getUsageInfoForHelpCommand();
Expand All @@ -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
Expand Down Expand Up @@ -1164,4 +1268,6 @@ private static ArrayList<String> splitByWhitespace(String toSplit) {
return new ArrayList<>(Arrays.asList(toSplit.trim().split("\\s+")));
}



}