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` 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..7fb04c69671 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/logic/commands/quiz/QuizCreateManuallyCommand.java b/src/main/java/seedu/address/logic/commands/quiz/QuizCreateManuallyCommand.java index dc849fde13b..8b5db2dc7a7 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; @@ -55,9 +58,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 +81,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 e9ac8aeff6f..f9780d709be 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -309,12 +309,12 @@ public interface Model { /** * Creates a quiz manually. */ - void createQuizManually(String quizId, ArrayList questionNumbers); + boolean createQuizManually(String quizId, ArrayList questionNumbers); /** * 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..fe7704ff516 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -512,13 +512,13 @@ 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 - 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..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; } /** @@ -53,8 +59,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 +83,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 +103,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..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); } /** @@ -85,10 +86,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); } /**