Пример #1
0
        private static void RunQuiz(Student student)
        {
            Console.Clear();
            Console.WriteLine("Running quiz");
            UserRepo.ClearStudentScore(student);
            var questionRepo = new QuestionRepository();
            var questions    = questionRepo.GetQuestions();

            foreach (var question in questions)
            {
                while (true)
                {
                    Console.Clear();
                    Console.WriteLine(question.Text);
                    Console.WriteLine();

                    for (int index = 0; index < question.Answers.Count; index++)
                    {
                        Console.WriteLine($"{index + 1}: {question.Answers[index]}");
                    }

                    Console.WriteLine();
                    var response = Console.ReadLine();
                    if (!int.TryParse(response, out int result))
                    {
                        Console.WriteLine($"Please enter a valid number 1-{question.Answers.Count}");
                        Console.ReadKey();
                        continue;
                    }
                    if (result > question.Answers.Count)
                    {
                        Console.WriteLine($"Please enter a valid number 1-{question.Answers.Count}");
                        Console.ReadKey();
                        continue;
                    }
                    if (result <= 0)
                    {
                        Console.WriteLine($"Please enter a valid number 1-{question.Answers.Count}");
                        Console.ReadKey();
                        continue;
                    }
                    var answer = question.Answers[result - 1];
                    if (questionRepo.CheckQuestion(question, answer))
                    {
                        UserRepo.IncreaseScore(student);
                    }
                    break;
                }
            }
            UserRepo.FinishQuiz(student);

            Console.Clear();
            Console.WriteLine($"You have finished the quiz with {student.Score} points");
            Console.WriteLine($"Press enter to exit");

            Console.ReadKey();
        }