/// <summary> /// Saves the specified profile. /// </summary> private void Save() { if (!Directory.Exists(PathToResults)) { Directory.CreateDirectory(PathToResults); } string [] answer = new string[WorkingProfile.Items.Count]; for (int i = 0; i < WorkingProfile.Items.Count(); i++) { answer[i] = $"{i + 1}. {WorkingProfile.Items[i].Question.QuestionName}{SurveyConst.Separator}{WorkingProfile.Items[i].Answer}"; } string path = Path.Combine(PathToResults, $"{WorkingProfile.ProfileId}.txt"); using (var writer = new StreamWriter(path)) { foreach (var s in answer) { writer.WriteLine(s, Environment.NewLine); } writer.WriteLine($"{SurveyConst.ProfileWasCreated}{SurveyConst.Separator}{DateTime.Now.ToString(SurveyConst.FormatDate)}", Environment.NewLine); } WriterAndReaderWorker.WriteLine($"{SurveyConst.FileWasSavedByPath} {Path.GetFullPath(path)}"); }
/// <summary> /// Goes to the specified question. /// </summary> /// <param name="userInput">user input</param> private void GoToTheQuestion(string userInput) { int commandLenght = CommandsList.CommandProfileGoToQuestion.Length; if (userInput.Length > commandLenght) { string indexString = userInput.Substring(commandLenght, userInput.Length - commandLenght).TrimStart().TrimEnd(); int index; if (int.TryParse(indexString, out index)) { if (index < 1 || index > WorkingProfile.Items.Count) { string error = ErrorMessages.QuestionNumberMustBeBetweenMinAndMax.Replace("max", WorkingProfile.Items.Count.ToString()); WriterAndReaderWorker.WriteLine(error); } } else { WriterAndReaderWorker.WriteLine(ErrorMessages.QuestionNumberIsNotNumber); } SetCurrIndex(index - 1); Question(m_CurrProfileItem); } else { WriterAndReaderWorker.WriteLine(ErrorMessages.QuestionNumberWasNotSpecified); } Question(m_CurrProfileItem); }
/// <summary> /// Checks the answer. If correct - goes to the next question. /// </summary> /// <param name="userInput">User input</param> private void CheckAnswer(string userInput) { try { m_CurrProfileItem.Answer = m_CurrProfileItem.Question.CheckedAnswer(userInput); if (WorkingProfile.Items.Any(n => n.Answer == "")) { SetCurrIndex(WorkingProfile.Items.FindIndex(n => n.Answer == "")); } else { m_ProfileComplete = true; } } catch (SurveyException QuestionExeption) { WriterAndReaderWorker.WriteLine(QuestionExeption.Message); } finally { if (m_ProfileComplete) { WriterAndReaderWorker.WriteLine($"{SurveyConst.SaveProfile}{CommandsList.CommandSave}"); WriterAndReaderWorker.WriteLine($"{SurveyConst.StartMessages}"); } else { Question(m_CurrProfileItem); } } }
/// <summary> /// Gets the list of files in the result directory. /// </summary> private void GetAllSavedProfile() { foreach (var file in m_files) { WriterAndReaderWorker.WriteLine(Path.GetFileNameWithoutExtension(file)); } }
/// <summary> /// Writes the average age. /// </summary> private void WriteAge() { string value = ErrorMessages.DataNotFound; if (m_Ages.Any()) { var averageAge = (int)Math.Round(m_Ages.Average(n => n), 0); value = $"{averageAge} {averageAge.GetRussianYears()}"; } WriterAndReaderWorker.WriteLine($"{SurveyConst.OutputMessageAverageAge}{SurveyConst.Separator}{value}"); }
/// <summary> /// Reads lines form a file, and shows them in the console. /// </summary> /// <param name="fullPath">path to the profile</param> private void VeiwProfile(string fullPath) { SavedProfileReader savedProfile = new SavedProfileReader(fullPath); if (savedProfile.ProfileStrings.Any()) { foreach (var str in savedProfile.ProfileStrings) { WriterAndReaderWorker.WriteLine(str); } } }
/// <summary> /// Goes to the previous question. /// </summary> private void GoToPrevios() { if (m_CurrIndex > 0) { SetCurrIndex(m_CurrIndex--); } else { WriterAndReaderWorker.WriteLine(ErrorMessages.FirstElementWasReached); } Question(m_CurrProfileItem); }
/// <summary> /// Gets the profiles, that was saved today. /// </summary> private void GetTodaySavedProfile() { foreach (var file in m_files) { SavedProfileReader savedProfile = new SavedProfileReader(file); if (savedProfile.DateCreation >= DateTime.Today && savedProfile.DateCreation < DateTime.Today.AddDays(1)) { WriterAndReaderWorker.WriteLine(Path.GetFileNameWithoutExtension(file)); } } }
/// <summary> /// Writes the most experienced programmer. /// </summary> private void WriteProgrammingLang() { string value = ErrorMessages.DataNotFound; if (m_Languages.Any()) { value = m_Languages.GroupBy(v => v) .Select(s => new { s, count = s.Count() }) .OrderByDescending(n => n.count) .First() .s.Key; } WriterAndReaderWorker.WriteLine($"{SurveyConst.OutputMessageProgrammingLang}{SurveyConst.Separator}{value}"); }
/// <summary> /// Displays the question and receives the response from the user. /// </summary> /// <param name="q">question</param> private void Question(ProfileItem q) { if (m_ProfileComplete) { return; } q.Answer = ""; WriterAndReaderWorker.WriteLine(q.Question.QuestionText); string userInput = WriterAndReaderWorker.ReadLine(); CheckInput(userInput); }
/// <summary> /// Checks user input. Finds a command. /// </summary> /// <param name="userInput">User input</param> private void CheckInput(string userInput) { if (string.IsNullOrEmpty(userInput)) { WriterAndReaderWorker.WriteLine(ErrorMessages.EmptyText); } else if (userInput == CommandsList.CommandProfileGoToPrevioseQuestion) { GoToPrevios(); } else if (userInput == CommandsList.CommandProfileRestart) { ProfileRestart(); } else if (userInput.Contains(CommandsList.CommandProfileGoToQuestion)) { GoToTheQuestion(userInput); } else { CheckAnswer(userInput); } }
/// <summary> /// Writes the most popular language. /// </summary> private void WriteMaxExpPerson() { string value = string.IsNullOrEmpty(m_FIOmaxExp) ? ErrorMessages.DataNotFound : m_FIOmaxExp; WriterAndReaderWorker.WriteLine($"{SurveyConst.OutputMessageMaxExpPerson}{SurveyConst.Separator}{value}"); }