From 12b07a1592242fa13a86afb7d92245558bf5d07e Mon Sep 17 00:00:00 2001 From: fabbbbbbyy Date: Fri, 1 Nov 2019 11:56:49 +0800 Subject: [PATCH 1/6] Add edge cases --- .../quiz/QuizCreateAutomaticallyCommand.java | 19 ++++++++++++++++--- src/main/java/seedu/address/model/Model.java | 2 +- .../seedu/address/model/ModelManager.java | 4 ++-- .../seedu/address/model/quiz/QuizManager.java | 9 +++++++-- .../address/model/quiz/SavedQuizzes.java | 5 +++-- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java index 19e2054978a..038836e6fc5 100644 --- a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java +++ b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java @@ -49,9 +49,14 @@ public CommandResult execute(Model model) throws CommandException { if (model.checkQuizExists(quizId)) { return new CommandResult(String.format(QUIZ_ALREADY_EXISTS, quizId)); } - QuizBank.setCurrentlyQueriedQuiz(quizId); - model.createQuizAutomatically(quizId, numQuestions, type); - return new CommandResult(generateSuccessMessage(), CommandResultType.SHOW_QUIZ_ALL); + + boolean isSuccess = model.createQuizAutomatically(quizId, numQuestions, type); + if(isSuccess) { + QuizBank.setCurrentlyQueriedQuiz(quizId); + return new CommandResult(generateSuccessMessage(), CommandResultType.SHOW_QUIZ_ALL); + } else { + return new CommandResult(generateFailureMessage()); + } } /** @@ -66,6 +71,14 @@ private String generateSuccessMessage() { } } + /** + * Generates a command execution failure message. + * @return The String representation of a failure message. + */ + private String generateFailureMessage() { + return "You do not have enough questions in the storage! Add more questions and try again."; + } + @Override public boolean equals(Object other) { // short circuit if same object diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index e9ac8aeff6f..a8a6e60aaa1 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -314,7 +314,7 @@ public interface Model { /** * Creates a quiz automatically. */ - void createQuizAutomatically(String quizId, int numQuestions, String type); + boolean createQuizAutomatically(String quizId, int numQuestions, String type); /** * Adds a question to a quiz. {@code quizId} Must already exist in the quiz bank. {@code diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index cae26171053..e67bf4c8dc8 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -517,8 +517,8 @@ public void createQuizManually(String quizId, ArrayList questionNumbers } @Override - public void createQuizAutomatically(String quizId, int numQuestions, String type) { - savedQuizzes.createQuizAutomatically(quizId, numQuestions, type, savedQuestions); + public boolean createQuizAutomatically(String quizId, int numQuestions, String type) { + return savedQuizzes.createQuizAutomatically(quizId, numQuestions, type, savedQuestions); } @Override diff --git a/src/main/java/seedu/address/model/quiz/QuizManager.java b/src/main/java/seedu/address/model/quiz/QuizManager.java index acaf66fd4ad..4f877402862 100644 --- a/src/main/java/seedu/address/model/quiz/QuizManager.java +++ b/src/main/java/seedu/address/model/quiz/QuizManager.java @@ -53,8 +53,9 @@ public static void createQuizManually(String quizId, ArrayList question * @param type The type of questions to be added to the quiz. * @param savedQuestions The saved questions. * @param quizBank The quiz bank. + * @return True if the quiz has been created, false if not. */ - public static void createQuizAutomatically(String quizId, int numQuestions, String type, + public static boolean createQuizAutomatically(String quizId, int numQuestions, String type, SavedQuestions savedQuestions, QuizBank quizBank) { Quiz quiz = new Quiz(quizId); QuestionBank questionBank = savedQuestions.getQuestionBank(); @@ -76,6 +77,10 @@ public static void createQuizAutomatically(String quizId, int numQuestions, Stri int listSize = relevantQuestions.size(); + if(listSize < numQuestions) { + return false; + } + if (listSize > numQuestions) { for (int i = 0; i < numQuestions; i++) { int randomQuestionIndex = getRandomQuestionIndex(listSize); @@ -92,8 +97,8 @@ public static void createQuizAutomatically(String quizId, int numQuestions, Stri quiz.addQuestion(q); } } - quizBank.addQuiz(quiz); + return true; } /** diff --git a/src/main/java/seedu/address/model/quiz/SavedQuizzes.java b/src/main/java/seedu/address/model/quiz/SavedQuizzes.java index 120bfe8e640..608b114e0b6 100644 --- a/src/main/java/seedu/address/model/quiz/SavedQuizzes.java +++ b/src/main/java/seedu/address/model/quiz/SavedQuizzes.java @@ -85,10 +85,11 @@ public void createQuizManually(String quizId, ArrayList questionNumbers * @param numQuestions The number of questions to be added to the quiz. * @param type The type of questions to be added to the quiz. * @param savedQuestions The saved questions. + * @return True if the quiz has been created, false if not. */ - public void createQuizAutomatically(String quizId, int numQuestions, String type, + public boolean createQuizAutomatically(String quizId, int numQuestions, String type, SavedQuestions savedQuestions) { - QuizManager.createQuizAutomatically(quizId, numQuestions, type, savedQuestions, quizzes); + return QuizManager.createQuizAutomatically(quizId, numQuestions, type, savedQuestions, quizzes); } /** From d4dfab5df89cef5b417c27afead9bd6a9a4e2da3 Mon Sep 17 00:00:00 2001 From: fabbbbbbyy Date: Fri, 1 Nov 2019 11:57:00 +0800 Subject: [PATCH 2/6] Update UG --- docs/UserGuide.adoc | 47 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 69b9e6f27b0..7d7e81e4deb 100755 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -421,29 +421,30 @@ Format: `quiz manual quizID/... questionNumber/...` The format supported by this feature includes: -- `Quiz ID` (The name of the quiz) -- `Question Numbers` (The question numbers you want to add to the quiz) +. `Quiz ID` (The label of the quiz) +. `Question Numbers` (The question numbers you want to add to the quiz) Examples: * `quiz manual quizID/CS2103T questionNumber/1 2 3` + -Adds question numbers 1, 2 and 3 to the quiz named CS2103T. +Adds question numbers 1, 2 and 3 to the quiz labelled CS2103T. ==== Creating a Quiz automatically: `auto` Allows a user to create a quiz automatically. + +[NOTE] The questions chosen are randomised from the questions that you have previously added. As such, the user has to ensure enough questions are available in storage for quiz creation. Format: `quiz auto quizID/... numQuestions/... type/...` The format supported by this feature includes: -- `Quiz ID` (The name of the quiz) -- `Number of Questions` (The number of questions you want added to the quiz) -- `Question Type` (mcq, open, all) +. `Quiz ID` (The label of the quiz) +. `Number of Questions` (The number of questions you want added to the quiz) +. `Question Type` (mcq, open, all) Examples: * `quiz auto quizID/CS2103T numQuestions/2 type/mcq` + -Adds 2 questions of type mcq to the quiz named CS2103T. +Adds 2 questions of type mcq to the quiz labelled CS2103T. ==== Adding a Question to a Quiz: `add` @@ -452,14 +453,14 @@ Format: `quiz add quizID/... questionNumber/... quizQuestionNumber/...` The format supported by this feature includes: -- `Quiz ID` (The name of your quiz) -- `Question Number` (The question number you want to add to the quiz) -- `Quiz Question Number` (The question number in the quiz you want to add the question to) +. `Quiz ID` (The label of your quiz) +. `Question Number` (The question number you want to add to the quiz) +. `Quiz Question Number` (The question number in the quiz you want to add the question to) Examples: * `quiz add quizID/CS2103T questionNumber/2 quizQuestionNumber/3` + -Adds question 2 to the quiz question number 3 for the quiz named CS2103T. +Adds question 2 to the quiz question number 3 for the quiz labelled CS2103T. ==== Deleting a Question from a Quiz: `delete` @@ -468,27 +469,27 @@ Format: `quiz delete quizID/... quizQuestionNumber/...` The format supported by this feature includes: -- `Quiz ID` (The name of the quiz) -- `Quiz Question Number` (The question number of the question in the quiz to be deleted) +. `Quiz ID` (The label of the quiz) +. `Quiz Question Number` (The question number of the question in the quiz to be deleted) Examples: * `quiz delete quizID/CS2103T quizQuestionNumber/3` + -Deletes the quiz question number 3 for the quiz named CS2103T. +Deletes the quiz question number 3 for the quiz labelled CS2103T. ==== Exporting a Quiz to HTML: `export` -Allows a user to export a quiz to a HTML file. + +Allows a user to export a quiz to a formatted HTML file. + Format: `quiz export quizID/...` The format supported by this feature includes: -- `Quiz ID` (The name of the quiz) +. `Quiz ID` (The label of the quiz) Examples: * `quiz export quizID/CS2103T` + -Exports the quiz named CS2103T to a HTML file. +Exports the quiz labelled CS2103T to a formatted HTML file. ==== Listing a Quiz: `list` @@ -497,12 +498,12 @@ Format: `quiz list quizID/...` The format supported by this feature includes: -- `Quiz ID` (The name of the quiz) +. `Quiz ID` (The label of the quiz) Examples: * `quiz list quizID/CS2103T` + -Lists the questions and answers for the quiz named CS2103T. +Lists the questions and answers for the quiz labelled CS2103T. ==== Showing only a Quiz's Questions: `showQuestions` @@ -511,12 +512,12 @@ Format: `quiz showQuestions quizID/...` The format supported by this feature includes: -- `Quiz ID` (The name of the quiz) +. `Quiz ID` (The label of the quiz) Examples: * `quiz showQuestions quizID/CS2103T` + -Shows only the questions for the quiz named CS2103T. +Shows only the questions for the quiz labelled CS2103T. ==== Showing only a Quiz's Answers: `showAnswers` @@ -525,12 +526,12 @@ Format: `quiz showAnswers quizID/...` The format supported by this feature includes: -- `Quiz ID` (The name of the quiz) +. `Quiz ID` (The label of the quiz) Examples: * `quiz showAnswers quizID/CS2103T` + -Shows only the answers for the quiz named CS2103T. +Shows only the answers for the quiz labelled CS2103T. === Events ​-​ `event` From 6c6cf283d2b99c9afc4e9fece7d7150b3f48aea8 Mon Sep 17 00:00:00 2001 From: fabbbbbbyy Date: Fri, 1 Nov 2019 12:01:53 +0800 Subject: [PATCH 3/6] Fix checkstyle --- .../logic/commands/quiz/QuizCreateAutomaticallyCommand.java | 2 +- src/main/java/seedu/address/model/quiz/QuizManager.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java index 038836e6fc5..7fb04c69671 100644 --- a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java +++ b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateAutomaticallyCommand.java @@ -51,7 +51,7 @@ public CommandResult execute(Model model) throws CommandException { } boolean isSuccess = model.createQuizAutomatically(quizId, numQuestions, type); - if(isSuccess) { + if (isSuccess) { QuizBank.setCurrentlyQueriedQuiz(quizId); return new CommandResult(generateSuccessMessage(), CommandResultType.SHOW_QUIZ_ALL); } else { diff --git a/src/main/java/seedu/address/model/quiz/QuizManager.java b/src/main/java/seedu/address/model/quiz/QuizManager.java index 4f877402862..909d1c9273b 100644 --- a/src/main/java/seedu/address/model/quiz/QuizManager.java +++ b/src/main/java/seedu/address/model/quiz/QuizManager.java @@ -77,7 +77,7 @@ public static boolean createQuizAutomatically(String quizId, int numQuestions, S int listSize = relevantQuestions.size(); - if(listSize < numQuestions) { + if (listSize < numQuestions) { return false; } From 59cd2b9ae8810c3395eb7e4d47fd0b934377980e Mon Sep 17 00:00:00 2001 From: fabbbbbbyy Date: Fri, 1 Nov 2019 12:20:10 +0800 Subject: [PATCH 4/6] Add edge cases --- .../quiz/QuizCreateManuallyCommand.java | 19 ++++++++++++++++--- src/main/java/seedu/address/model/Model.java | 2 +- .../seedu/address/model/ModelManager.java | 4 ++-- .../seedu/address/model/quiz/QuizManager.java | 8 +++++++- .../address/model/quiz/SavedQuizzes.java | 5 +++-- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java index dc849fde13b..21553443223 100644 --- a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java +++ b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java @@ -55,9 +55,14 @@ public CommandResult execute(Model model) throws CommandException { if (model.checkQuizExists(quizId)) { return new CommandResult(String.format(QUIZ_ALREADY_EXISTS, quizId)); } - QuizBank.setCurrentlyQueriedQuiz(quizId); - model.createQuizManually(quizId, questionNumbers); - return new CommandResult(generateSuccessMessage(), CommandResultType.SHOW_QUIZ_ALL); + + boolean isSuccess = model.createQuizManually(quizId, questionNumbers); + if(isSuccess) { + QuizBank.setCurrentlyQueriedQuiz(quizId); + return new CommandResult(generateSuccessMessage(), CommandResultType.SHOW_QUIZ_ALL); + } else { + return new CommandResult(generateFailureMessage()); + } } /** @@ -73,6 +78,14 @@ private String generateSuccessMessage() { } } + /** + * Generates a command execution failure message. + * @return The String representation of a failure message. + */ + private String generateFailureMessage() { + return "You have entered one or more invalid question indexes."; + } + @Override public boolean equals(Object other) { // short circuit if same object diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index a8a6e60aaa1..f9780d709be 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -309,7 +309,7 @@ public interface Model { /** * Creates a quiz manually. */ - void createQuizManually(String quizId, ArrayList questionNumbers); + boolean createQuizManually(String quizId, ArrayList questionNumbers); /** * Creates a quiz automatically. diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index e67bf4c8dc8..fe7704ff516 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -512,8 +512,8 @@ public ObservableList getSlideshowQuestions() { //region Quizzes @Override - public void createQuizManually(String quizId, ArrayList questionNumbers) { - savedQuizzes.createQuizManually(quizId, questionNumbers, savedQuestions); + public boolean createQuizManually(String quizId, ArrayList questionNumbers) { + return savedQuizzes.createQuizManually(quizId, questionNumbers, savedQuestions); } @Override diff --git a/src/main/java/seedu/address/model/quiz/QuizManager.java b/src/main/java/seedu/address/model/quiz/QuizManager.java index 909d1c9273b..11241da5915 100644 --- a/src/main/java/seedu/address/model/quiz/QuizManager.java +++ b/src/main/java/seedu/address/model/quiz/QuizManager.java @@ -28,14 +28,19 @@ public QuizManager() { * @param questionNumbers The question numbers to be added to the quiz. * @param savedQuestions The saved questions. * @param quizBank The quiz bank. + * @return True if the quiz has been created, false if not. */ - public static void createQuizManually(String quizId, ArrayList questionNumbers, + public static boolean createQuizManually(String quizId, ArrayList questionNumbers, SavedQuestions savedQuestions, QuizBank quizBank) { Quiz quiz = new Quiz(quizId); QuestionBank questionBank = savedQuestions.getQuestionBank(); + int questionBankSize = questionBank.getAllQuestions().size(); ArrayList questions = new ArrayList<>(); for (Integer i : questionNumbers) { + if (i < 0 || i > questionBankSize) { + return false; + } questions.add(questionBank.getQuestion(Index.fromOneBased(i))); } @@ -44,6 +49,7 @@ public static void createQuizManually(String quizId, ArrayList question } quizBank.addQuiz(quiz); + return true; } /** diff --git a/src/main/java/seedu/address/model/quiz/SavedQuizzes.java b/src/main/java/seedu/address/model/quiz/SavedQuizzes.java index 608b114e0b6..e39689c8ba5 100644 --- a/src/main/java/seedu/address/model/quiz/SavedQuizzes.java +++ b/src/main/java/seedu/address/model/quiz/SavedQuizzes.java @@ -73,10 +73,11 @@ public void resetData(ReadOnlyQuizzes newData) { * @param quizId The identifier of the quiz to be created. * @param questionNumbers The question numbers to be added to the quiz. * @param savedQuestions The saved questions. + * @return True is quiz has been created, false if not. */ - public void createQuizManually(String quizId, ArrayList questionNumbers, + public boolean createQuizManually(String quizId, ArrayList questionNumbers, SavedQuestions savedQuestions) { - QuizManager.createQuizManually(quizId, questionNumbers, savedQuestions, quizzes); + return QuizManager.createQuizManually(quizId, questionNumbers, savedQuestions, quizzes); } /** From dba75452934f455d81692a2985aad164a53cf77e Mon Sep 17 00:00:00 2001 From: fabbbbbbyy Date: Fri, 1 Nov 2019 12:25:02 +0800 Subject: [PATCH 5/6] Add edge case --- .../logic/commands/quiz/QuizCreateManuallyCommand.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java index 21553443223..78171175974 100644 --- a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java +++ b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java @@ -37,7 +37,10 @@ public QuizCreateManuallyCommand(HashMap fields) { ArrayList questionNumbers = new ArrayList<>(); for (String s : splitQuestionNumbers) { - questionNumbers.add(Integer.parseInt(s)); + int convertedInt = Integer.parseInt(s); + if (!questionNumbers.contains(convertedInt)) { + questionNumbers.add(convertedInt); + } } this.quizId = quizId; From c575993425a572e515e84f53d3b7b3aa95169e87 Mon Sep 17 00:00:00 2001 From: fabbbbbbyy Date: Fri, 1 Nov 2019 12:26:59 +0800 Subject: [PATCH 6/6] Fix checkstyle --- .../address/logic/commands/quiz/QuizCreateManuallyCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java index 78171175974..8b5db2dc7a7 100644 --- a/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java +++ b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java @@ -60,7 +60,7 @@ public CommandResult execute(Model model) throws CommandException { } boolean isSuccess = model.createQuizManually(quizId, questionNumbers); - if(isSuccess) { + if (isSuccess) { QuizBank.setCurrentlyQueriedQuiz(quizId); return new CommandResult(generateSuccessMessage(), CommandResultType.SHOW_QUIZ_ALL); } else {