Exemple #1
0
 private void TryParseParametersForOrderAndTake(string takeCommand, string takeQuantity,
                                                string courseName, string filter)
 {
     if (takeCommand == "take")
     {
         if (takeQuantity == "all")
         {
             this.repository.OrderAndTake(courseName, filter);
         }
         else
         {
             int  studentsToTake;
             bool hasParsed = int.TryParse(takeQuantity, out studentsToTake);
             if (hasParsed)
             {
                 this.repository.OrderAndTake(courseName, filter, studentsToTake);
             }
             else
             {
                 OutputWriter.DisplayMessage(ExceptionMessages.InvalidTakeQuantityParameter);
             }
         }
     }
     else
     {
         OutputWriter.DisplayMessage(ExceptionMessages.InvalidTakeQuantityParameter);
     }
 }
 public static void OrderAndTake(Dictionary <string, List <int> > database, string comparison, int studentsToTake)
 {
     comparison = comparison.ToLower();
     if (comparison == "ascending")
     {
         PrintStudents(database.OrderBy(s => s.Value.Sum()).Take(studentsToTake)
                       .ToDictionary(st => st.Key, st => st.Value));
     }
     else if (comparison == "descending")
     {
         PrintStudents(database.OrderByDescending(s => s.Value.Sum()).Take(studentsToTake)
                       .ToDictionary(st => st.Key, st => st.Value));
     }
     else
     {
         OutputWriter.DisplayMessage(ExceptionMessages.InvalidComparisonQuery);
     }
 }
        private static void ReadData(string fileName)
        {
            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+)";

            string path = SessionData.currentPath + "\\" + fileName;

            if (File.Exists(path))
            {
                string[] allInputLines = File.ReadAllLines(path);

                for (int i = 0; i < allInputLines.Length; i++)
                {
                    if (!string.IsNullOrEmpty(allInputLines[i]) && Regex.IsMatch(allInputLines[i], pattern))
                    {
                        Match  match   = Regex.Match(allInputLines[i], pattern);
                        string course  = match.Groups[1].Value;
                        string student = match.Groups[2].Value;
                        int    studentScore;
                        bool   hasParseStudentScore = int.TryParse(match.Groups[3].Value, out studentScore);

                        if (hasParseStudentScore && studentScore >= 0 && studentScore <= 100)
                        {
                            if (!studentsByCourse.ContainsKey(course))
                            {
                                studentsByCourse[course] = new Dictionary <string, List <int> >();
                            }
                            if (!studentsByCourse[course].ContainsKey(student))
                            {
                                studentsByCourse[course][student] = new List <int>();
                            }
                            studentsByCourse[course][student].Add(studentScore);
                        }
                    }
                }
            }
            else
            {
                OutputWriter.DisplayMessage(ExceptionMessages.InvalidPath);
                return;
            }

            isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLine("Data read!");
        }
 public static void FilterAndTake(Dictionary <string, List <int> > database, string filter, int studentsToTake)
 {
     if (filter == "excellent")
     {
         FilterAndTake(database, x => x >= 5, studentsToTake);
     }
     else if (filter == "average")
     {
         FilterAndTake(database, x => x < 5 && x >= 3.5, studentsToTake);
     }
     else if (filter == "poor")
     {
         FilterAndTake(database, x => x < 3.5, studentsToTake);
     }
     else
     {
         OutputWriter.DisplayMessage(ExceptionMessages.InvalidStudentFilter);
     }
 }
        public void CompareContent(string userOutputPath, string expectedOutputPath)
        {
            try
            {
                OutputWriter.DisplayWaitingMessage("Reading files...");
                string mismatchPath = this.GetMismatchPath(expectedOutputPath);

                string[] actualOutputLines   = File.ReadAllLines(userOutputPath);
                string[] expectedOutputLines = File.ReadAllLines(expectedOutputPath);

                bool     hasMismatch;
                string[] mismatches =
                    this.GetLinesWithPossibleMismatches(actualOutputLines, expectedOutputLines, out hasMismatch);

                this.PrintOutput(mismatches, hasMismatch, mismatchPath);
                OutputWriter.DisplaySuccessfulMessage("Files read!");
            }
            catch (IOException ioException)
            {
                OutputWriter.DisplayMessage(ioException.Message);
            }
        }
        public static void CompareContent(string userOutputPath, string expectedOutputPath)
        {
            try
            {
                OutputWriter.WriteMessageOnNewLine("Reading files...");
                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);
            }
        }
        private static 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.DisplayMessage(ExceptionMessages.ComparisonOfFilesWithDifferentSizes);
            }

            for (int i = 0; i < minOutputLines; i++)
            {
                string actualLine   = actualOutputLines[i];
                string expectedLine = expectedOutputLines[i];

                if (actualLine != expectedLine)
                {
                    output      = $"Mismatch at line {i} -- expected: \"{expectedLine}\", actual: \"{actualLine}\"";
                    output     += Environment.NewLine;
                    hasMismatch = true;
                }
                else
                {
                    output  = actualLine;
                    output += Environment.NewLine;
                }
                mismatches[i] = output;
            }

            return(mismatches);
        }
        private static void PrintOutput(string[] mismatches, bool hasMismatch, string mismatchPath)
        {
            if (hasMismatch)
            {
                foreach (string line in mismatches)
                {
                    OutputWriter.WriteMessageOnNewLine(line);
                }

                try
                {
                    File.WriteAllLines(mismatchPath, mismatches);
                }
                catch (DirectoryNotFoundException)
                {
                    OutputWriter.DisplayMessage(ExceptionMessages.InvalidPath);
                }

                return;
            }

            OutputWriter.WriteMessageOnNewLine("Files are identical. There are no mismatches.");
        }
 public override void Execute()
 {
     if (this.Data.Length == 1)
     {
         this.Manager.TraverseFolder(0);
     }
     else if (this.Data.Length == 2)
     {
         int  depth;
         bool hasParsed = int.TryParse(this.Data[1], out depth);
         if (hasParsed)
         {
             this.Manager.TraverseFolder(depth);
         }
         else
         {
             OutputWriter.DisplayMessage(ExceptionMessages.UnableToParseNumber);
         }
     }
     else
     {
         throw new InvalidCommandException(this.Input);
     }
 }
        private void ReadData(string fileName)
        {
            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]+)";

            string path = SessionData.currentPath + "\\" + fileName;

            if (File.Exists(path))
            {
                string[] allInputLines = File.ReadAllLines(path);

                for (int i = 0; i < allInputLines.Length; i++)
                {
                    if (!string.IsNullOrEmpty(allInputLines[i]) && Regex.IsMatch(allInputLines[i], pattern))
                    {
                        Match  match        = Regex.Match(allInputLines[i], pattern);
                        string courseName   = match.Groups[1].Value;
                        string username     = match.Groups[2].Value;
                        string scoresString = match.Groups[3].Value;

                        try
                        {
                            int[] scores = scoresString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                           .Select(int.Parse).ToArray();

                            if (scores.Any(s => s > 100 || s < 0))
                            {
                                throw new InvalidScoreException();
                            }
                            if (scores.Length > Course.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayMessage(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));
                            }

                            ICourse  course  = this.courses[courseName];
                            IStudent student = this.students[username];

                            student.EnrollInCourse(course);
                            student.SetMarkOnCourse(courseName, scores);
                            course.EnrollStudent(student);
                        }
                        catch (FormatException fe)
                        {
                            OutputWriter.DisplayMessage(fe.Message + $"at line: {i}");
                        }
                    }
                }
            }
            else
            {
                throw new InvalidPathException();
            }

            this.isDataInitialized = true;
            OutputWriter.DisplaySuccessfulMessage("Data read!");
        }