public void TraverseDirectory(int depth) { OutputWriter.WriteEmptyLine(); int initialIdentation = SessionData.CurrentPath.Split('\\').Length; Queue <string> subFolders = new Queue <string>(); subFolders.Enqueue(SessionData.CurrentPath); while (subFolders.Count != 0) { string current = subFolders.Dequeue(); int identation = current.Split('\\').Length - initialIdentation; if (depth - identation < 0) { break; } OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}", new string('-', identation), current)); try { foreach (var directory in Directory.GetDirectories(current)) { subFolders.Enqueue(directory); } foreach (var file in Directory.GetFiles(current)) { int indexOfLastSlash = file.LastIndexOf("\\"); string filename = file.Substring(indexOfLastSlash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + file); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccess); } } }
private string[] GetLinesWithPossibleMismatches(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; string output = string.Empty; int minOuputLines = actualOutputLines.Length; if (minOuputLines != expectedOutputLines.Length) { hasMismatch = true; minOuputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.WriteMessageOnNewLine(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } string[] mismatches = new string[minOuputLines]; OutputWriter.WriteMessageOnNewLine("Comparing files..."); for (int index = 0; index < minOuputLines; index++) { string actualLine = actualOutputLines[index]; string expectedLine = expectedOutputLines[index]; if (!actualOutputLines.Equals(expectedLine)) { output = string.Format($"Mismatch at line {index} -- expected: \"{expectedLine}\", actual: \"{actualLine}\""); output += Environment.NewLine; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[index] = output; } return(mismatches); }
private static string[] GetLineWithPossibleMismatches( string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch ) { int minOutputLines = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.DisplayException(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } hasMismatch = false; string output; string[] mismatches = new string[actualOutputLines.Length]; OutputWriter.WriteMessageOnNewLine(ComparingFilesMessage); for (int i = 0; i < minOutputLines; i++) { string actualLine = actualOutputLines[i]; string expectedLine = expectedOutputLines[i]; if (!actualLine.Equals(expectedLine)) { output = $"Mismatch at line {i} -- expected: \"{expectedLine}\", actual: \"{actualLine}\""; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[i] = output; } return(mismatches); }
private void PrintOutput(string[] mismatches, bool hasMismatch, string mismatchPath) { if (hasMismatch) { foreach (var line in mismatches) { OutputWriter.WriteMessageOnNewLine(line); } try { File.WriteAllLines(mismatchPath, mismatches); } catch (DirectoryNotFoundException) { throw new InvalidPathException(); } return; } OutputWriter.WriteMessageOnNewLine("Files are identical. There are no mismatches."); }
public void CompareContent(string userOutputPath, string expectedOutputPath) { OutputWriter.WriteMessageOnNewLine("Reading files..."); try { string mismatchPath = GetMismatchPath(expectedOutputPath); string[] actualOutputLines = File.ReadAllLines(userOutputPath); string[] expectedOutputLines = File.ReadAllLines(expectedOutputPath); bool hasMismatch; string[] mismatches = GetLinesWithPossibleMismatches(actualOutputLines, expectedOutputLines, out hasMismatch); PrintOutput(mismatches, hasMismatch, mismatchPath); OutputWriter.WriteMessageOnNewLine("Files Read!"); } catch (IOException) { throw new IOException(ExceptionMessages.FolderNotFoundException); } }
public static void OrderAndTake(Dictionary <string, List <int> > wantedData, string comparison, int studentsToTake) { comparison = comparison.ToLower(); if (comparison == "ascending") { PrintStudents(wantedData .OrderBy(x => x.Value.Sum()) .Take(studentsToTake) .ToDictionary(x => x.Key, x => x.Value)); } else if (comparison == "descending") { PrintStudents(wantedData .OrderByDescending(x => x.Value.Sum()) .Take(studentsToTake) .ToDictionary(x => x.Key, x => x.Value)); } else { OutputWriter.WriteMessageOnNewLine(ExceptionMessages.InvalidQueryComparison); } }
private static void PrintOutput(string[] mismatches, bool hasMismatch, string mismatchPath) { if (hasMismatch) { foreach (string mismatch in mismatches) { OutputWriter.WriteMessageOnNewLine(mismatch); } try { File.WriteAllLines(mismatchPath, mismatches); } catch (DirectoryNotFoundException) { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } } else { OutputWriter.WriteMessageOnNewLine($"Files are identical. There are no mismatches."); } }
private static void ReadData(string fileName) { OutputWriter.WriteMessageOnNewLine("Reading data..."); string currentPath = SessionData.currentPath; fileName = fileName.TrimStart('\\'); currentPath += '\\' + fileName; currentPath = currentPath.TrimEnd('/', '\\'); if (File.Exists(currentPath)) { string[] input = System.IO.File.ReadAllLines(currentPath); for (int i = 0; i < input.Length; i++) { string[] tokens = input[i].Split(' '); string course = tokens[0]; string student = tokens[1]; int mark = int.Parse(tokens[2]); if (!studentsByCourse.ContainsKey(course)) { studentsByCourse.Add(course, new Dictionary <string, List <int> >()); } if (!studentsByCourse[course].ContainsKey(student)) { studentsByCourse[course].Add(student, new List <int>()); } studentsByCourse[course][student].Add(mark); } initializedDataName = fileName; isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } }
public static void CompareContent(string userOutputPath, string expectedOutputPath) { try { OutputWriter.WriteMessageOnNewLine("Reading files..."); string mismatchPath = GetMismatchPath(expectedOutputPath); string[] actualOutputLines = File.ReadAllLines(userOutputPath); string[] expectedOutputLines = File.ReadAllLines(expectedOutputPath); bool hasMismatch; string[] mismatches = GetLineWithPossibleMismatches(actualOutputLines, expectedOutputLines, out hasMismatch); PrintOutput(mismatches, hasMismatch, mismatchPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (FileNotFoundException) { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } }
public static void CompareContent(string userOutputPath, string expectedOutputPath) { OutputWriter.WriteMessageOnNewLine("Reading files..."); try { var mismatchesPath = GetMismatchPath(expectedOutputPath); var actualOutputLines = File.ReadAllLines(userOutputPath); var expectedOutputLine = File.ReadAllLines(expectedOutputPath); bool hasMismatches; var mismatches = GetLineWithPossibleMismatches(actualOutputLines, expectedOutputLine, out hasMismatches); Printoutput(mismatches, hasMismatches, mismatchesPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (FileNotFoundException) { throw new InvalidPathException(); } }
public static void TraverseDirectory(int depth) { OutputWriter.WriteEmptyLine(); int initialIndentation = SessionData.currentPath.Split('\\').Length; Queue <string> subfolders = new Queue <string>(); subfolders.Enqueue(SessionData.currentPath); while (subfolders.Count != 0) { string currentPath = subfolders.Dequeue(); int indentation = currentPath.Split('\\').Length - initialIndentation; if (depth < indentation) { break; } OutputWriter.WriteMessageOnNewLine($"{new string('-', indentation)}{currentPath}"); var indexOfLastSlash = currentPath.Length; try { foreach (var file in Directory.GetFiles(currentPath)) { string fileName = file.Substring(indexOfLastSlash); OutputWriter.WriteMessageOnNewLine($"{new string('-', indexOfLastSlash)}{fileName}"); } foreach (string directoryPath in Directory.GetDirectories(currentPath)) { subfolders.Enqueue(directoryPath); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessException); } } }
private static string[] GetLinesWithPossibleMissmatches(string[] actualOtputLines, string[] expectedOutputLines, out bool hasMismatch) { string output = string.Empty; hasMismatch = false; OutputWriter.WriteMessageOnNewLine("Comparing files..."); int minOutputLines = actualOtputLines.Length; if (actualOtputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputLines = Math.Min(actualOtputLines.Length, expectedOutputLines.Length); OutputWriter.DisplayException(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } string[] mismatches = new string[minOutputLines]; for (int i = 0; i < minOutputLines; i++) { string actualLine = actualOtputLines[i]; string expectedLine = expectedOutputLines[i]; if (!actualLine.Equals(expectedLine)) { output = string.Format("Mismatch at line {0}: '{1}' Expected: '{2}'", i + 1, actualLine, expectedLine); //output += Environment.NewLine; hasMismatch = true; } else { output = actualLine; //output += Environment.NewLine; } mismatches[i] = output; } return(mismatches); }
public static string[] GetLinesWithPossibleMismatches(string[] actualLines, string[] expectedLines, out bool hasMismatch) { hasMismatch = false; string output = string.Empty; OutputWriter.WriteMessageOnNewLine("Comparing files ..."); int minOutputLines = 0; if (actualLines.Length != expectedLines.Length) { hasMismatch = true; minOutputLines = Math.Min(actualLines.Length, expectedLines.Length); OutputWriter.DisplayExeption(ExeptionMessages.ComparisonOfFilesWithDifferentSizes); } else { minOutputLines = expectedLines.Length; } string[] mismatches = new string[minOutputLines]; for (int index = 0; index < minOutputLines; index++) { string outputLine = actualLines[index]; string expectedLine = expectedLines[index]; if (outputLine != expectedLine) { output = string.Format("Mismatch at line {0} -- expected \"{1}\", actual \"{2}\"", index, expectedLine, outputLine); output += Environment.NewLine; hasMismatch = true; } else { output = expectedLine; output += Environment.NewLine; } mismatches[index] = output; } return(mismatches); }
// Method for comparing the content of two paths public void CompareContent(string userOutputPath, string expectedOutputPath) { // Trying to excecute the main logic of the method // If it detects an exception we throw a new InvalidPathException try { OutputWriter.WriteMessageOnNewLine($"Reading files..."); string mismatchPath = GetMismatchPath(expectedOutputPath); string[] actualOutputLines = File.ReadAllLines(userOutputPath); string[] expectedOutputLines = File.ReadAllLines(expectedOutputPath); bool hasMismatch; string[] mismatches = GetLinesWithPossibleMismatches(actualOutputLines, expectedOutputLines, out hasMismatch); this.PrintOutput(mismatches, hasMismatch, mismatchPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (IOException) { throw new InvalidPathException(); } }
public void CompareContent(string userOuputPath, string expectedOutputPath) { try { OutputWriter.WriteMessageOnNewLine("Reading files..."); string mismatchPath = GetMismatchPath(expectedOutputPath); string[] actualOuputLines = File.ReadAllLines(userOuputPath); string[] exptectedOuputLines = File.ReadAllLines(expectedOutputPath); bool hasMismatches; string[] mismatches = GetLinesWithPossibleMismatches( actualOuputLines, exptectedOuputLines, out hasMismatches); PrintOutput(mismatches, hasMismatches, mismatchPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (IOException) { throw new InvalidPathException(); } }
public static void ChangeCurrentDirectoryRelative(string relativePath) { if (relativePath == "..") { try { string currentPath = SessionData.currentPath; int indexOfLastSlash = currentPath.LastIndexOf("\\"); string newPath = currentPath.Substring(0, indexOfLastSlash); SessionData.currentPath = newPath; } catch (ArgumentOutOfRangeException) { OutputWriter.WriteMessageOnNewLine(ExceptionMessages.UnableToGoHigherInParitionHierarchy); } } else { string currenPath = SessionData.currentPath; currenPath += "\\" + relativePath; SessionData.currentPath = currenPath; } }
public static void TraverseDirectory() { OutputWriter.WriteEmptyLine(); int initialIdentation = SessionData.currentPath.Split('\\').Length; Queue <string> subFolders = new Queue <string>(); subFolders.Enqueue(SessionData.currentPath); OutputWriter.WriteMessageOnNewLine("*********************************************************************************"); while (subFolders.Count != 0) { string currentPath = subFolders.Dequeue(); int identation = currentPath.Split('\\').Length - initialIdentation; //OutputWriter.WriteMessageOnNewLine(currentPath); foreach (string directoryPath in Directory.GetDirectories(currentPath)) { subFolders.Enqueue(directoryPath); } OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}", new string('-', identation), currentPath)); } OutputWriter.WriteMessageOnNewLine("*********************************************************************************"); }
public static void ShowDirectory() { try { foreach (var file in Directory.GetFiles(SessionData.currentPath)) { int indexOfLastSlash = file.LastIndexOf("\\"); string fileName = file.Substring(indexOfLastSlash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + fileName); } foreach (var directory in Directory.GetDirectories(SessionData.currentPath)) { int indexOfLastSlash = directory.LastIndexOf("\\"); string directoryName = directory.Substring(indexOfLastSlash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + directoryName); } } catch (System.UnauthorizedAccessException) { OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessExceptionMessage); } }
private static void PrintOutput(string[] mismatches, bool hasMismatch, string mismatchesPath) { if (hasMismatch) { foreach (var line in mismatches) { OutputWriter.WriteMessageOnNewLine(line); } try { File.WriteAllLines(mismatchesPath, mismatches); } catch (DirectoryNotFoundException) { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } return; } OutputWriter.WriteMessageOnNewLine("Files are identicle. There are no mismatches"); }
private string[] GetLineWithPossibleMissmatch(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; var output = string.Empty; OutputWriter.WriteMessageOnNewLine("Comparing files..."); var minOutputLine = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputLine = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.WriteMessageOnNewLine(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } var mismatches = new string[minOutputLine]; for (int i = 0; i < minOutputLine; i++) { var actualLine = actualOutputLines[i]; var expectedLine = expectedOutputLines[i]; if (!actualLine.Equals(expectedLine)) { output = string.Format("Mismatch at line{0} -- expected: \"{1}\", actual: \"{2}\"", i, expectedLine, actualLine); output += Environment.NewLine; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[i] = output; } return(mismatches); }
public void TraverseDirectory(int depth) { OutputWriter.WriteEmptyLine(); var initialIdentation = SessionData.currentPath.Split('\\').Length; var subFolders = new Queue <string>(); subFolders.Enqueue(SessionData.currentPath); while (subFolders.Count > 0) { var currantPath = subFolders.Dequeue(); var identation = currantPath.Split('\\').Length - initialIdentation; if (depth - identation < 0) { break; } OutputWriter.WriteMessageOnNewLine($"{new string('-', identation)}{currantPath}"); try { foreach (var file in Directory.GetFiles(currantPath)) { var indexOfLastSlash = file.LastIndexOf('\\'); var fileName = file.Substring(indexOfLastSlash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + fileName); } foreach (var subFolderPath in Directory.GetDirectories(currantPath)) { subFolders.Enqueue(subFolderPath); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessExceptionMessage); } } }
private static void ReadData() { string input = Console.ReadLine(); while (!string.IsNullOrEmpty(input)) { string[] tokens = input.Split(' '); string course = tokens[0]; string student = tokens[1]; int mark = int.Parse(tokens[2]); if (!studentsByCourse.ContainsKey(course)) { studentsByCourse.Add(course, new Dictionary <string, List <int> >()); } if (!studentsByCourse[course].ContainsKey(student)) { studentsByCourse[course].Add(student, new List <int>()); } studentsByCourse[course][student].Add(mark); input = Console.ReadLine(); } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
/// <summary> /// Lists all available commands /// </summary> /// <param name="input">Current command</param> /// <param name="data">Parameters collection: command</param> private static void TryGetHelp(string input, string[] data) { if (data.Length == 1) { OutputWriter.WriteMessageOnNewLine($"{new string('_', 100)}"); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "make directory - mkdir: path ")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "traverse directory - ls: depth ")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "comparing files - cmp: path1 path2")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "change directory - changeDirREl:relative path")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "change directory - changeDir:absolute path")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "read students data base - readDb: path")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "filter {courseName} excelent/average/poor take 2/5/all students - filterExcelent (the output is written on the console)")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "order increasing students - order {courseName} ascending/descending take 20/10/all (the output is written on the console)")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "download file - download: path of file (saved in current directory)")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "download file asinchronously - downloadAsynch: path of file (save in the current directory)")); OutputWriter.WriteMessageOnNewLine(string.Format("|{0, -98}|", "get help – help")); OutputWriter.WriteMessageOnNewLine($"{new string('_', 100)}"); OutputWriter.WriteEmptyLine(); } else { DisplayInvalidCommandMessage(input); } }
public void CompareContent(string userOutputPath, string expectedOutputPath) { try { OutputWriter.WriteMessageOnNewLine("Reading files ..."); string mismatchPath = GetMismatchPath(expectedOutputPath); string[] actualOutputLines = File.ReadAllLines(userOutputPath); string[] expectedOutputLines = File.ReadAllLines(expectedOutputPath); int minOutputLines = actualOutputLines.Length; bool hasMismatch = false; string[] mismatches = this.GetLinesWithPossibleMismatches(actualOutputLines, expectedOutputLines, hasMismatch, minOutputLines); PrintOutput(mismatches, hasMismatch, mismatchPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (IOException ioException) { OutputWriter.DisplayException(ioException.Message); } }
private void ReadData(string fileName) { string path = SessionData.currentPath + "\\" + fileName; if (File.Exists(path)) { var rgx = new Regex(@"([A-Z][a-zA-Z#\++]*_[A-Z][a-z]{2}_\d{4})\s+([A-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)"); var allInputLines = File.ReadAllLines(path); for (int i = 0; i < allInputLines.Length; i++) { if (!string.IsNullOrEmpty(allInputLines[i]) && rgx.IsMatch(allInputLines[i])) { var currentMatch = rgx.Match(allInputLines[i]); var courseName = currentMatch.Groups[1].Value; var username = currentMatch.Groups[2].Value; var scoreStr = currentMatch.Groups[3].Value; try { var scores = scoreStr.Split(new[] { ' ' }, 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 (!this.students.ContainsKey(username)) { this.students.Add(username, new SoftUniStudent(username)); } if (!this.courses.ContainsKey(courseName)) { this.courses.Add(courseName, new SoftUniCourse(courseName)); } var course = this.courses[courseName]; var student = this.students[username]; student.EnrollInCourse(course); student.SetMarkOnCourse(courseName, scores); course.EnrollStudent(student); } catch (FormatException fex) { OutputWriter.DisplayException(fex.Message + $"at line : {i}"); } } } } else { throw new InvalidPathException(); } isDataInilized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
public static void DisplayStudent(KeyValuePair <string, List <int> > student) { OutputWriter.WriteMessageOnNewLine(string.Format($"{student.Key} - {string.Join(", ", student.Value)}")); }
private void DisplayInvalidCommandMessage(string input) { OutputWriter.WriteMessageOnNewLine($"The command {input} is invalid!"); }
private void ReadData(string fileName) { string path = SessionData.currentPath + "\\" + fileName; if (File.Exists(path)) { OutputWriter.WriteMessageOnNewLine("Reading data..."); 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(x => x > 100 || x < 0)) { OutputWriter.DisplayException(ExceptionMessages.InvalidScore); } if (scores.Length > Course.NUMBER_OF_TASK_ON_EXAM) { OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores); continue; } if (!this.students.ContainsKey(username)) { this.students.Add(username, new Student(username)); } if (!this.courses.ContainsKey(courseName)) { this.courses.Add(courseName, new Course(courseName)); } Course course = this.courses[courseName]; Student student = this.students[username]; student.EnrollInCourse(course); student.SetMarkOnCourse(courseName, scores); course.EnrollStudent(student); } catch (FormatException fex) { OutputWriter.DisplayException(fex.Message + $"at line: {line}"); } } } this.isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } }
// Main method in this class, used for reading data and accepting a fileName string as a parameter private void ReadData(string fileName) { string path = SessionData.currentPath + @"\" + fileName; if (File.Exists(path)) { // Regex pattern used for detecting all the valid input lines 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 regex = new Regex(pattern); string[] allInputLines = File.ReadAllLines(path); foreach (string line in allInputLines) { if (!string.IsNullOrEmpty(line) && regex.IsMatch(line)) { Match currentMatch = regex.Match(line); string courseName = currentMatch.Groups[1].Value; string username = currentMatch.Groups[2].Value; string studentsStr = currentMatch.Groups[3].Value; try { int[] scores = studentsStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToArray(); if (scores.Any(x => x > 100 || x < 0)) { OutputWriter.DisplayException(ExceptionMessages.InvalidScore); continue; } if (scores.Length > SoftUniCourse.MaxScoreOnExamTask) { OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores); continue; } if (!this.students.ContainsKey(username)) { this.students.Add(username, new SoftUniStudent(username)); } if (!this.courses.ContainsKey(courseName)) { this.courses.Add(courseName, new SoftUniCourse(courseName)); } // Taking the currently needed course and student ICourse course = this.courses[courseName]; IStudent student = this.students[username]; // Using the previously created methods in the Student and Course classes student.EnrollInCourse(course); student.SetMarkOnCourse(courseName, scores); course.EnrollStudent(student); } catch (FormatException fex) { OutputWriter.DisplayException(fex.Message + $"at line : {line}"); } } } } else { throw new InvalidPathException(); } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine($"Data read!"); }
public static void PrintStudent(KeyValuePair <string, double> student) { OutputWriter.WriteMessageOnNewLine($"{student.Key} - {student.Value}"); }