diff --git a/Lab04/.classpath b/Lab04/.classpath new file mode 100644 index 0000000..d171cd4 --- /dev/null +++ b/Lab04/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Lab04/.project b/Lab04/.project new file mode 100644 index 0000000..e44e2cf --- /dev/null +++ b/Lab04/.project @@ -0,0 +1,17 @@ + + + Lab04 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Lab04/src/lab04/Lab04.java b/Lab04/src/lab04/Lab04.java new file mode 100644 index 0000000..e1c99f4 --- /dev/null +++ b/Lab04/src/lab04/Lab04.java @@ -0,0 +1,13 @@ +package lab04; + +public class Lab04 { + + public static void main(String[] args) { + Quiz quiz = new Quiz(new String[] { "5*5 =", "12-7 =", "9/3 =", "12+10 =", "55/5 =" }, + new String[][] { { "25", "62", "11", "5" }, { "75", "5", "11", "3" }, { "1", "2", "3", "4" }, + { "55", "66", "77", "22" }, { "111", "1111", "1", "11" } }, + new int[] { 0, 1, 2, 3, 3 }); + quiz.start(); + } + +} diff --git a/Lab04/src/lab04/Question.java b/Lab04/src/lab04/Question.java new file mode 100644 index 0000000..6222cb4 --- /dev/null +++ b/Lab04/src/lab04/Question.java @@ -0,0 +1,82 @@ +package lab04; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; + +public class Question { + private int questionNumber; + private String question; + private String[] answers; + private int correct; + private QuestionResponseEventHandler responseHandler; + + private JFrame frame; + + public Question(int questionNumber, String question, String[] answers, int correct, + QuestionResponseEventHandler responseHandler) throws Exception { + this.questionNumber = questionNumber; + this.question = question; + this.answers = answers; + this.correct = correct; + this.responseHandler = responseHandler; + + if (answers.length != 4) { + throw new Exception("Should be exactly 4 answers"); + } + if (correct < 0 || correct > 3) { + throw new Exception("Correct answer should be valid"); + } + + this.renderForm(); + } + + private void renderForm() { + frame = new JFrame("Question #" + String.valueOf(this.questionNumber)); + + JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + + panel.add(new JLabel(question)); + + JPanel answers = new JPanel(); + + Question that = this; + for (int i = 0; i < 4; i++) { + JButton button = new JButton(this.answers[i]); + boolean isCorrect = i == correct; + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + that.handleAnswer(isCorrect); + } + }); + answers.add(button); + } + + panel.add(answers); + + frame.add(panel); + + frame.setSize(500, 150); + frame.setVisible(true); + } + + private void handleAnswer(boolean isCorrect) { + if (isCorrect) { + JOptionPane.showMessageDialog(frame, "Your answer is correct!", "Hurray!", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(frame, "Your answers is wrong. The correct is: " + this.answers[this.correct], + "Oof", JOptionPane.ERROR_MESSAGE); + } + + this.frame.setVisible(false); + this.responseHandler.onResponse(isCorrect); + } +} diff --git a/Lab04/src/lab04/QuestionResponseEventHandler.java b/Lab04/src/lab04/QuestionResponseEventHandler.java new file mode 100644 index 0000000..6619930 --- /dev/null +++ b/Lab04/src/lab04/QuestionResponseEventHandler.java @@ -0,0 +1,5 @@ +package lab04; + +public interface QuestionResponseEventHandler { + public void onResponse(boolean isCorrect); +} diff --git a/Lab04/src/lab04/Quiz.java b/Lab04/src/lab04/Quiz.java new file mode 100644 index 0000000..fe7bbfc --- /dev/null +++ b/Lab04/src/lab04/Quiz.java @@ -0,0 +1,63 @@ +package lab04; + +import javax.swing.JOptionPane; + +public class Quiz implements QuestionResponseEventHandler { + private String[] questions; + private String[][] answers; + private int[] correct; + + private int answerCorrect; + private int currentQuestion; + + public Quiz(String[] questions, String[][] answers, int[] correct) { + this.questions = questions; + this.answers = answers; + this.correct = correct; + + this.answerCorrect = 0; + this.currentQuestion = -1; + } + + public boolean start() { + if (this.currentQuestion == -1) { + this.nextQuestion(); + return true; + } + + return false; + } + + private void nextQuestion() { + this.currentQuestion++; + + if (this.currentQuestion >= this.questions.length) { + this.finishQuiz(); + return; + } + + try { + new Question(this.currentQuestion + 1, this.questions[this.currentQuestion], + this.answers[this.currentQuestion], this.correct[this.currentQuestion], this); + } catch (Exception e) { + JOptionPane.showMessageDialog(null, "ERROR: Invalid question: " + e.getMessage()); + this.nextQuestion(); + } + } + + private void finishQuiz() { + JOptionPane.showMessageDialog(null, + "Correct answers: " + String.valueOf(this.answerCorrect) + "/" + String.valueOf(this.questions.length) + + " | " + String.valueOf(this.answerCorrect * 100 / this.questions.length) + "%", + "Quiz completed!", JOptionPane.QUESTION_MESSAGE); + } + + @Override + public void onResponse(boolean isCorrect) { + if (isCorrect) { + this.answerCorrect++; + } + + this.nextQuestion(); + } +}