none
What schould be between the parethenses on addquestion ? RRS feed

  • Question

  • Hello, 

    I have this code : 

    using System;
    using System.Collections.Generic;
    
    public class Program
    {
        public static void Main()
        {
    		var survey = new Survey("My Survey");
    		survey.AddQuestion(); 
    		var Score = survey.GetScore(); 
    		Console.WriteLine("Your score: {Score}"); 
        }
    }
    
    public abstract class Answer
    {
        public int Score { get; set; }
    }
    
    public abstract class Question
    {
        public string Label { get; set; }
    
        protected abstract Answer CreateAnswer(string input);
    
        protected virtual void PrintQuestion()
        {
            Console.WriteLine(Label);
        }
    
        public Answer Ask()
        {
            PrintQuestion();
    
            string input = Console.ReadLine();
    
            return CreateAnswer(input);
        }
    }
    
    public class TextAnswer : Answer
    {
        public string Text { get; set; }
    }
    
    public class TextQuestion : Question
    {
        protected override Answer CreateAnswer(string input)
        {
            return new TextAnswer { Text = input, Score = input.Length };
        }
    }
    
    public class Survey
    {
        public Survey(string title)
        {
            Title = title;
            Questions = new List<Question>();
        }
    
        public string Title { get; set; }
    
        public List<Question> Questions { get; private set; }
    
        public void AddQuestion(Question question)
        {
            Questions.Add(question);
        }
    
        public int GetScore()
        {
            int total = 0;
            foreach (Question question in Questions)
            {
                Answer answer = question.Ask();
                total = total + answer.Score;
            }
    
            return total;
        }
    }

    but now I wonder what schould be between the parentheses on addQuestion on main. 

    I tried a string but I need a Question but that one is abstract. 

    Friday, September 29, 2017 7:58 AM

Answers

  • Hi Roelof1967,

    As you made the class "Question" as abstract so an instance cannot be created. You are trying to store a string in the "List<Question>" so you need access the string "Label" defined in your abstract Question class. In order to do that you can make another class that inherits from the abstract class "Question" and create instances from the child (non-abstract) class. in this case "TextQuestion" is the one and call the Model. So make an instance and assign "Label" value to "Questions" list. Please look into below BOLD lines. Hope this will help you.

    using System;
    using System.Collections.Generic;
    
    public class Program
    {
        public static void Main()
        {
            var survey = new Survey("My Survey");
            survey.AddQuestion("abc");
            var Score = survey.GetScore();
            Console.WriteLine("Your score: {Score}");
        }
    }
    
    public abstract class Answer
    {
        public int Score { get; set; }
    }
    
    public abstract class Question
    {
        public string Label { get; set; }
    
        protected abstract Answer CreateAnswer(string input);
    
        protected virtual void PrintQuestion()
        {
            Console.WriteLine(Label);
        }
    
        public Answer Ask()
        {
            PrintQuestion();
    
            string input = Console.ReadLine();
    
            return CreateAnswer(input);
        }
    }
    
    public class TextAnswer : Answer
    {
        public string Text { get; set; }
    }
    
    public class TextQuestion : Question
    {
        protected override Answer CreateAnswer(string input)
        {
            return new TextAnswer { Text = input, Score = input.Length };
        }
    }
    
    public class Survey
    {
        public Survey(string title)
        {
            Title = title;
            Questions = new List<Question>();
        }
    
        public string Title { get; set; }
    
        public List<Question> Questions { get; private set; }
    
        public void AddQuestion(string question)
        {
            TextQuestion t = new TextQuestion();
            t.Label = question;        
            Questions.Add(t);
        }
    
        public int GetScore()
        {
            int total = 0;
            foreach (Question question in Questions)
            {
                Answer answer = question.Ask();
                total = total + answer.Score;
            }
    
            return total;
        }
    }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Friday, September 29, 2017 12:49 PM
    Moderator