public static void OrderAndTake(Dictionary <string, List <int> > wantedData,
                                 string comparison,
                                 int studentsToTake)
 {
     comparison = comparison.ToLower();
     if (comparison == "ascending")
     {
         PrintStudent(wantedData.OrderBy(x => x.Value.Sum())
                      .Take(studentsToTake)
                      .ToDictionary(pair => pair.Key, pair => pair.Value));
     }
     else if (comparison == "descending")
     {
         PrintStudent(wantedData.OrderByDescending(x => x.Value.Sum())
                      .Take(studentsToTake)
                      .ToDictionary(pair => pair.Key, pair => pair.Value));
     }
     else
     {
         OutputWriter.DisplayException(ExceptionMessages.InvalidComparisonQuery);
     }
 }
Пример #2
0
        public static void CompareContent(string userOutputPath, string expectedOutputPath)
        {
            OutputWriter.WriteMessageOnNewLine("Reading files...");

            try
            {
                var mismatchesPath = GetMismatchPath(expectedOutputPath);

                var actualOutputLines  = File.ReadAllLines(userOutputPath);
                var expectedOutputLine = File.ReadAllLines(expectedOutputPath);

                bool hasMismatches;
                var  mismatches = GetLineWithPossibleMismatches(actualOutputLines, expectedOutputLine, out hasMismatches);

                Printoutput(mismatches, hasMismatches, mismatchesPath);
                OutputWriter.WriteMessageOnNewLine("Files read!");
            }
            catch (FileNotFoundException)
            {
                throw new InvalidPathException();
            }
        }
Пример #3
0
        private static void FilterAndTake(Dictionary <string, List <int> > wantedData, Predicate <double> givenFilter, int studentsToTake)
        {
            int counter = 0;

            foreach (var username_score in wantedData)
            {
                if (counter == studentsToTake)
                {
                    break;
                }

                var avrScore = username_score.Value.Average();
                var percentageOfFullfillments = avrScore / 100;
                var mark = percentageOfFullfillments * 4 + 2;

                if (givenFilter(mark))
                {
                    OutputWriter.PrintStudent(username_score);
                    counter++;
                }
            }
        }
        private static void FilterAndTake(Dictionary <string, List <int> > wantedData, Predicate <double> givenFilter, int studentsToTake)
        {
            int counterForPrinted = 0;

            foreach (var userName_Points in wantedData)
            {
                if (counterForPrinted == studentsToTake)
                {
                    break;
                }

                double averageScore            = userName_Points.Value.Average();
                double percentageOfFullfilment = averageScore / 100;
                double mark = percentageOfFullfilment * 4 + 2;

                if (givenFilter(mark))
                {
                    OutputWriter.PrintStudent(userName_Points);
                    counterForPrinted++;
                }
            }
        }
Пример #5
0
 private static void TryTraverseFolder(string input, string[] data)
 {
     if (data.Length < 2)
     {
         IOManager.TraverseDirectory(0);
     }
     else if (data.Length == 2)
     {
         if (int.TryParse(data[1], out int depth))
         {
             IOManager.TraverseDirectory(depth);
         }
         else
         {
             OutputWriter.DisplayException(ExceptionMessages.UnableToParseNumber);
         }
     }
     else
     {
         OutputWriter.DisplayException(string.Format(ExceptionMessages.InvalidCommandParametersCount, data[0]));
     }
 }
Пример #6
0
        public void CompareContent(string userOutputPath, string expectedOutputPath)
        {
            try
            {
                OutputWriter.WriteMessageOnNewLine("Reading files...");

                string mismatchPath = GetMismatchPath(expectedOutputPath);

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

                bool     hasMismatch;
                string[] mismatches = GetLinesWithPossibleMismatches(actualOuputLines, expectedOutputLines, out hasMismatch);

                this.PrintOutput(mismatches, hasMismatch, mismatchPath);
                OutputWriter.WriteMessageOnNewLine("Files read!");
            }
            catch (IOException e)
            {
                OutputWriter.DisplayException(e.Message);
            }
        }
