public static void CompareContent(string userOutputPath, string expectedOutputPath) { OutputWriter.WriteMessageOnNewLine("Reading files..."); try { string mismathPath = GetMismatchPath(expectedOutputPath); string[] actualOutputLines = File.ReadAllLines(userOutputPath); string[] expectedOutputLines = File.ReadAllLines(expectedOutputPath); bool hasMismath; string[] mismatches = GetLinesWithPossibleMismatches(actualOutputLines, expectedOutputLines, out hasMismath); PrintOutput(mismatches, hasMismath, mismathPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (FileNotFoundException) { OutputWriter.DisplayExeption(ExceptionMessages.InvalidPath); } }
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(pair => pair.Key, pair => pair.Value)); } else if (comparison == "descending") { PrintStudents(wantedData .OrderByDescending(x => x.Value.Sum()) .Take(studentsToTake) .ToDictionary(pair => pair.Key, pair => pair.Value)); } else { OutputWriter.DisplayExeption(ExceptionMessages.InvalidComparisonQuery); } }
public static 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) { OutputWriter.DisplayExeption(ExeptionMessages.InvalidPath); } } else { OutputWriter.WriteMessageOnNewLine("Files are identical. No 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); }
public static void ChangeCurrentDirectoryRelative(string relativePath) { if (relativePath == "..") { try { string curentPath = SessionData.currentPath; int index = curentPath.LastIndexOf("\\"); string newPath = curentPath.Substring(0, index); SessionData.currentPath = newPath; } catch (ArgumentOutOfRangeException) { OutputWriter.DisplayExeption(ExeptionMessages.UnableToGoHigher); } } else { string curentPath = SessionData.currentPath; curentPath += "\\" + relativePath; ChangeCurrentDirectoryAbsolute(curentPath); } }
private string[] GetLinesWithPossibleMismatches(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; string output = string.Empty; string[] mismatches = new string[actualOutputLines.Length]; OutputWriter.WriteMessageOnNewLine("Comparing files..."); int minOutputlines = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputlines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.DisplayExeption(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } for (int index = 0; index < minOutputlines; index++) { string actualLine = actualOutputLines[index]; string expectedLine = expectedOutputLines[index]; if (!actualLine.Equals(expectedLine)) { output = $"Mismatch at line {index} -- expected: \"{expectedLine}\", actual: \"{actualLine}\""; output += Environment.NewLine; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[index] = output; } return(mismatches); }
public void TraverseDirectory(string 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; OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}", new string('-', indentation), currentPath)); try { foreach (var file in Directory.GetFiles(currentPath)) { int indexOfLastSlash = file.LastIndexOf("\\"); string filename = file.Substring(indexOfLastSlash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + filename); } var subDirectories = Directory.GetDirectories(currentPath); foreach (var subDir in subDirectories) { subFolders.Enqueue(subDir); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayExeption(ExceptionMessages.UnauthorizedAccessExceptionMessage); } } }
public static void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayExeption("The command '" + input + "' is invalid"); }
private void ReadData(string fileName) { var path = SessionData.currentPath + "\\" + fileName; if (File.Exists(path)) { var 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]+)"; var regex = new Regex(pattern); var allInputLines = File.ReadAllLines(path); for (var line = 0; line < allInputLines.Length; line++) { if (!string.IsNullOrEmpty(allInputLines[line]) && regex.IsMatch(allInputLines[line])) { var tokens = regex.Match(allInputLines[line]); var courseName = tokens.Groups[1].Value; var userName = tokens.Groups[2].Value; var scoreStr = tokens.Groups[3].Value; try { var scores = scoreStr.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse).ToArray(); if (scores.Any(x => x > 100 || x < 0)) { OutputWriter.DisplayExeption(ExceptionMessages.InvalidScore); continue; } if (scores.Length > Course.NumberOfTaskOnexam) { OutputWriter.DisplayExeption(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.SetMarksInCourse(courseName, scores); } catch (FormatException e) { throw new FormatException(e.Message + $"at line : {line}"); } } } IsDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { throw new InvalidPathException(); } }
private static void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayExeption($"The command '{input}' is invalid"); }