Skip to content

Commit

Permalink
Merge 1cdb1f6 into ef8bc07
Browse files Browse the repository at this point in the history
  • Loading branch information
alxkohh committed Oct 31, 2019
2 parents ef8bc07 + 1cdb1f6 commit 0c057f5
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/dukeacademy/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.dukeacademy.logic.commands.CommandLogicManager;
import com.dukeacademy.logic.commands.attempt.AttemptCommandFactory;
import com.dukeacademy.logic.commands.bookmark.BookmarkCommandFactory;
import com.dukeacademy.logic.commands.bookmark.DeleteBookmarkCommandFactory;
import com.dukeacademy.logic.commands.browse.BrowseCommandFactory;
import com.dukeacademy.logic.commands.exit.ExitCommandFactory;
import com.dukeacademy.logic.commands.find.FindCommandFactory;
Expand Down Expand Up @@ -303,6 +304,10 @@ private CommandLogicManager initCommandLogic() {
// Registering tab command
TabCommandFactory tabCommandFactory = new TabCommandFactory(this.applicationState);
commandLogicManager.registerCommand(tabCommandFactory);
// Registering delete bookmark command
DeleteBookmarkCommandFactory deleteBookmarkCommandFactory =
new DeleteBookmarkCommandFactory(this.questionsLogic);
commandLogicManager.registerCommand(deleteBookmarkCommandFactory);

return commandLogicManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public BookmarkCommand(int index, QuestionsLogic questionsLogic) {

/**
* Executes the bookmark command.
* Execution of this command is conditional in nature. If question that user selects to bookmark
* is already bookmarked, user will be notified of that through the feedback panel.
* Otherwise if the question that user selects is not already bookmarked, we will return a bookmarked
* version of the same question and notify user of a successful bookmark action.
* Execution of this command is conditional in nature. If question that user chooses to bookmark
* is already bookmarked, we will simply notify the user of that through the CLI feedback panel.
* Otherwise if the question that user selects is not already bookmarked, we will update the question
* with a bookmarked version of the same question and notify user of a successful bookmark action.
* @return
* @throws CommandException
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.dukeacademy.logic.commands.bookmark;

import java.util.logging.Logger;

import com.dukeacademy.commons.core.LogsCenter;
import com.dukeacademy.logic.commands.Command;
import com.dukeacademy.logic.commands.CommandResult;
import com.dukeacademy.logic.commands.exceptions.CommandException;
import com.dukeacademy.logic.question.QuestionsLogic;
import com.dukeacademy.model.question.Question;

/**
* Command to delete a bookmark made on a question.
*/
public class DeleteBookmarkCommand implements Command {
private final Logger logger;
private final QuestionsLogic questionsLogic;
private final int index;

/**
* Instantiates a new Delete Bookmark command.
* @param index the index
* @param questionsLogic the questions logic
*/
public DeleteBookmarkCommand(int index, QuestionsLogic questionsLogic) {
this.logger = LogsCenter.getLogger(DeleteBookmarkCommand.class);
this.index = index - 1;
this.questionsLogic = questionsLogic;
}

/**
* Executes the delete bookmark command.
* Execution of this command is conditional in nature. If the question that user chooses to delete bookmark,
* is indeed bookmarked, we will update the question to a non-bookmarked version of the same question.
* Otherwise if the question that user selects is not bookmarked in the first place, we will simply notify
* the user of that through the CLI feedback panel.
* @return
* @throws CommandException
*/
@Override
public CommandResult execute() throws CommandException {
try {
Question userSelection = this.questionsLogic.getQuestion(index);
boolean userSelectionIsBookmarked = userSelection.isBookmarked();

if (userSelectionIsBookmarked) {
// Update isBookmarked of question to false
Question bookmarkedQuestion = userSelection.withNewIsBookmarked(false);
this.questionsLogic.setQuestion(index, bookmarkedQuestion);
logger.info("Deleted bookmark for question at index " + index + " : " + bookmarkedQuestion);

// Notify user of successful bookmark action
String feedback = "Deleted bookmark for question " + (index + 1) + " : "
+ bookmarkedQuestion.getTitle();
return new CommandResult(feedback, false, false
);
} else {
// Simply notify user that question is not bookmarked
String feedback = "Question " + (index + 1) + " : " + userSelection.getTitle()
+ " - is not bookmarked.";
return new CommandResult(feedback, false, false
);
}

} catch (IndexOutOfBoundsException e) {
throw new CommandException("Index " + (index + 1) + " entered out of range for current list of questions.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.dukeacademy.logic.commands.bookmark;

import com.dukeacademy.logic.commands.Command;
import com.dukeacademy.logic.commands.CommandFactory;
import com.dukeacademy.logic.commands.exceptions.InvalidCommandArgumentsException;
import com.dukeacademy.logic.question.QuestionsLogic;

/**
* Factory class to represent all the necessary components for creating an
* DeleteBookmarkCommand instance.
*/
public class DeleteBookmarkCommandFactory implements CommandFactory {
private final QuestionsLogic questionsLogic;

/**
* Instantiates a new DeleteBookmark command factory.
*
* @param questionsLogic the questions logic
*/
public DeleteBookmarkCommandFactory(QuestionsLogic questionsLogic) {
this.questionsLogic = questionsLogic;
}

@Override
public String getCommandWord() {
return "deletebookmark";
}

@Override
public Command getCommand(String commandArguments) throws InvalidCommandArgumentsException {
try {
int index = Integer.parseInt(commandArguments.strip());
return new DeleteBookmarkCommand(index, questionsLogic);
} catch (NumberFormatException e) {
throw new InvalidCommandArgumentsException("Invalid index entered.");
}
}
}

0 comments on commit 0c057f5

Please sign in to comment.