private static void TryParseParametersForOrderAndTake(string takeCommand, string takeQuantity, string courseName, string order) { if (takeCommand == "take") { if (takeQuantity == "all") { StudentsRepository.OrderAndTake(courseName, order); } else { int studentsToTake; bool isParsed = int.TryParse(takeQuantity, out studentsToTake); if (isParsed) { StudentsRepository.OrderAndTake(courseName, order, studentsToTake); } else { OutputWriter.DisplayMessage(ExceptionMessages.InvalidTakeQuantityParameter); } } } else { OutputWriter.DisplayMessage(ExceptionMessages.InvalidTakeCommand); } }
private static bool IsQueryForStudentPossible(string courseName, string studentUserName) { if (IsQueryForCoursePossible(courseName) && studentsByCourse[courseName].ContainsKey(studentUserName)) { return(true); } else { OutputWriter.DisplayMessage(ExceptionMessages.InexistingStudentInDataBase); } return(false); }
public static void CreateDirectoryInCurrentFolder(string name) { string path = SessionData.currentPath + "\\" + name; try { Directory.CreateDirectory(path); } catch (ArgumentException) { OutputWriter.DisplayMessage(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 regex = new Regex(pattern); string[] allInputLines = File.ReadAllLines(path); for (int line = 0; line < allInputLines.Length; line++) { if (!string.IsNullOrEmpty(allInputLines[line]) && regex.IsMatch(allInputLines[line])) { Match currentMatch = regex.Match(allInputLines[line]); string course = currentMatch.Groups[1].Value; string student = currentMatch.Groups[2].Value; int studentScoreOnTask; bool hasParsedScore = int.TryParse(currentMatch.Groups[3].Value, out studentScoreOnTask); if (hasParsedScore && studentScoreOnTask >= 0 && studentScoreOnTask <= 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(studentScoreOnTask); } } } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { OutputWriter.DisplayMessage(ExceptionMessages.InvalidPath); } }
public static void FilterAndTake(Dictionary <string, List <int> > wantedData, string wantedFilter, int studentsToTake) { if (wantedFilter == "excellent") { FilterAndTake(wantedData, x => x >= 5, studentsToTake); } else if (wantedFilter == "average") { FilterAndTake(wantedData, x => x < 5 && x >= 3.5, studentsToTake); } else if (wantedFilter == "poor") { FilterAndTake(wantedData, x => x < 3.5, studentsToTake); } else { OutputWriter.DisplayMessage(ExceptionMessages.InvalidStudentFilter); } }
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.DisplayMessage(ExceptionMessages.InvalidComparisonQuery); } }
private static bool IsQueryForCoursePossible(string courseName) { if (isDataInitialized) { if (studentsByCourse.ContainsKey(courseName)) { return(true); } else { OutputWriter.DisplayMessage(ExceptionMessages.InexistingCourseInDataBase); } } else { OutputWriter.DisplayMessage(ExceptionMessages.DataNotInitializedExceptionMessage); } return(false); }
private 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.DisplayMessage(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } string[] mismatches = new string[minOutputLines]; for (int index = 0; index < minOutputLines; index++) { string actualLine = actualOutputLines[index]; string expectedLine = expectedOutputLines[index]; if (!actualOutputLines[index].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); }
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.DisplayMessage(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($"{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 (var directory in Directory.GetDirectories(currentPath)) { subFolders.Enqueue(directory); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayMessage(ExceptionMessages.UnauthorizedAccessExceptionMessage); } } }
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.DisplayMessage(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.DisplayMessage(ExceptionMessages.UnableToGoHigherInPartitionHierarchy); } } else { string currentPath = SessionData.currentPath; currentPath += "\\" + relativePath; ChangeCurrentDirectoryAbsolute(currentPath); } }
private static void TryTraverseFolders(string input, string[] data) { if (data.Length == 1) { IOManager.TraverseDirectory(0); } else if (data.Length == 2) { int depth; bool hasParsed = int.TryParse(data[1], out depth); if (hasParsed) { IOManager.TraverseDirectory(depth); } else { OutputWriter.DisplayMessage(ExceptionMessages.UnableToParseNumber); } } else { DisplayInvalidCommandMessage(input); } }
private static void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayMessage($"The command '{input}' is invalid"); }