private void ReadData(string fileName) { string path = SessionData.currentPath + "\\" + fileName; if (File.Exists(path)) { string pattern = @"([A-Z][a-zA-Z#\+]*_[A-Z][a-z]{2}_\d{4})\s+([A-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)"; Regex rgx = new Regex(pattern); string[] allInputLines = File.ReadAllLines(path); for (int line = 0; line < allInputLines.Length; line++) { if (!string.IsNullOrEmpty(allInputLines[line]) && rgx.IsMatch(allInputLines[line])) { Match currentMatch = rgx.Match(allInputLines[line]); string courseName = currentMatch.Groups[1].Value; string userName = currentMatch.Groups[2].Value; string scoresStr = currentMatch.Groups[3].Value; try { int[] scores = scoresStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToArray(); if (scores.Any(s => s > 100 || s < 0)) { OutputWriter.DisplayException(ExceptionMessages.InvalidScore); continue; } if (scores.Length > SoftUniCourse.NumberOfTasksOnExam) { OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores); continue; } if (!students.ContainsKey(userName)) { students.Add(userName, new SoftUniStudent(userName)); } if (!courses.ContainsKey(courseName)) { courses.Add(courseName, new SoftUniCourse(courseName)); } ICourse course = courses[courseName]; IStudent student = students[userName]; student.EnrollInCourse(course); student.SetMarkOnCourse(courseName, scores); course.EnrollStudent(student); } catch (FormatException fex) { throw new FormatException(fex.Message + $"at line: {line}"); } } } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { throw new InvalidPathException(); } }
private static void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayException(ExceptionMessages.InvalidCommand); }