From c720c4836fe3b03c0f6fcd3db6e706c00f709271 Mon Sep 17 00:00:00 2001 From: Friederike von Gruben Date: Thu, 2 Feb 2023 01:03:43 +0100 Subject: [PATCH] refactoring: Outsourced Code Generating the question with possible answers now in outsourced method --- .../org/bitbiome/commands/QuizCommand.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/QuizCommand.java b/src/main/java/org/bitbiome/commands/QuizCommand.java index d4c37dd..5748d7b 100644 --- a/src/main/java/org/bitbiome/commands/QuizCommand.java +++ b/src/main/java/org/bitbiome/commands/QuizCommand.java @@ -22,22 +22,15 @@ public class QuizCommand implements CommandAPI { JSONArray antworten = frage.getJSONArray("antworten"); - String frageString = frage.getString("frage"); - String korrekteAntwort = frage.getString("korrekteAntwort"); + String korrekteAntwort = frage.getString("korrekteAntwort"); print("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"); - print(frageString); - - for (int i = 0; i < antworten.length(); i++) { - String antwort = antworten.getString(i); - print(i + 1 + ". " + antwort); - } + print(generateQuestion(frage, antworten)); int eingabe = quizScanner.nextInt(); - if (answerIsCorrect(eingabe, korrekteAntwort, antworten)) { print("Richtige Antwort!\n"); } else { @@ -60,6 +53,15 @@ public class QuizCommand implements CommandAPI { return new Random().nextInt(length); } + public static String generateQuestion(JSONObject frage, JSONArray answers) { + StringBuilder sb = new StringBuilder(); + sb.append(frage.getString("frage")).append("\n"); + for (int i = 0; i < answers.length(); i++) { + sb.append(i+1).append(". ").append(answers.getString(i)).append("\n"); + } + return sb.toString(); + } + }