Пример #7
0
 public void OrderAndTake(Dictionary <string, double> studentsWithMarks, string comparison, int studentsToTake)
 {
     comparison = comparison.ToLower();
     if (comparison == "ascending")
     {
         this.PrintStudents(studentsWithMarks
                            .OrderBy(x => x.Value)
                            .Take(studentsToTake)
                            .ToDictionary(pair => pair.Key, pair => pair.Value));
     }
     else if (comparison == "descending")
     {
         this.PrintStudents(studentsWithMarks
                            .OrderByDescending(x => x.Value)
                            .Take(studentsToTake)
                            .ToDictionary(pair => pair.Key, pair => pair.Value));
     }
     else
     {
         OutputWriter.DisplayException(ExceptionMessages.InvalidComparisonQuery);
     }
 }
Пример #8
0
        public static void TraverseDirectory(string path)
        {
            OutputWriter.WriteEmptyLine();
            int            initialIdentation = path.Split('\\').Length;
            Queue <string> subFolders        = new Queue <string>();

            subFolders.Enqueue(path);

            while (subFolders.Count != 0)
            {
                string currentPath = subFolders.Dequeue();
                int    identetion  = currentPath.Split('\\').Length - initialIdentation;

                OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}",
                                                                 new string('-', identetion),
                                                                 currentPath));
                foreach (var directoryPath in Directory.GetDirectories(currentPath))
                {
                    subFolders.Enqueue(directoryPath);
                }
            }
        }
Пример #9
0
        private 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)
                {
                    throw new InvalidPathException();
                }

                return;
            }

            OutputWriter.WriteMessageOnNewLine("Files are identical. There are no mismatches.");
        }
        private static void ReadData(string fileName)
        {
            string path = SessionData.GetCurrentDirectoryPath() + "\\" + fileName;

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

                for (int line = 0; line < allInputLines.Length; line++)
                {
                    InputEntryIntoDB(allInputLines[line]);
                }
            }
            else
            {
                OutputWriter.DisplayException(ExceptionMessages.InvalidPath);
                return;
            }

            isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLine("Data read");
        }
        private bool IsQueryForCoursePossible(string courseName)
        {
            if (isDataInitialized)
            {
                return(true);
            }
            else
            {
                OutputWriter.DisplayExpetion(ExceptionMessages.DataNotInitializedExceptionMessage);
            }
            //return false;

            if (this.courses.ContainsKey(courseName))
            {
                return(true);
            }
            else
            {
                OutputWriter.DisplayExpetion(ExceptionMessages.InexistingStudentInDataBase);
            }
            return(false);
        }
Пример #12
0
 private 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.DisplayException(ExceptionMessages.InvalidPath);
         //}
     }
     else
     {
         OutputWriter.WriteMessageOnNewLine($"Files are identical. There are no mismatches.");
     }
 }
Пример #13
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;
                try
                {
                    foreach (var directory in Directory.GetDirectories(currentPath))
                    {
                        subFolders.Enqueue(directory);
                    }

                    if (depth - identation <= 0)
                    {
                        break;
                    }

                    OutputWriter.WriteMessageOnNewLine(string.Format($"{new string('-', identation)}{currentPath}"));
                    foreach (var file in Directory.GetFiles(currentPath))
                    {
                        int    indexOfLastSlash = file.LastIndexOf("\\");
                        string fileName         = file.Substring(indexOfLastSlash);
                        OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + fileName);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessExceptionMessage);
                }
            }
        }
Пример #14
0
        public static void FilterAndTake(Dictionary <string, List <int> > wantedData, Predicate <double> givenFilter,
                                         int studentsToTake)
        {
            int counterForPrinted = 0;

            foreach (var usernamePoint in wantedData)
            {
                if (counterForPrinted == studentsToTake)
                {
                    break;
                }

                double averageMark = usernamePoint.Value.Average();
                double percentage  = averageMark / 100;
                double mark        = percentage * 4 + 2;
                if (givenFilter(mark))
                {
                    OutputWriter.PrintStudent(usernamePoint);
                    counterForPrinted++;
                }
            }
        }
