From 803ed2f6fc67f6c4d90cefd6e91f6f6ab10667e3 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 00:19:13 +0100 Subject: [PATCH] Update QuizCommand.java Random question picked from json file and printed out with answeres --- .../org/bitbiome/commands/QuizCommand.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index cdcf4e1..3d10aea 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -1,12 +1,35 @@ package org.bitbiome.commands; +import org.bitbiome.classes.JsonParser; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.Random; import java.util.Scanner; public class QuizCommand implements CommandAPI { @Override public void performCommand(Scanner scanner, boolean isRunning, String message) { + String path = "src\\main\\resources\\quiz.json"; + JSONObject quiz = JsonParser.readJSONFile(path); + + JSONArray fragen = quiz.getJSONArray("Quiz"); + int index = new Random().nextInt(fragen.length()); + JSONObject frage = fragen.getJSONObject(index); + JSONArray antworten = frage.getJSONArray("antworten"); + + System.out.println("Du hast das Quiz gestartet! Hinweis: Wähle deine Antwort, indem du die Zahl (1-4) eingibst. Ist deine Lösung richtig, erhälst du 5 Münzen. Viel Erfolg! \n"); + + String frageString = frage.getString("frage"); + + System.out.println(frageString); + + for (int i = 0; i < antworten.length(); i++) { + String antwort = antworten.getString(i); + System.out.println(i + 1 + ". " + antwort); + } } }