Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1920S1#122 from fabbbbbbyy/master
Browse files Browse the repository at this point in the history
Add edge cases
  • Loading branch information
Russell-Loh-NUS authored Nov 1, 2019
2 parents efbe5b5 + c575993 commit 6c67479
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 43 deletions.
47 changes: 24 additions & 23 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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`

Expand All @@ -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`

Expand All @@ -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`

Expand All @@ -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`

Expand All @@ -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`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public QuizCreateManuallyCommand(HashMap<String, String> fields) {

ArrayList<Integer> 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;
Expand All @@ -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());
}
}

/**
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ public interface Model {
/**
* Creates a quiz manually.
*/
void createQuizManually(String quizId, ArrayList<Integer> questionNumbers);
boolean createQuizManually(String quizId, ArrayList<Integer> 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
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,13 @@ public ObservableList<Question> getSlideshowQuestions() {
//region Quizzes

@Override
public void createQuizManually(String quizId, ArrayList<Integer> questionNumbers) {
savedQuizzes.createQuizManually(quizId, questionNumbers, savedQuestions);
public boolean createQuizManually(String quizId, ArrayList<Integer> 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
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/seedu/address/model/quiz/QuizManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> questionNumbers,
public static boolean createQuizManually(String quizId, ArrayList<Integer> questionNumbers,
SavedQuestions savedQuestions, QuizBank quizBank) {
Quiz quiz = new Quiz(quizId);
QuestionBank questionBank = savedQuestions.getQuestionBank();
int questionBankSize = questionBank.getAllQuestions().size();

ArrayList<Question> questions = new ArrayList<>();
for (Integer i : questionNumbers) {
if (i < 0 || i > questionBankSize) {
return false;
}
questions.add(questionBank.getQuestion(Index.fromOneBased(i)));
}

Expand All @@ -44,6 +49,7 @@ public static void createQuizManually(String quizId, ArrayList<Integer> question
}

quizBank.addQuiz(quiz);
return true;
}

/**
Expand All @@ -53,8 +59,9 @@ public static void createQuizManually(String quizId, ArrayList<Integer> 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();
Expand All @@ -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);
Expand All @@ -92,8 +103,8 @@ public static void createQuizAutomatically(String quizId, int numQuestions, Stri
quiz.addQuestion(q);
}
}

quizBank.addQuiz(quiz);
return true;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/address/model/quiz/SavedQuizzes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> questionNumbers,
public boolean createQuizManually(String quizId, ArrayList<Integer> questionNumbers,
SavedQuestions savedQuestions) {
QuizManager.createQuizManually(quizId, questionNumbers, savedQuestions, quizzes);
return QuizManager.createQuizManually(quizId, questionNumbers, savedQuestions, quizzes);
}

/**
Expand All @@ -85,10 +86,11 @@ public void createQuizManually(String quizId, ArrayList<Integer> 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);
}

/**
Expand Down

0 comments on commit 6c67479

Please sign in to comment.