Example #1
0
 public TestQuestion(Question question)
 {
     if (question == null)
         throw new ArgumentNullException("question");
     this.Question = question;
     this.Answers = question.Answers.Shuffle().Select(a => new TestAnswer(a)).ToList();
 }
Example #2
0
        Question ReadQuestion()
        {
            if (string.IsNullOrWhiteSpace(_curLine))
                return null;

            var question = new Question { Text = _curLine.Trim() };
            if (question.Text.EndsWith(":"))
            {
                question.Text = question.Text.Substring(0, question.Text.Length - 1);
            }

            try {

                if (!ReadLine())
                    return null;

                int[] nums;
                try
                {
                    nums = _curLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
                }
                catch (FormatException)
                {
                    throw new ApplicationException("Числа в непонятном формате: " + _curLine);
                }
                if (nums.Length < 4)
                {
                    ReadUntilBlankLine();
                    throw new ApplicationException("Чисел должно быть минимум 5: " + _curLine);
                }

                question.Index = nums[0] + 1;
                int answerCount = nums[1];
                question.PointPerAnswer = nums[2];
                int correctAnswerCount = nums[3];
                int[] correctAnswers = nums.Skip(4).ToArray();

                while (ReadLine() && !string.IsNullOrWhiteSpace(_curLine))
                {
                    question.Answers.Add(new Answer
                    {
                        Text = _curLine,
                        IsCorrect = correctAnswers.Contains(question.Answers.Count + 1)
                    });
                }

                if (question.Answers.Count != answerCount || correctAnswerCount != question.CorrectAnswerCount)
                {
                    var msg = new StringBuilder();
                    IEnumerable<Answer> answers;
                    if (question.Answers.Count != answerCount)
                    {
                        msg.AppendFormat("Ответов должно быть {0}, а найдено {1}", answerCount, question.Answers.Count);
                        answers = question.Answers;
                    }
                    else
                    {
                        msg.AppendFormat("Правильных ответов должно быть {0}, а найдено {1}", correctAnswerCount, question.CorrectAnswerCount);
                        answers = question.Answers.Where(a => a.IsCorrect);
                    }
                    msg.AppendLine(":");
                    foreach (var ans in answers)
                    {
                        msg.AppendLine(ans.Text);
                    }

                    throw new ApplicationException(msg.ToString().Trim());
                }

                return question;
            }
            catch(Exception ex) {
                var msg = string.Format("Ошибка в вопросе '{1}'", question.Index, question.Text);
                throw new ApplicationException(msg, ex);
            }
        }