-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestionBank.java
68 lines (59 loc) · 2.1 KB
/
QuestionBank.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
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
public class QuestionBank {
final int TOTAL_QUESTION = 20;
Question[] questions = new Question[TOTAL_QUESTION];
String selection[] = new String[TOTAL_QUESTION];
public QuestionBank(String filePath) {
loadQuestionFromCSV(filePath);
}
public void loadQuestionFromCSV(String filePath) {
try {
File obj = new File(filePath);
Scanner myReader = new Scanner(obj);
if (myReader.hasNextLine()) {
myReader.nextLine(); // Skip the first line (column names)
}
int i = 0;
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] questionFromText = data.split(",");
String[] opt = Arrays.copyOfRange(questionFromText, 2, 6);
questions[i] = new Question(Integer.parseInt(questionFromText[0]), questionFromText[1], opt,
questionFromText[6]);
i++;
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred" + e);
e.printStackTrace();
}
}
public void playQuiz() {
int i = 0;
for (Question question : questions) {
System.out.println("Question no.: " + question.getId());
System.out.println(question.getQuestion());
String[] opt = question.getOpt();
for (String option : opt) {
System.out.println(option);
}
Scanner sc = new Scanner(System.in);
selection[i] = sc.nextLine();
i++;
}
}
public void showScore(){
int score = 0;
for (int i = 0; i < questions.length; i++) {
String answer = questions[i].getAnswer();
String userAnswer = selection[i];
if(answer.equals(userAnswer)){
score++;
}
}
System.out.println("Your score is: " + score);
}
}