public static 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 (FileNotFoundException) { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } }
public static 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 currentPath = subFolders.Dequeue(); int identation = currentPath.Split('\\').Length - initialIdentation; if (depth - identation < 0) { break; } OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}", new string('-', identation), currentPath)); try { foreach (var file in Directory.GetFiles(currentPath)) { int indexOfLastSlash = file.LastIndexOf('\\'); 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); } } }
public static string[] GetLinesWithPossibleMismatches(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; string output = string.Empty; OutputWriter.WriteMessageOnNewLine("Comparing files..."); int minOutputLines = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.DisplayException(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } string[] mismatches = new string[minOutputLines]; for (int index = 0; index < minOutputLines; index++) { string actualLine = actualOutputLines[index]; string expectedLine = expectedOutputLines[index]; if (!actualLine.Equals(expectedLine)) { output = string.Format("Mismatch at line {0} -- expected: \"{1}\",actual:\"{2}\"", index, expectedLine, actualLine); output += Environment.NewLine; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[index] = output; } return(mismatches); }
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.DisplayException(ExceptionMessages.UnableToGoHigherInPartitionHierarchy); } } else { string currentPath = SessionData.currentPath; currentPath = "\\" + relativePath; ChangeCurrentDirectoryAbsolute(currentPath); } }
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(); } }