From e71dc82f8582dfe6fbf0a2db1d90f5820ec4c735 Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Mon, 6 Jan 2025 13:24:06 +0100 Subject: [PATCH] =?UTF-8?q?N=C3=A5=20med=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Emne 3/IntroInterface/IntroInterface/IQuestion.cs | 6 ++++++ .../IntroInterface/MultipleChoiceQuestions.cs | 6 +++++- Emne 3/IntroInterface/IntroInterface/Program.cs | 7 ++++--- .../{SimpleAnswerQuestion.cs => SingleAnswerQuestion.cs} | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 Emne 3/IntroInterface/IntroInterface/IQuestion.cs rename Emne 3/IntroInterface/IntroInterface/{SimpleAnswerQuestion.cs => SingleAnswerQuestion.cs} (76%) diff --git a/Emne 3/IntroInterface/IntroInterface/IQuestion.cs b/Emne 3/IntroInterface/IntroInterface/IQuestion.cs new file mode 100644 index 0000000..5c5c60c --- /dev/null +++ b/Emne 3/IntroInterface/IntroInterface/IQuestion.cs @@ -0,0 +1,6 @@ +namespace IntroInterface; + +public interface IQuestion +{ + bool Run(); +} \ No newline at end of file diff --git a/Emne 3/IntroInterface/IntroInterface/MultipleChoiceQuestions.cs b/Emne 3/IntroInterface/IntroInterface/MultipleChoiceQuestions.cs index baa2fe9..7cf3144 100644 --- a/Emne 3/IntroInterface/IntroInterface/MultipleChoiceQuestions.cs +++ b/Emne 3/IntroInterface/IntroInterface/MultipleChoiceQuestions.cs @@ -1,6 +1,6 @@ namespace IntroInterface; -public class MultipleChoiceQuestions +public class MultipleChoiceQuestions: IQuestion { private readonly string _question; private readonly string[] _answers; @@ -23,5 +23,9 @@ public class MultipleChoiceQuestions var answer = _answers[i]; Console.WriteLine(answerNo + ": " + answer); } + Console.WriteLine("Velg Svaralternativ: "); + var selectedAnswerNoStr = Console.ReadLine(); + var selectedAnswerNo = Convert.ToInt32(selectedAnswerNoStr); + return selectedAnswerNo == _correctAnswerNo; } } \ No newline at end of file diff --git a/Emne 3/IntroInterface/IntroInterface/Program.cs b/Emne 3/IntroInterface/IntroInterface/Program.cs index adb3dc7..3109613 100644 --- a/Emne 3/IntroInterface/IntroInterface/Program.cs +++ b/Emne 3/IntroInterface/IntroInterface/Program.cs @@ -1,15 +1,16 @@ using IntroInterface; - var questions = new ???[] + var questions = new IQuestion[] { - new SimpleAnswerQuestion("Hva er 2+2?", "4"), - new MultipleChoiceQuestions("Hva er hovedstaden i Norge?", 3,"Stavern", "Larvik","Oslo"), + new SingleAnswerQuestion("Hva er 2+2?", "4"), + new MultipleChoiceQuestions("Hva er hovedstaden i Norge?", 3,"Stavern", "Larvik","Oslo"), }; var points = 0; foreach (var question in questions) { var isCorrect = question.Run(); + if (isCorrect) { Console.WriteLine("Riktig!"); diff --git a/Emne 3/IntroInterface/IntroInterface/SimpleAnswerQuestion.cs b/Emne 3/IntroInterface/IntroInterface/SingleAnswerQuestion.cs similarity index 76% rename from Emne 3/IntroInterface/IntroInterface/SimpleAnswerQuestion.cs rename to Emne 3/IntroInterface/IntroInterface/SingleAnswerQuestion.cs index 3985708..f4ce4db 100644 --- a/Emne 3/IntroInterface/IntroInterface/SimpleAnswerQuestion.cs +++ b/Emne 3/IntroInterface/IntroInterface/SingleAnswerQuestion.cs @@ -1,11 +1,11 @@ namespace IntroInterface; -public class SimpleAnswerQuestion +public class SingleAnswerQuestion: IQuestion { private readonly string _question; private readonly string _correctanswer; - public SimpleAnswerQuestion(string question, string correctanswer) + public SingleAnswerQuestion(string question, string correctanswer) { _correctanswer = correctanswer; _question = question;