Пример #15
0
        private static string[] GetLineWithPossibleMismatches(
            string[] actualOutputLines,
            string[] expectedOutputLines,
            out bool hasMismatch
            )
        {
            int minOutputLines = actualOutputLines.Length;

            if (actualOutputLines.Length != expectedOutputLines.Length)
            {
                hasMismatch    = true;
                minOutputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length);
                OutputWriter.DisplayException(ExceptionMessages.ComparisonOfFilesWithDifferentSizes);
            }
            hasMismatch = false;
            string output;

            string[] mismatches = new string[actualOutputLines.Length];
            OutputWriter.WriteMessageOnNewLine(ComparingFilesMessage);

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

                if (!actualLine.Equals(expectedLine))
                {
                    output      = $"Mismatch at line {i} -- expected: \"{expectedLine}\", actual: \"{actualLine}\"";
                    hasMismatch = true;
                }
                else
                {
                    output  = actualLine;
                    output += Environment.NewLine;
                }
                mismatches[i] = 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.DisplayException(ExceptionMessages.InvalidPath);
            }
        }
Пример #17
0
        private string[] GetLinesWithPossibleMismatches(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch)
        {
            hasMismatch = false;
            string output = string.Empty;

            int minOuputLines = actualOutputLines.Length;

            if (minOuputLines != expectedOutputLines.Length)
            {
                hasMismatch   = true;
                minOuputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length);
                OutputWriter.WriteMessageOnNewLine(ExceptionMessages.ComparisonOfFilesWithDifferentSizes);
            }

            string[] mismatches = new string[minOuputLines];
            OutputWriter.WriteMessageOnNewLine("Comparing files...");

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

                if (!actualOutputLines.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);
        }
Пример #18
0
        private static string[] GetLinesWithPossibleMissmatches(string[] actualOtputLines, string[] expectedOutputLines, out bool hasMismatch)
        {
            string output = string.Empty;

            hasMismatch = false;

            OutputWriter.WriteMessageOnNewLine("Comparing files...");
            int minOutputLines = actualOtputLines.Length;

            if (actualOtputLines.Length != expectedOutputLines.Length)
            {
                hasMismatch    = true;
                minOutputLines = Math.Min(actualOtputLines.Length, expectedOutputLines.Length);
                OutputWriter.DisplayException(ExceptionMessages.ComparisonOfFilesWithDifferentSizes);
            }

            string[] mismatches = new string[minOutputLines];

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

                if (!actualLine.Equals(expectedLine))
                {
                    output = string.Format("Mismatch at line {0}: '{1}' Expected: '{2}'", i + 1, actualLine, expectedLine);
                    //output += Environment.NewLine;
                    hasMismatch = true;
                }
                else
                {
                    output = actualLine;
                    //output += Environment.NewLine;
                }
                mismatches[i] = output;
            }
            return(mismatches);
        }
Пример #19
0
        private static void ReadData(string fileName)
        {
            string path = SessionData.currentPath + "\\" + fileName;

            if (Directory.Exists(path))
            {
                string[] tokens  = path.Split(' ');
                string   course  = tokens[0];
                string   student = tokens[1];
                int      mark    = int.Parse(tokens[2]);

                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(mark);

                string[] allInputLines = File.ReadAllLines(path);
                for (int i = 0; i < allInputLines.Length; i++)
                {
                    if (!string.IsNullOrEmpty(allInputLines[i]))
                    {
                        string[] data = allInputLines[i].Split(' ');
                    }
                }
            }
            else
            {
                OutputWriter.DisplayException(ExceptionMessages.InvalidPath);
            }

            isDataInitilize = true;
            OutputWriter.WriteMessegesOnNewLine("Data Read!");
        }
Пример #20
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);
                }
            }
            else
            {
                OutputWriter.WriteMessageOnNewLine(FilesAreIdenticalMessage);
            }
        }
Пример #21
0
 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.WriteMessageOnNewLine(ExceptionMessages.UnableToGoHigherInParitionHierarchy);
         }
     }
     else
     {
         string currenPath = SessionData.currentPath;
         currenPath += "\\" + relativePath;
         SessionData.currentPath = currenPath;
     }
 }
Пример #22
0
        // Method for comparing the content of two paths
        public void CompareContent(string userOutputPath, string expectedOutputPath)
        {
            // Trying to excecute the main logic of the method
            // If it detects an exception we throw a new InvalidPathException
            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);

                this.PrintOutput(mismatches, hasMismatch, mismatchPath);
                OutputWriter.WriteMessageOnNewLine("Files read!");
            }
            catch (IOException)
            {
                throw new InvalidPathException();
            }
        }
