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][F12-A2]Liu Yiming #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
97 changes: 81 additions & 16 deletions src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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/[email protected]";
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;
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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<String[]> 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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
|| Parameters: NAME p/PHONE_NUMBER e/EMAIL
|| Example: add John Doe p/98765432 e/[email protected]
||
|| 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/[email protected]
||
|| 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
Expand Down Expand Up @@ -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/[email protected]
||
|| ===================================================
|| 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/[email protected]
||
|| ===================================================
|| 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/[email protected]
||
|| ===================================================
|| Enter command: || [Command entered: edit 3]
|| The person index provided is invalid
|| ===================================================
|| Enter command: || [Command entered: edit 2 Adam Brown p/111111 e/[email protected]]
|| Person edited: Adam Brown, Phone: 111111, Email: [email protected]
|| ===================================================
|| Enter command: || [Command entered: list]
|| 1. Betsy Choo Phone Number: 222222 Email: [email protected]
|| 2. Adam Brown Phone Number: 111111 Email: [email protected]
||
|| 2 persons found!
|| ===================================================
|| Enter command: || [Command entered: clear]
|| Address book has been cleared!
|| ===================================================
Expand Down
16 changes: 16 additions & 0 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
list

##########################################################
# test clear command
##########################################################
Expand Down