Skip to content

Instantly share code, notes, and snippets.

@nbyouri
Last active May 23, 2017 12:27
Show Gist options
  • Save nbyouri/557e64a0b9e88663d6d48b035db80350 to your computer and use it in GitHub Desktop.
Save nbyouri/557e64a0b9e88663d6d48b035db80350 to your computer and use it in GitHub Desktop.
package Calcu;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class QCMFlash {
private ArrayList<Tuple<String, String>> map;
private Random rng;
private static final int[] nbQuestions = new int[]{8, 16, 24, 16, 7, 6, 7, 9, 7, 7, 14, 23};
private static final int totalNbQuestions = 144;
private int[] nbQuestionsSum;
private Tuple<Integer, String> currentQ;
class Tuple<X, Y> {
public final X x;
public final Y y;
Tuple(X x, Y y) {
this.x = x;
this.y = y;
}
public X getFirst() { return this.x; }
public Y getLast() { return this.y; }
}
QCMFlash() throws Exception {
map = new ArrayList<>();
rng = new Random();
nbQuestionsSum = new int[nbQuestions.length];
for (int i = 0; i < nbQuestions.length; i++) {
if (i == 0)
nbQuestionsSum[i] = nbQuestions[i];
else
nbQuestionsSum[i] = nbQuestionsSum[i-1] + nbQuestions[i];
}
if (nbQuestionsSum[nbQuestionsSum.length - 1] != totalNbQuestions) {
throw new Exception("nbQuestions and totalNbQuestions Inconsistent.");
}
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(getClass().getResource("qcm.txt").getFile()), "UTF8"));
try {
String line = br.readLine();
int lino = 1;
int curChapter = 0;
int nbQuestionsCurChapter = 0;
while (line != null) {
if (line.trim().equals("") || line.startsWith("//")) {
if (line.startsWith("//")) {
if (curChapter > 0 && (nbQuestionsCurChapter != nbQuestions[curChapter - 1])) {
throw new Exception("Only read " + nbQuestionsCurChapter + "/" + nbQuestions[curChapter - 1] +
" questions for chapter " + curChapter);
}
curChapter++;
nbQuestionsCurChapter = 0;
}
line = br.readLine();
continue;
}
String[] cols = line.split(";");
if (cols.length != 2) {
throw new Exception("qcm.txt has a broken entry at line " + lino);
}
map.add(new Tuple<String,String>(cols[1], cols[0]));
line = br.readLine();
nbQuestionsCurChapter++;
lino++;
}
if ((lino - 1 != totalNbQuestions) && (totalNbQuestions != map.size())) {
System.out.println(map.size() + " "+lino);
throw new Exception("Amount of questions in qcm.txt and totalNbQuestions Inconsistent.");
}
} finally {
br.close();
}
}
String getRandomQ(int chapter) {
int floor = 0;
int ceil = 0;
if (chapter == 0) {
ceil = totalNbQuestions;
} else {
if (chapter > 1)
floor = nbQuestionsSum[chapter - 2];
ceil = nbQuestions[chapter - 1];
}
int index = floor + rng.nextInt(ceil);
currentQ = new Tuple<Integer, String>(index, map.get(index).getFirst());
return currentQ.getLast();
}
String getQuestionAnswer(int index) {
return map.get(index).getLast();
}
public static void main(String[] args) {
QCMFlash f;
try {
f = new QCMFlash();
JFrame frame = new JFrame("Calcu Mémo");
frame.setSize(400,400);
frame.setLayout(new GridLayout(5, 1));
JLabel question = new JLabel("Question", JLabel.CENTER);
JLabel answer = new JLabel("Réponse", JLabel.CENTER);
JComboBox<String> courseSelection = new JComboBox<String>(
new String[]{"Tous les cours", "Séance 1", "Séance 2", "Séance 3", "Séance 4", "Séance 5",
"Séance 6", "Séance 7", "Séance 8", "Séance 9", "Séance 10", "Séance 11", "Séance 12"});
courseSelection.setSelectedIndex(0);
JButton randomQ = new JButton("Question");
JButton answerB = new JButton("Réponse");
answerB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
answer.setText(f.getQuestionAnswer(f.currentQ.getFirst()));
}
});
randomQ.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
question.setText("<html>"+f.getRandomQ(courseSelection.getSelectedIndex())+"</html>");
answer.setText("Réponse");
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(question);
frame.getContentPane().add(answer);
frame.getContentPane().add(courseSelection);
frame.getContentPane().add(answerB);
frame.getContentPane().add(randomQ);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment