Пример #1
0
    internal static void CreateDirectory(string path)
    {
        path = IOManager.BuildAbsolutePath(path);
        string existingPath = String.Empty;

        string[] pathToCreate = path.Split('\\', StringSplitOptions.RemoveEmptyEntries);
        try
        {
            for (int l = 0; l < pathToCreate.Length; l++)
            {
                string currentPath = existingPath + pathToCreate[l];
                if (!Directory.Exists(currentPath))
                {
                    Directory.CreateDirectory(currentPath);
                    IOManager.OutputLine(typeof(Feedback), String.Format(
                                             new DirectoryCreationFeedback().ResultMessage, pathToCreate[l], existingPath));
                }
                existingPath = $"{currentPath}\\";
            }
        }
        catch (Exception exception)
        {
            if (exception is ArgumentException || exception is NotSupportedException)
            {
                throw new InvalidNameException("Directory");
            }
            else if (exception is UnauthorizedAccessException)
            {
                throw new InsufficientPrivilegesException();
            }
        }
    }
Пример #2
0
    internal static string[] ReadFile(string path)
    {
        string fileName = IOManager.ExtractFileName(path);

        if (String.IsNullOrEmpty(fileName))
        {
            throw new FileNotSpecifiedException();
        }
        path = IOManager.BuildAbsolutePath(path);
        string filePath = $"{path}\\{fileName}";

        try
        {
            string[] fileContents = File.ReadAllLines(filePath);
            return(fileContents);
        }
        catch (Exception exception)
        {
            if (exception is DirectoryNotFoundException || exception is FileNotFoundException)
            {
                throw new InvalidPathException();
            }
            else if (exception is UnauthorizedAccessException)
            {
                throw new InsufficientPrivilegesException();
            }
            return(null);
        }
    }
Пример #3
0
    internal static void DownloadFile(string sourcePath)
    {
        string fileName = IOManager.ExtractFileName(sourcePath);

        if (String.IsNullOrEmpty(fileName))
        {
            throw new FileNotSpecifiedException();
        }
        else
        {
            sourcePath = IOManager.BuildAbsolutePath(sourcePath);
            string sourceFilePath       = $"{sourcePath}\\{fileName}";
            string destinationFilePath  = $"{CurrentDirectory}\\{fileName}";
            var    fileDownloadFeedback = new FileDownloadingFeedback();
            try
            {
                File.Copy(sourceFilePath, destinationFilePath);
                IOManager.OutputLine(typeof(Feedback), String.Format(
                                         fileDownloadFeedback.ResultMessage, fileName));
            }
            catch (Exception exception)
            {
                if (exception is DirectoryNotFoundException || exception is FileNotFoundException)
                {
                    throw new InvalidPathException();
                }
                else if (exception is UnauthorizedAccessException)
                {
                    throw new InsufficientPrivilegesException();
                }
                else if (exception is IOException)
                {
                    if (File.Exists(destinationFilePath))
                    {
                        IOManager.Output(typeof(Exception), new FileAlreadyDownloadedException().Message);
                        ConsoleKeyInfo choice = Console.ReadKey();
                        IOManager.OutputLine();
                        if (choice.Key == ConsoleKey.Y)
                        {
                            File.Copy(sourceFilePath, destinationFilePath, true);
                            IOManager.OutputLine(typeof(Feedback), String.Format(
                                                     fileDownloadFeedback.EndMessage, fileName));
                        }
                        else
                        {
                            IOManager.OutputLine(typeof(Feedback), fileDownloadFeedback.AbortMessage);
                        }
                    }
                    else
                    {
                        throw exception;
                    }
                }
            }
        }
    }
