Nå med interface

This commit is contained in:
Geir Okkenhaug Jerstad 2025-01-06 13:24:06 +01:00
parent 9f4483e85f
commit e71dc82f85
4 changed files with 17 additions and 6 deletions

View file

@ -0,0 +1,6 @@
namespace IntroInterface;
public interface IQuestion
{
bool Run();
}

View file

@ -1,6 +1,6 @@
namespace IntroInterface; namespace IntroInterface;
public class MultipleChoiceQuestions public class MultipleChoiceQuestions: IQuestion
{ {
private readonly string _question; private readonly string _question;
private readonly string[] _answers; private readonly string[] _answers;
@ -23,5 +23,9 @@ public class MultipleChoiceQuestions
var answer = _answers[i]; var answer = _answers[i];
Console.WriteLine(answerNo + ": " + answer); Console.WriteLine(answerNo + ": " + answer);
} }
Console.WriteLine("Velg Svaralternativ: ");
var selectedAnswerNoStr = Console.ReadLine();
var selectedAnswerNo = Convert.ToInt32(selectedAnswerNoStr);
return selectedAnswerNo == _correctAnswerNo;
} }
} }

View file

@ -1,15 +1,16 @@
using IntroInterface; using IntroInterface;
var questions = new ???[] var questions = new IQuestion[]
{ {
new SimpleAnswerQuestion("Hva er 2+2?", "4"), new SingleAnswerQuestion("Hva er 2+2?", "4"),
new MultipleChoiceQuestions("Hva er hovedstaden i Norge?", 3,"Stavern", "Larvik","Oslo"), new MultipleChoiceQuestions("Hva er hovedstaden i Norge?", 3,"Stavern", "Larvik","Oslo"),
}; };
var points = 0; var points = 0;
foreach (var question in questions) foreach (var question in questions)
{ {
var isCorrect = question.Run(); var isCorrect = question.Run();
if (isCorrect) if (isCorrect)
{ {
Console.WriteLine("Riktig!"); Console.WriteLine("Riktig!");

View file

@ -1,11 +1,11 @@
namespace IntroInterface; namespace IntroInterface;
public class SimpleAnswerQuestion public class SingleAnswerQuestion: IQuestion
{ {
private readonly string _question; private readonly string _question;
private readonly string _correctanswer; private readonly string _correctanswer;
public SimpleAnswerQuestion(string question, string correctanswer) public SingleAnswerQuestion(string question, string correctanswer)
{ {
_correctanswer = correctanswer; _correctanswer = correctanswer;
_question = question; _question = question;