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

AddressBook.java: add recover command #10

Open
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ Deletes the 2nd person in the address book.
`delete 1` +
Deletes the 1st person in the results of the `find` command.

=== Recovering a person: `recover`

Recovers the specified version from the address book. +
Format: `recover`


=== Clearing all entries: `clear`

Clears all entries from the address book. +
Expand Down
36 changes: 36 additions & 0 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_ADDRESSBOOK_RECOVERED = "Latest deleted person has been recovered!";
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 @@ -115,6 +116,10 @@ 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_RECOVER_WORD = "recover";
private static final String COMMAND_RECOVER_DESC = "Recover the latest deleted people.";
private static final String COMMAND_RECOVER_EXAMPLE = COMMAND_RECOVER_WORD;

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 @@ -183,6 +188,7 @@ public class AddressBook {
*/
private static final ArrayList<String[]> ALL_PERSONS = new ArrayList<>();

private static String LAST_DELETE_PERSON;
/**
* Stores the most recent list of persons shown to the user as a result of a user command.
* This is a subset of the full list. Deleting persons in the pull list does not delete
Expand Down Expand Up @@ -377,6 +383,8 @@ private static String executeCommand(String userInputString) {
return executeListAllPersonsInAddressBook();
case COMMAND_DELETE_WORD:
return executeDeletePerson(commandArgs);
case COMMAND_RECOVER_WORD:
return executeRecoverAddressBook();
case COMMAND_CLEAR_WORD:
return executeClearAddressBook();
case COMMAND_HELP_WORD:
Expand Down Expand Up @@ -558,6 +566,18 @@ private static String getMessageForSuccessfulDelete(String[] deletedPerson) {
return String.format(MESSAGE_DELETE_PERSON_SUCCESS, getMessageForFormattedPersonData(deletedPerson));
}


/**
* Clears all persons in the address book.
*
* @return feedback display message for the operation result
*/
private static String executeRecoverAddressBook() {
recoverPersonToAddressBook();
return MESSAGE_ADDRESSBOOK_RECOVERED;
}


/**
* Clears all persons in the address book.
*
Expand Down Expand Up @@ -786,6 +806,14 @@ private static void addPersonToAddressBook(String[] person) {
ALL_PERSONS.add(person);
savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath);
}
/**
* Recovers the latest deleted person to the address book. Saves changes to storage file.
*
*/
private static void recoverPersonToAddressBook() {
ALL_PERSONS.add(decodePersonFromString(LAST_DELETE_PERSON ).get());
savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath);
}

/**
* Deletes the specified person from the addressbook if it is inside. Saves any changes to storage file.
Expand All @@ -795,6 +823,7 @@ private static void addPersonToAddressBook(String[] person) {
*/
private static boolean deletePersonFromAddressBook(String[] exactPerson) {
final boolean changed = ALL_PERSONS.remove(exactPerson);
LAST_DELETE_PERSON = encodePersonToString(exactPerson);
if (changed) {
savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath);
}
Expand Down Expand Up @@ -1086,6 +1115,7 @@ private static String getUsageInfoForAllCommands() {
+ getUsageInfoForFindCommand() + LS
+ getUsageInfoForViewCommand() + LS
+ getUsageInfoForDeleteCommand() + LS
+ getUsageInfoForRecoverCommand() + LS
+ getUsageInfoForClearCommand() + LS
+ getUsageInfoForExitCommand() + LS
+ getUsageInfoForHelpCommand();
Expand All @@ -1105,6 +1135,12 @@ private static String getUsageInfoForFindCommand() {
+ String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_FIND_EXAMPLE) + LS;
}

/** Returns the string for showing 'recover' command usage instruction */
private static String getUsageInfoForRecoverCommand() {
return String.format(MESSAGE_COMMAND_HELP, COMMAND_RECOVER_WORD, COMMAND_RECOVER_DESC) + LS
+ String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_RECOVER_EXAMPLE) + LS;
}

/** Returns the string for showing 'delete' command usage instruction */
private static String getUsageInfoForDeleteCommand() {
return String.format(MESSAGE_COMMAND_HELP, COMMAND_DELETE_WORD, COMMAND_DELETE_DESC) + LS
Expand Down
3 changes: 3 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
|| Parameters: INDEX
|| Example: delete 1
||
|| recover: Recover the latest deleted people.
|| Example: recover
||
|| clear: Clears address book permanently.
|| Example: clear
||
Expand Down