public Poll AddPoll() { Console.Write("Name of poll = "); var name = Console.ReadLine(); if (string.IsNullOrEmpty(name.Trim())) { Console.WriteLine("Cant be empty"); return(null); } Console.WriteLine("How many questions will be in this poll"); if (!int.TryParse(Console.ReadLine(), out var questionsCount)) { Console.WriteLine("Incorrect input , count must be integer "); return(null); } if (questionsCount <= 0) { Console.WriteLine("incorrect input "); return(null); } var questions = new string[questionsCount]; var answers = new List <VariantAnswers>(); Console.WriteLine("Input your questions , if you want stop it , enter 'back'"); for (int i = 0; i < questionsCount; i++) { Console.WriteLine($"Input question number {i+1}"); questions[i] = Console.ReadLine(); if (string.IsNullOrEmpty(questions[i].Trim())) { Console.WriteLine("Cant be empty"); i--; continue; } if (questions[i] == "back") { return(null); } Console.WriteLine("This question will have some variants of answer ? "); Console.WriteLine("If Yes , press Y , else press any another key "); var isVariable = Console.ReadKey(); Console.WriteLine(); if (isVariable.Key == ConsoleKey.Y) { Console.WriteLine("How many variants answer this question will have ?"); if (!int.TryParse(Console.ReadLine(), out var variants)) { Console.WriteLine("Incorrect input , count must be integer "); return(null); } if (variants <= 0) { Console.WriteLine("Incorrect input "); return(null); } var variant = new VariantAnswers(); variant.IndexQuestion = i; var variantsAnswer = new string[variants]; for (var k = 0; k < variants; k++) { Console.WriteLine($"Insert {k + 1} variant "); variantsAnswer[k] = Console.ReadLine(); if (string.IsNullOrEmpty(variantsAnswer[k].Trim())) { Console.WriteLine("Cant be empty"); k--; continue; } } variant.VariantAnswer = variantsAnswer; answers.Add(variant); } Console.WriteLine(); } var poll = new Poll() { Id = _id, PollName = name, Questions = questions, VariantAnswers = answers, Stat = new Statistic() { ManFrom18To30 = 0, ManFrom30 = 0, ManTo18 = 0, WomanFrom18To30 = 0, WomanFrom30 = 0, WomanTo18 = 0, Votes = 0, Answers = new List <ReadyAnswers>() }, PassedLogins = new List <int>() }; return(poll); }
public void MakePoll(List <Poll> polList, RegistrationPerson person) { Console.WriteLine("Select a poll what you want to start "); var sortedPolList = polList.Select(x => x).OrderBy(x => x.Id).ToList(); sortedPolList.ForEach(x => Console.WriteLine($"{x.Id} poll : { x.PollName }")); if (!int.TryParse(Console.ReadLine(), out var idPoll)) { Console.WriteLine("Incorrect input , age must be correct "); return; } Poll selectedPoll; try { selectedPoll = polList.Select(x => x).Single(x => x.Id == idPoll); } catch { Console.WriteLine("Error input , you must choose id "); return; } if (selectedPoll.PassedLogins.Contains(person.Phone)) { Console.WriteLine("You have been voted in this poll , choose another one "); return; } var answers = new string[selectedPoll.Questions.Length]; for (var i = 0; i <= selectedPoll.Questions.Length - 1; i++) { var index = 1; var variantAnswers = new VariantAnswers(); Console.WriteLine(selectedPoll.Questions[i]); try { variantAnswers = selectedPoll.VariantAnswers.Select(x => x).Single(x => x.IndexQuestion == i); foreach (var answer in variantAnswers.VariantAnswer) { Console.WriteLine($"{index} : {answer}"); index++; } } catch { } if (variantAnswers.VariantAnswer == null) { answers[i] = Console.ReadLine(); } else { while (true) { if (!int.TryParse(Console.ReadLine(), out var answerNum)) { Console.WriteLine("Incorrect input , if you want and this poll , without save press 0 "); continue; } if (answerNum == 0) { return; } try { answers[i] = variantAnswers.VariantAnswer[answerNum - 1]; break; } catch { Console.WriteLine("Incorrect this id of answer, if you want and this poll , without save press 0 "); } } } } selectedPoll.PassedLogins.Add(person.Phone); WriteStat(selectedPoll, person); WriteAnswerStat(selectedPoll, person, answers); var newPoll = selectedPoll; polList.Remove(selectedPoll); polList.Add(newPoll); Update.UpdateFile(polList); }