public static void ChangeCurrentDirectoryAbsolute(string absolutePath) { if (!Directory.Exists(absolutePath)) { OutputWriter.DisplayExeptions(ExceptionMessages.InvalidPath); return; } SessionData.CurrentPath = absolutePath; }
private static bool IsQueryForStudentPossible(string courseName, string studentUserName) { if (IsQueryForCoursePossible(courseName) && studentsByCourse[courseName].ContainsKey(studentUserName)) { return(true); } else { OutputWriter.DisplayExeptions(ExceptionMessages.InexistingStudentInDataBase); } return(false); }
public static void CreateDirectoryInCurrentFolder(string folderName) { string path = SessionData.CurrentPath + "\\" + folderName; try { Directory.CreateDirectory(path); } catch (ArgumentException) { OutputWriter.DisplayExeptions(ExceptionMessages.ForbiddenSymbolsContainedInName); } }
private static 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-Z][a-z]{0,3}\d{2}_\d{2,4})\s(\d+)"; 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 course = currentMatch.Groups[1].Value; string student = currentMatch.Groups[2].Value; int studentsScoreOnTask; bool hasParsedScore = int.TryParse(currentMatch.Groups[3].Value, out studentsScoreOnTask); if (hasParsedScore && studentsScoreOnTask >= 0 && studentsScoreOnTask <= 100) { 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(studentsScoreOnTask); } } } } else { OutputWriter.DisplayExeptions(ExceptionMessages.InvalidPath); return; } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
private static bool IsQueryForCoursePossible(string courseName) { if (isDataInitialized) { if (studentsByCourse.ContainsKey(courseName)) { return(true); } else { OutputWriter.DisplayExeptions(ExceptionMessages.InexistingCourseInDataBase); } } else { OutputWriter.DisplayExeptions(ExceptionMessages.DataNotInitializedExceptionMessage); } return(false); }
public static void TraverseDirectory(int depth) { OutputWriter.WriteEmptyLine(); int initialIdentation = SessionData.CurrentPath.Split('\\').Length; var subFolders = new Queue <string>(); subFolders.Enqueue(SessionData.CurrentPath); while (subFolders.Count != 0) { string currentPath = subFolders.Dequeue(); int identation = currentPath.Split('\\').Length - initialIdentation; OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}", new string('-', identation), currentPath)); try { foreach (string directoryPath in Directory.GetDirectories(currentPath)) { subFolders.Enqueue(directoryPath); } //get and write file names for current directory foreach (var file in Directory.GetFiles(currentPath)) { var indexOfLastSplash = file.LastIndexOf('\\'); var fileName = file.Substring(indexOfLastSplash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSplash) + fileName); } } catch (System.UnauthorizedAccessException) { OutputWriter.DisplayExeptions(ExceptionMessages.UnauthorizedAccessException); } if (depth - identation < 0) { break; } } }
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 hasMismatches; string[] mismatches = GetLineWithPossibleMismatche(actualOutputLines, expectedOutputLines, out hasMismatches); PrintOutput(mismatches, hasMismatches, mismatchPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (FileNotFoundException) { OutputWriter.DisplayExeptions(ExceptionMessages.InvalidPath); } }
private static string[] GetLineWithPossibleMismatche(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; string output = string.Empty; OutputWriter.WriteMessageOnNewLine("Comparng files..."); int minOutputLines = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.DisplayExeptions(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 {index} -- expected: {expectedLine}, actual: {actualLine}"); output += Environment.NewLine; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[index] = output; } return(mismatches); }
private 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.DisplayExeptions(ExceptionMessages.InvalidPath); } } else { OutputWriter.WriteMessageOnNewLine("Files are identical.There are no 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.DisplayExeptions(ExceptionMessages.UnableToGoHigherInPartitionHierarchy); } } else { string currentPath = SessionData.CurrentPath; currentPath += "\\" + relativePath; ChangeCurrentDirectoryAbsolute(currentPath); } }