Пример #4
0
    internal void CompareFiles(string userOutputPath, string expectedOutputPath)
    {
        string userOutputFile = IOManager.ExtractFileName(userOutputPath);

        userOutputPath = IOManager.BuildAbsolutePath(userOutputPath);
        string userOutputFilePath = $"{userOutputPath}\\{userOutputFile}";
        string expectedOutputFile = IOManager.ExtractFileName(expectedOutputPath);

        expectedOutputPath = IOManager.BuildAbsolutePath(expectedOutputPath);
        string expectedOutputFilePath = $"{expectedOutputPath}\\{expectedOutputFile}";
        var    fileReadingFeedback    = new FileReadingFeedback();

        IOManager.OutputLine(typeof(Feedback), fileReadingFeedback.BeginMessage);
        try
        {
            var      fileComparisonFeedback = new FileComparisonFeedback();
            string[] actualOutput           = File.ReadAllLines(userOutputFilePath);
            string[] expectedOutput         = File.ReadAllLines(expectedOutputFilePath);
            IOManager.OutputLine(typeof(Feedback), fileReadingFeedback.ProgressMessage);
            bool     hasMismatch;
            string[] comparisonResults = Compare(actualOutput, expectedOutput, out hasMismatch);
            if (hasMismatch)
            {
                string mismatchesFilePath = $"{expectedOutputPath}\\mismatches.txt";
                FSManager.CreateFile(mismatchesFilePath, comparisonResults);
                string[] mismatches = FSManager.ReadFile(mismatchesFilePath);
                IOManager.OutputLine(typeof(Feedback), fileComparisonFeedback.ProgressMessage);
                IOManager.DisplayFileContents(mismatches);
                IOManager.OutputLine(typeof(Feedback), String.Format(
                                         fileComparisonFeedback.ResultMessage, mismatchesFilePath));
            }
            else if (comparisonResults != null)
            {
                IOManager.OutputLine(typeof(Feedback), fileComparisonFeedback.EndMessage);
            }
        }
        catch (Exception exception)
        {
            if (exception is DirectoryNotFoundException || exception is FileNotFoundException)
            {
                throw new InvalidPathException();
            }
            else if (exception is UnauthorizedAccessException)
            {
                throw new InsufficientPrivilegesException();
            }
        }
    }
Пример #5
0
 internal static void ChangeDirectory(string destinationPath)
 {
     destinationPath = IOManager.BuildAbsolutePath(destinationPath);
     try
     {
         Directory.SetCurrentDirectory(destinationPath);
     }
     catch (Exception exception)
     {
         if (exception is DirectoryNotFoundException)
         {
             throw new InvalidPathException();
         }
         else if (exception is ArgumentOutOfRangeException)
         {
             throw new InvalidCommandParameterException("Directory path");
         }
         else if (exception is UnauthorizedAccessException)
         {
             throw new InsufficientPrivilegesException();
         }
     }
 }
Пример #6
0
    internal static void LoadData(string path)
    {
        var dbInitFeedback = new DatabaseInitializationFeedback();

        if (!IsDatabaseInitialized)
        {
            string fileName = IOManager.ExtractFileName(path);
            path = IOManager.BuildAbsolutePath(path);
            IOManager.OutputLine(typeof(Feedback), dbInitFeedback.BeginMessage);
            string[] databaseSource = FSManager.ReadFile($"{path}\\{fileName}");
            if (databaseSource.Length == 0)
            {
                throw new DatabaseSourceEmptyException();
            }
            IOManager.OutputLine(typeof(Feedback), dbInitFeedback.ProgressMessage);
            foreach (string record in databaseSource.Where(r => IsRecordValid(r)))
            {
                try
                {
                    Match   validRecord = Regex.Match(record, dbRecordPattern);
                    Course  course      = new Course(validRecord.Groups["Course"].Value);
                    Student student     = new Student(validRecord.Groups["Student"].Value);
                    int[]   scores      = validRecord.Groups["Scores"].Value.Split().Select(int.Parse).ToArray();
                    if (!Courses.Any(c => c.Name == course.Name))
                    {
                        Courses.Add(course);
                    }
                    else
                    {
                        course = Courses.First(c => c.Name == course.Name);
                    }
                    if (!Students.Any(s => s.Name == student.Name))
                    {
                        Students.Add(student);
                    }
                    else
                    {
                        student = Students.First(s => s.Name == student.Name);
                    }
                    course.EnrollStudent(student.Name);
                    course.SetScoresForStudent(student.Name, scores);
                    student.EnrollInCourse(course.Name);
                    student.SetScoresForCourse(course.Name, scores);
                }
                catch (Exception exception)
                {
                    if (exception is DirectoryNotFoundException || exception is FileNotFoundException)
                    {
                        IOManager.OutputLine(typeof(Exception), new InvalidPathException().Message);
                    }
                    else if (exception is UnauthorizedAccessException)
                    {
                        IOManager.OutputLine(typeof(Exception), new InsufficientPrivilegesException().Message);
                    }
                }
            }
            IOManager.OutputLine(typeof(Feedback), dbInitFeedback.EndMessage);
            IOManager.OutputLine(typeof(Feedback), String.Format(
                                     dbInitFeedback.ResultMessage, Students.Count));
            IsDatabaseInitialized = true;
        }
        else
        {
            IOManager.Output(typeof(Exception), new DatabaseAlreadyInitializedException().Message);
            ConsoleKeyInfo choice = Console.ReadKey();
            IOManager.OutputLine();
            if (choice.Key == ConsoleKey.Y)
            {
                DeleteData();
            }
            else
            {
                IOManager.OutputLine(typeof(Feedback), dbInitFeedback.AbortMessage);
            }
        }
    }