From 8b1ce87319aea8dfcb5c995752ad1602059d597c Mon Sep 17 00:00:00 2001 From: Wang Xueting Date: Thu, 17 Oct 2019 09:45:41 +0800 Subject: [PATCH] Implement listq and deleteq features for question --- .../DeleteQuestionCommand.java | 55 +++++++++++++++++++ .../questioncommands/ListQuestionCommand.java | 24 ++++++++ .../logic/parser/AddressBookParser.java | 9 +++ .../DeleteQuestionCommandParser.java | 31 +++++++++++ .../seedu/address/model/ModelManager.java | 2 +- .../java/seedu/address/ui/MainWindow.java | 7 +++ ...uestionCard.java => QuestionListCard.java} | 8 +-- .../seedu/address/ui/QuestionListPanel.java | 2 +- 8 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/questioncommands/DeleteQuestionCommand.java create mode 100644 src/main/java/seedu/address/logic/commands/questioncommands/ListQuestionCommand.java create mode 100644 src/main/java/seedu/address/logic/parser/questionparser/DeleteQuestionCommandParser.java rename src/main/java/seedu/address/ui/{QuestionCard.java => QuestionListCard.java} (84%) diff --git a/src/main/java/seedu/address/logic/commands/questioncommands/DeleteQuestionCommand.java b/src/main/java/seedu/address/logic/commands/questioncommands/DeleteQuestionCommand.java new file mode 100644 index 00000000000..98f623075bb --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/questioncommands/DeleteQuestionCommand.java @@ -0,0 +1,55 @@ +package seedu.address.logic.commands.questioncommands; + +import static java.util.Objects.requireNonNull; + +import java.util.List; + +import seedu.address.commons.core.Messages; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.question.Question; + +/** + * Deletes a question by index number. + */ +public class DeleteQuestionCommand extends Command { + public static final String COMMAND_WORD = "deleteq"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Deletes the question with the specified index number.\n" + + "Parameters: INDEX (must be a positive integer)\n" + + "Example: " + COMMAND_WORD + " 1"; + + public static final String MESSAGE_DELETE_QUESTION_SUCCESS = "Deleted question: %1$s"; + + private final Index targetIndex; + + public DeleteQuestionCommand(Index targetIndex) { + this.targetIndex = targetIndex; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredQuestionList(); + + if (targetIndex.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_QUESTION_DISPLAYED_INDEX); + } + + Question questionToDelete = lastShownList.get(targetIndex.getZeroBased()); + model.deleteQuestion(questionToDelete); + model.updateFilteredQuestionList(Model.PREDICATE_SHOW_ALL_QUESTIONS); + return new CommandResult(String.format(MESSAGE_DELETE_QUESTION_SUCCESS, questionToDelete)); + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof DeleteQuestionCommand // instanceof handles nulls + && targetIndex.equals(((DeleteQuestionCommand) other).targetIndex)); // state check + } +} diff --git a/src/main/java/seedu/address/logic/commands/questioncommands/ListQuestionCommand.java b/src/main/java/seedu/address/logic/commands/questioncommands/ListQuestionCommand.java new file mode 100644 index 00000000000..56e10035140 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/questioncommands/ListQuestionCommand.java @@ -0,0 +1,24 @@ +package seedu.address.logic.commands.questioncommands; + +import static java.util.Objects.requireNonNull; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_QUESTIONS; + +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.CommandResult; +import seedu.address.model.Model; + +/** + * Lists all questions. + */ +public class ListQuestionCommand extends Command { + public static final String COMMAND_WORD = "listq"; + + public static final String MESSAGE_SUCCESS = "Listed all questions."; + + @Override + public CommandResult execute(Model model) { + requireNonNull(model); + model.updateFilteredQuestionList(PREDICATE_SHOW_ALL_QUESTIONS); + return new CommandResult(MESSAGE_SUCCESS); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index f6baa2df20b..c9390e58a7e 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -16,6 +16,8 @@ import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListNoteCommand; import seedu.address.logic.commands.questioncommands.AddQuestionCommand; +import seedu.address.logic.commands.questioncommands.DeleteQuestionCommand; +import seedu.address.logic.commands.questioncommands.ListQuestionCommand; import seedu.address.logic.commands.quiz.QuizModeCommand; import seedu.address.logic.commands.statistics.GetQnsCommand; import seedu.address.logic.commands.statistics.GetReportCommand; @@ -26,6 +28,7 @@ import seedu.address.logic.commands.task.DoneTaskCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.logic.parser.questionparser.AddQuestionCommandParser; +import seedu.address.logic.parser.questionparser.DeleteQuestionCommandParser; import seedu.address.logic.parser.quiz.QuizModeCommandParser; import seedu.address.logic.parser.statistics.GetQnsCommandParser; import seedu.address.logic.parser.statistics.GetReportCommandParser; @@ -100,6 +103,12 @@ public Command parseCommand(String userInput) throws ParseException { case AddQuestionCommand.COMMAND_WORD: return new AddQuestionCommandParser().parse(arguments); + case DeleteQuestionCommand.COMMAND_WORD: + return new DeleteQuestionCommandParser().parse(arguments); + + case ListQuestionCommand.COMMAND_WORD: + return new ListQuestionCommand(); + case AddTaskForNoteCommand.COMMAND_WORD: return new AddTaskForNoteCommandParser().parse(arguments); diff --git a/src/main/java/seedu/address/logic/parser/questionparser/DeleteQuestionCommandParser.java b/src/main/java/seedu/address/logic/parser/questionparser/DeleteQuestionCommandParser.java new file mode 100644 index 00000000000..ffa891e9a7d --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/questionparser/DeleteQuestionCommandParser.java @@ -0,0 +1,31 @@ +package seedu.address.logic.parser.questionparser; + +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.questioncommands.DeleteQuestionCommand; +import seedu.address.logic.parser.Parser; +import seedu.address.logic.parser.ParserUtil; +import seedu.address.logic.parser.exceptions.ParseException; + +/** + * Parses input arguments and creates a new DeleteQuestionCommand object + */ +public class DeleteQuestionCommandParser implements Parser { + + /** + * Parses the given {@code String} of arguments in the context of the DeleteQuestionCommand + * and returns a DeleteQuestionCommand object for execution. + * @throws ParseException if the user input does not conform the expected format + */ + public DeleteQuestionCommand parse(String args) throws ParseException { + try { + Index index = ParserUtil.parseIndex(args); + return new DeleteQuestionCommand(index); + } catch (ParseException pe) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteQuestionCommand.MESSAGE_USAGE), pe); + } + } + +} diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 4fac26568f1..a44fc22aeb8 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -227,7 +227,7 @@ public void updateFilteredQuestionList(Predicate predicate) { filteredQuestions.setPredicate(predicate); } - //=========== Filtered Question List Accessors ========================================================= + //=========== Filtered Quiz Question List Accessors ========================================================= @Override public ObservableList getFilteredQuizQuestionList() { diff --git a/src/main/java/seedu/address/ui/MainWindow.java b/src/main/java/seedu/address/ui/MainWindow.java index 7fa0d8a090d..1cc5ef99cd8 100644 --- a/src/main/java/seedu/address/ui/MainWindow.java +++ b/src/main/java/seedu/address/ui/MainWindow.java @@ -35,6 +35,7 @@ public class MainWindow extends UiPart { // Independent Ui parts residing in this Ui container private NoteListPanel noteListPanel; private TaskListPanel taskListPanel; + private QuestionListPanel questionListPanel; private QuizQuestionListPanel quizQuestionListPanel; private ResultDisplay resultDisplay; private HelpWindow helpWindow; @@ -52,6 +53,9 @@ public class MainWindow extends UiPart { @FXML private StackPane taskListPanelPlaceholder; + @FXML + private StackPane questionListPanelPlaceholder; + @FXML private StackPane quizQuestionListPanelPlaceholder; @@ -126,6 +130,9 @@ void fillInnerParts() { taskListPanel = new TaskListPanel(logic.getFilteredTaskList()); taskListPanelPlaceholder.getChildren().add(taskListPanel.getRoot()); + questionListPanel = new QuestionListPanel(logic.getFilteredQuestionList()); + questionListPanelPlaceholder.getChildren().add(questionListPanel.getRoot()); + quizQuestionListPanel = new QuizQuestionListPanel(logic.getFilteredQuizQuestionList()); quizQuestionListPanelPlaceholder.getChildren().add(quizQuestionListPanel.getRoot()); diff --git a/src/main/java/seedu/address/ui/QuestionCard.java b/src/main/java/seedu/address/ui/QuestionListCard.java similarity index 84% rename from src/main/java/seedu/address/ui/QuestionCard.java rename to src/main/java/seedu/address/ui/QuestionListCard.java index a5a934896f2..b2ae83a5db2 100644 --- a/src/main/java/seedu/address/ui/QuestionCard.java +++ b/src/main/java/seedu/address/ui/QuestionListCard.java @@ -9,7 +9,7 @@ /** * An UI component that displays information of a {@code Question}. */ -public class QuestionCard extends UiPart { +public class QuestionListCard extends UiPart { private static final String FXML = "QuestionListCard.fxml"; public final Question question; @@ -27,7 +27,7 @@ public class QuestionCard extends UiPart { @FXML private Label difficulty; - public QuestionCard(Question question, int displayedIndex) { + public QuestionListCard(Question question, int displayedIndex) { super(FXML); this.question = question; id.setText(displayedIndex + ". "); @@ -45,12 +45,12 @@ public boolean equals(Object other) { } // instanceof handles nulls - if (!(other instanceof QuestionCard)) { + if (!(other instanceof QuestionListCard)) { return false; } // state check - QuestionCard card = (QuestionCard) other; + QuestionListCard card = (QuestionListCard) other; return id.getText().equals(card.id.getText()) && question.equals(card.question); } diff --git a/src/main/java/seedu/address/ui/QuestionListPanel.java b/src/main/java/seedu/address/ui/QuestionListPanel.java index 761f88d812a..82654a91592 100644 --- a/src/main/java/seedu/address/ui/QuestionListPanel.java +++ b/src/main/java/seedu/address/ui/QuestionListPanel.java @@ -38,7 +38,7 @@ protected void updateItem(Question question, boolean empty) { setGraphic(null); setText(null); } else { - setGraphic(new QuestionCard(question, getIndex() + 1).getRoot()); + setGraphic(new QuestionListCard(question, getIndex() + 1).getRoot()); } } }