Proponuję, żebyś trzymał same pytania w formacie json
. Dlaczego? Poniewaz jest to jeden ze standardow. Drugim najpopularniejszym jest xml. Musisz tylko poszukac jak to sparsowac (google: parse json java
) i zamienic kazde pytanie na obiekt klasy Question
{
"questions":[
{
"question":"Which material is most dense?",
"answers":[
"Silver",
"Styrofoam",
"Butter",
"Gold"
],
"correct":3
}
]
}
Już w samej aplikacji możesz utworzyć sobie taką strukturę (z samej Javy za dużo nie wiem, więc...)
class Question {
private String body;
private List<String> answers;
private int correctAnswer;
public Question(String body, List<String> answers, int correctAnswer) {
this.body = body;
this.answers = answers;
this.correctAnswer = correctAnswer;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
char ch = 65;
for (String answer : answers) {
String formattedAnswer = ch++ + ") " + answer + "\n";
sb.append(formattedAnswer);
}
return body + "\n" + sb;
}
}
Przykład
public class App {
public static void main(String[] args) {
List<Question> questions = new ArrayList<>();
List<String> answers = new ArrayList<>();
answers.add("1");
answers.add("2");
answers.add("3");
answers.add("4");
Question question = new Question("Pytanie?", answers, 3);
questions.add(question);
questions.forEach(System.out::println);
}
}
Pytanie?
A) 1
B) 2
C) 3
D) 4