Created
May 14, 2019 17:24
-
-
Save LeRoiDesKiwis/d2c7937c808db026f83ea1ca4f1dfa67 to your computer and use it in GitHub Desktop.
Qcm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.leroideskiwis.qcm; | |
import java.util.Scanner; | |
public class Main { | |
private Qcm qcm; | |
private Scanner scanner = new Scanner(System.in); | |
public Main(){ | |
qcm = new Qcm(new Qcm.Question("Quel est la couleur du cheval blanc d'henri quatre ? ", new Qcm.Answer(true, "blanc"), new Qcm.Answer(false, "noir")), | |
new Qcm.Question("Quel est la couleur de quelque chose", new Qcm.Answer(true,"vert"), new Qcm.Answer(false, "bleue"), new Qcm.Answer(true, "cyan"), new Qcm.Answer(true, "chiant :harold:"))); | |
while(qcm.has()){ | |
qcm.showQuestion(); | |
boolean correct; | |
boolean isJust = false; | |
do { | |
try { | |
isJust = qcm.correct(readString("Votre réponse : ")); | |
correct = true; | |
} catch (Exception e) { | |
System.out.println("Votre réponse est incorrect ! Veuillez réessayer"); | |
correct = false; | |
} | |
}while(!correct); | |
System.out.println(isJust ? "Bonne réponse ! Bien joué :D" : "Mauvaise réponse :("); | |
} | |
System.out.println("Vous avez "+qcm.getTotalJust()+" réponse(s) juste(s) !"); | |
} | |
public static void main(String[] args) { | |
new Main(); | |
} | |
private String readString(String string){ | |
System.out.print(string); | |
String readed = scanner.nextLine(); | |
System.out.println(); | |
return readed; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.leroideskiwis.qcm; | |
import java.util.Arrays; | |
public class Qcm { | |
private Question[] questions; | |
private int current = 0; | |
public Qcm(Question... questions) { | |
this.questions = questions; | |
} | |
public boolean has(){ | |
return current < questions.length; | |
} | |
public boolean correct(String response) throws Exception { | |
if(!isNumber(response)) throw new Exception("Need number !"); | |
int responseIndex = Integer.parseInt(response)-1; | |
Question currentQuestion = questions[current]; | |
if(responseIndex < 0 || currentQuestion.answers.length < responseIndex){ | |
throw new IndexOutOfBoundsException(); | |
} | |
Answer answer = currentQuestion.answers[responseIndex]; | |
if(current < questions.length) current++; | |
currentQuestion.setJust(answer.isJust()); | |
return answer.isJust(); | |
} | |
public int getTotalJust(){ | |
return (int)Arrays.stream(questions).filter(Question::isJust).count(); | |
} | |
public void showQuestion(){ | |
System.out.println(questions[current].toString()); | |
} | |
private boolean isNumber(String s){ | |
try{ | |
Integer.parseInt(s); | |
return true; | |
}catch(Exception ex){ | |
return false; | |
} | |
} | |
public static class Question { | |
private Answer[] answers; | |
private String text; | |
private boolean just; | |
public Question(String text,Answer... answers) { | |
this.answers = answers; | |
this.text = text; | |
} | |
public Answer[] getAnswers() { | |
return answers; | |
} | |
public String getText() { | |
return text; | |
} | |
public boolean isJust() { | |
return just; | |
} | |
@Override | |
public String toString() { | |
StringBuilder builder = new StringBuilder("Il y a "+answers.length+" réponses possibles : "); | |
for (int i = 0; i < answers.length; i++) { | |
Answer answer = answers[i]; | |
builder.append("\n").append(i + 1).append(". ").append(answer.getText()); | |
} | |
return builder.toString(); | |
} | |
public void setJust(boolean just) { | |
this.just = just; | |
} | |
} | |
public static class Answer { | |
private boolean just; | |
private String text; | |
public Answer(boolean just, String text) { | |
this.just = just; | |
this.text = text; | |
} | |
public boolean isJust() { | |
return just; | |
} | |
public String getText() { | |
return text; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bah testes...