Пример #23
0
        private static void PrintOutput(string[] mismatches, bool hasMismatch, string mismatchesPath)
        {
            if (hasMismatch)
            {
                foreach (var line in mismatches)
                {
                    OutputWriter.WriteMessageOnNewLine(line);
                }

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

                return;
            }

            OutputWriter.WriteMessageOnNewLine("Files are identicle. There are no mismatches");
        }
Пример #24
0
 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.DisplayException(ExceptionMessages.UnableToGoHigherInPartitionHierarchy);
         }
     }
     else
     {
         string currentPath = SessionData.currentPath;
         currentPath += $"\\{relativePath}";
         ChangeCurrentDirectoryAbsolute(currentPath);
     }
 }
Пример #25
0
        public void CompareContent(string userOuputPath, string expectedOutputPath)
        {
            try
            {
                OutputWriter.WriteMessageOnNewLine("Reading files...");

                string mismatchPath = GetMismatchPath(expectedOutputPath);

                string[] actualOuputLines    = File.ReadAllLines(userOuputPath);
                string[] exptectedOuputLines = File.ReadAllLines(expectedOutputPath);

                bool     hasMismatches;
                string[] mismatches = GetLinesWithPossibleMismatches(
                    actualOuputLines, exptectedOuputLines, out hasMismatches);

                PrintOutput(mismatches, hasMismatches, mismatchPath);
                OutputWriter.WriteMessageOnNewLine("Files read!");
            }
            catch (IOException)
            {
                throw new InvalidPathException();
            }
        }
Пример #26
0
        public static void ShowDirectory()
        {
            try
            {
                foreach (var file in Directory.GetFiles(SessionData.currentPath))
                {
                    int    indexOfLastSlash = file.LastIndexOf("\\");
                    string fileName         = file.Substring(indexOfLastSlash);
                    OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + fileName);
                }

                foreach (var directory in Directory.GetDirectories(SessionData.currentPath))
                {
                    int    indexOfLastSlash = directory.LastIndexOf("\\");
                    string directoryName    = directory.Substring(indexOfLastSlash);
                    OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + directoryName);
                }
            }
            catch (System.UnauthorizedAccessException)
            {
                OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessExceptionMessage);
            }
        }
 /// <summary>
 /// Tries to traverse the folders with given depth. Default depth is 0 (current folder)
 /// </summary>
 /// <param name="input">Current command</param>
 /// <param name="data">Parameters collection: command, depth (0 if not given)</param>
 private static void TryTraverseFolders(string input, string[] data)
 {
     if (data.Length == 1)
     {
         IOManager.TraverseDirectory(0);
     }
     else if (data.Length == 2)
     {
         int depth = 0;
         if (int.TryParse(data[1], out depth))
         {
             IOManager.TraverseDirectory(depth);
         }
         else
         {
             OutputWriter.DisplayException(ExceptionMessages.UnableToParseNumber);
         }
     }
     else
     {
         DisplayInvalidCommandMessage(input);
     }
 }
 private static void TryParseParametersForOrderAndTake(string takeCommand, string takeQuantity, string course, string filter)
 {
     if (takeCommand == "take")
     {
         if (takeQuantity == "all")
         {
             StudentsRepository.OrderAndTake(course, filter);
         }
         else
         {
             int  studentsToTake;
             bool hasParsed = int.TryParse(takeQuantity, out studentsToTake);
             if (hasParsed)
             {
                 StudentsRepository.OrderAndTake(course, filter, studentsToTake);
             }
             else
             {
                 OutputWriter.DisplayException(ExceptionMessages.InvalidTakeQuantityParameter);
             }
         }
     }
 }
Пример #29
0
 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);
     }
 }
Пример #30
0
 public static void ChangeCurrentDirectoryRelative(string relativePath)
 {
     if (relativePath == "..")
     {
         try
         {
             var currentPath      = SessionData.currentPath;
             var indexOfLastSlash = currentPath.LastIndexOf("\\");
             var newPath          = currentPath.Substring(0, indexOfLastSlash);
             SessionData.currentPath = newPath;
         }
         catch (ArgumentOutOfRangeException)
         {
             OutputWriter.DisplayExceptions(ExceptionMessages.UnabelToGoHigherInPartitionHierarchy);
         }
     }
     else
     {
         var currentPath = SessionData.currentPath;
         currentPath += "\\" + relativePath;
         ChangeCurrentDirectoryRelative(currentPath);
     }
 }