-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuizz.java
55 lines (45 loc) · 1.63 KB
/
Quizz.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.teacher.quiz;
import com.teacher.quiz.generatorQuestions.QuestionsGenerator;
abstract public class Quizz {
private String numberOfQuestions;
private int goodAnswer = 0;
private long startTime;
private long endTime;
private long timeElapsed;
private QuestionsGenerator generatorOfQuestions;
public Quizz (QuestionsGenerator generator) {
this.generatorOfQuestions = generator;
}
abstract public void displayMessage(String format);
abstract public String retrieveAnswer(String text);
public void start() {
startTime = System.currentTimeMillis();
for (Question question : generatorOfQuestions.generateAllQuestions(Integer.parseInt(getNumberOfQuestions()), this)) {
String answer = retrieveAnswer(question.getText());
checkAnswer(question, answer);
};
endTime = System.currentTimeMillis();
timeElapsed = (endTime - startTime)/1000;
};
public String getNumberOfQuestions() {
return numberOfQuestions = retrieveAnswer("Combien de questions voulez-vous ?");
}
public boolean checkAnswer(Question question, String answer) {
boolean checkAnswer = false;
if (answer.toLowerCase().equals(question.getResponse().toLowerCase()))
checkAnswer = true;
if (checkAnswer) {
displayMessage("Bonne réponse!");
goodAnswer++;
} else {
displayMessage("Mauvaise réponse...");
}
return checkAnswer;
};
public void displayResults(boolean hasPlayed) {
if(hasPlayed) {
displayMessage("\nVous avez " + goodAnswer + "/" + numberOfQuestions + " bonnes réponses !"
+ "\nVous avez mis " + timeElapsed + " secondes pour terminer le quizz.");
}
}
}