Пример #1
0
 public static void GetAllStudentsFromCourse(string courseName)
 {
     if (IsQueryForCoursePossible(courseName))
     {
         OutputWriter.WriteMessageOnNewLIne($"{courseName}:");
         foreach (var studentMarksEntry in studentsByCourse[courseName])
         {
             OutputWriter.PrintStudent(studentMarksEntry);
         }
     }
 }
Пример #2
0
 public static void InitializeData(string fileName)
 {
     if (!isDataInitialized)
     {
         OutputWriter.WriteMessageOnNewLIne("Reading data...");
         studentsByCourse = new Dictionary <string, Dictionary <string, List <int> > >();
         ReadData(fileName);
     }
     else
     {
         OutputWriter.WriteMessageOnNewLIne(ExceptionMessages.DataAlreadyInitialisedException);
     }
 }
Пример #3
0
 private static void TryGetHelp()
 {
     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 - cdRel relative path"));
     OutputWriter.WriteMessageOnNewLIne(string.Format("|{0, -98}|", "change directory - cdAbs 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();
 }
Пример #4
0
        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    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);
                        }
                    }
                }
            }
            else
            {
                OutputWriter.DisplayException(ExceptionMessages.InvalidPath);
            }


            isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLIne("Data read!");
        }
Пример #5
0
        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.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);
        }
Пример #6
0
        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;

                OutputWriter.WriteMessage(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 directoryPath in Directory.GetDirectories(currentPath))
                    {
                        subFolders.Enqueue(directoryPath);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessExceptionMessage);
                }
                OutputWriter.WriteMessageOnNewLIne(string.Format("{0}{1}", new string('-', identation), currentPath));

                if (depth - identation < 0)
                {
                    break;
                }
            }
        }
Пример #7
0
        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);
            }
        }
Пример #8
0
        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.DisplayException(ExceptionMessages.InvalidPath);
                }

                return;
            }

            OutputWriter.WriteMessageOnNewLIne("Files ara identical. There are no mismatches.");
        }
Пример #9
0
 public static void PrintStudent(KeyValuePair <string, List <int> > student)
 {
     OutputWriter.WriteMessageOnNewLIne(String.Format($"{student.Key} - {string.Join(", ", student.Value)}"));
 }