コード例 #1
0
        private 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-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)";
                Regex    rgx           = new Regex(pattern);
                string[] allInputLines = File.ReadAllLines(path);

                for (int line = 0; line < allInputLines.Length; line++)
                {
                    string inputLine = allInputLines[line];

                    if (!string.IsNullOrEmpty(inputLine) && rgx.IsMatch(inputLine))
                    {
                        Match  currentMatch = rgx.Match(inputLine);
                        string courseName   = currentMatch.Groups[1].Value;
                        string username     = currentMatch.Groups[2].Value;
                        string scoresStr    = currentMatch.Groups[3].Value;

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

                            if (scores.Any(s => s > 100 || s < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                                continue;
                            }

                            if (scores.Length > SoftUniCourse.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                                continue;
                            }

                            if (!this.studentsByName.ContainsKey(username))
                            {
                                this.studentsByName.Add(username, new SoftUniStudent(username));
                            }

                            if (!this.coursesByName.ContainsKey(courseName))
                            {
                                this.coursesByName.Add(courseName, new SoftUniCourse(courseName));
                            }

                            ICourse  course  = this.coursesByName[courseName];
                            IStudent student = this.studentsByName[username];

                            student.EnrollInCourse(course);
                            student.SetMarkOnCourse(courseName, scores);

                            course.EnrollStudent(student);
                        }
                        catch (FormatException formatException)
                        {
                            OutputWriter.DisplayException($"{formatException.Message} at line: {line}");
                        }
                    }
                }

                this.isDataInitialized = true;
                OutputWriter.WriteMessageOnNewLine("Data read!");
            }
            else
            {
                throw new InvalidPathException();
            }
        }
コード例 #2
0
        private void ReadData(string fileName)
        {
            var path = SessionData.CurrentPath + "\\" + fileName;

            if (File.Exists(path))
            {
                var rgx           = new Regex(@"([A-Z][a-zA-Z#\++]*_[A-Z][a-z]{2}_\d{4})\s+([A-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)");
                var allInputLines = File.ReadAllLines(path);
                for (var i = 0; i < allInputLines.Length; i++)
                {
                    if (!string.IsNullOrEmpty(allInputLines[i]) && rgx.IsMatch(allInputLines[i]))
                    {
                        var currentMatch = rgx.Match(allInputLines[i]);
                        var courseName   = currentMatch.Groups[1].Value;
                        var username     = currentMatch.Groups[2].Value;
                        var scoreStr     = currentMatch.Groups[3].Value;

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

                            if (scores.Any(s => s > 100 && s < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                                continue;
                            }

                            if (scores.Length > SoftUniCourse.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                                continue;
                            }

                            if (!this.students.ContainsKey(username))
                            {
                                this.students.Add(username, new SoftUniStudent(username));
                            }

                            if (!this.courses.ContainsKey(courseName))
                            {
                                this.courses.Add(courseName, new SoftUniCourse(courseName));
                            }

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

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

            this.isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLine("Data read!");
        }
コード例 #3
0
        private void ReadData(string fileName)
        {
            var path = $"{SessionData.CurrentPath}\\{fileName}";

            if (File.Exists(path))
            {
                var   allInputLines = File.ReadAllLines(path);
                var   pattern       = @"(?<courseName>[A-Z][a-zA-Z\#+]*_[A-Z][a-z]{2}_\d{4})\s+(?<userName>[A-Za-z]+\d{2}_\d{2,4})\s(?<score>[\s0-9]+)";
                Regex regex         = new Regex(pattern);

                for (int line = 0; line < allInputLines.Length; line++)
                {
                    try
                    {
                        var inputData = allInputLines[line];
                        if (!String.IsNullOrEmpty(inputData) && regex.IsMatch(inputData))
                        {
                            Match currentMatch = regex.Match(inputData);

                            var courseName = currentMatch.Groups["courseName"].Value;
                            var userName   = currentMatch.Groups["userName"].Value;
                            var scoresStr  = currentMatch.Groups["score"].Value;

                            int[] scores = scoresStr.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                                           .Select(int.Parse)
                                           .ToArray();

                            if (scores.Length > Course.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                                continue;
                            }

                            if (scores.Any(x => x > 100 || x < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                            }

                            if (!this.students.ContainsKey(userName))
                            {
                                this.students.Add(userName, new Student(userName));
                            }

                            if (!this.courses.ContainsKey(courseName))
                            {
                                this.courses.Add(courseName, new Course(courseName));
                            }

                            Course  course  = this.courses[courseName];
                            Student student = this.students[userName];

                            student.EnrollInCourse(course);
                            course.EnrollStudent(student);
                            student.SetMarkOnCourse(courseName, scores);
                        }
                    }
                    catch (FormatException fex)
                    {
                        Console.WriteLine(fex.Message + $" at line : {line}");
                    }
                }
            }

            this.isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLine("Data read!");
        }
        private void ReadData(string fileName)
        {
            string path = $"{SessionData.currentPath}\\{fileName}";

            if (!File.Exists(path))
            {
                throw new InvalidPathException();
            }

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

            foreach (string line in allInputLines)
            {
                Match match = Patterns.DbPattern.Match(line);

                if (!match.Success)
                {
                    continue;
                }

                int year = int.Parse(match.Groups["year"].Value);

                if (!IsYearValid(year))
                {
                    continue;
                }

                string courseName = match.Groups["courseName"].Value;
                string username   = match.Groups["username"].Value;
                string scoresStr  = match.Groups["scores"].Value;

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

                    if (scores.Any(x => x < 0 || x > 100))
                    {
                        OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                    }

                    if (scores.Length > SoftUniCourse.NumberOfTasksOnExam)
                    {
                        OutputWriter.DisplayException(
                            ExceptionMessages.InvalidNumberOfScores);
                        continue;
                    }

                    if (!this.courses.ContainsKey(courseName))
                    {
                        this.courses[courseName] = new SoftUniCourse(courseName);
                    }

                    if (!this.students.ContainsKey(username))
                    {
                        this.students[username] = new SoftUniStudent(username);
                    }

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

                    student.EnrollInCourse(course);
                    student.SetMarksInCourse(courseName, scores);

                    course.EnrollStudent(student);
                }
                catch (FormatException fex)
                {
                    OutputWriter.DisplayException(fex.Message + $"at line : {line}");
                }
            }

            isDataInitialized = true;
            OutputWriter.WriteColoredMessageOnNewLine("Data read!");
        }
コード例 #5
0
        private 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-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)";
                //// my Regex:
                ////string pattern = @"([A-Z][a-zA-Z#\++]*_[A-Za-z]{3}_\d{4})\s+([A-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)";
                //string pattern = @"([A-Z][a-zA-Z#\++]*_[A-Za-z]+)\s+([A-Za-z]+\s+\d{2,4})";


                Regex    regex         = new Regex(pattern);
                string[] allInputLines = File.ReadAllLines(path);

                for (int line = 0; line < allInputLines.Length; line++)
                {
                    bool is1 = !string.IsNullOrEmpty(allInputLines[line]);
                    bool is2 = regex.IsMatch(allInputLines[line]); // тук не мачва!?

                    if (is1 && is2)
                    {
                        Match  currentMatch = regex.Match(allInputLines[line]);
                        string courseName   = currentMatch.Groups[1].Value;
                        string userName     = currentMatch.Groups[2].Value;
                        string scoresStr    = currentMatch.Groups[3].Value;

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

                            if (scores.Any(x => x > 100 || x < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                            }

                            if (scores.Length > Course.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(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));
                            }

                            Course  course  = this.courses[courseName];
                            Student student = this.students[userName];

                            student.EnrollInCourse(course);
                            student.SetMarksInCourse(courseName, scores);
                            course.EnrollStudent(student);
                        }
                        catch (FormatException fex)
                        {
                            OutputWriter.DisplayException(fex.Message + $"at line: {line}");
                        }
                    }
                }
            }
            else
            {
                throw new InvalidPathException();
            }

            this.isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLine("Data read!");
        }
コード例 #6
0
 public void DisplayInvalidCommandMessage(string input)
 {
     OutputWriter.DisplayException($"The command '{input}' is invalid");
 }
コード例 #7
0
 public InvalidPathException()
 {
     OutputWriter.DisplayException(InvalidPath);
 }
コード例 #8
0
        private void ReadData(string fileName)
        {
            string[] inputLines;
            string   path = $"{SessionData.CurrentPath}\\{fileName}";

            if (File.Exists(path))
            {
                inputLines = InputReader.ReadLines(fileName);
            }
            else
            {
                throw new InvalidPathException();
            }

            string pattern = @"(?'course'[A-Z][a-zA-Z#+]*_[A-Z][a-z]{2}_\d{4})\s+(?'username'[A-Za-z]+\d{2}_\d{2,4})\s*(?'marks'(\s\d+)+)";
            var    regex   = new Regex(pattern);

            for (int index = 0; index < inputLines.Length; index++)
            {
                try
                {
                    if (!string.IsNullOrEmpty(inputLines[index]) &&
                        regex.IsMatch(inputLines[index]))
                    {
                        var    currentMatch = regex.Match(inputLines[index]);
                        string courseName   = currentMatch.Groups["course"].Value;
                        string studentName  = currentMatch.Groups["username"].Value;
                        string scoresStr    = currentMatch.Groups["marks"].Value;
                        int[]  scores       = scoresStr
                                              .Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries)
                                              .Select(int.Parse)
                                              .ToArray();

                        if (scores.Any(sc => sc > 100 || sc < 0))
                        {
                            OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                            continue;
                        }

                        if (scores.Length > Course.NumberOfTasksOnExam)
                        {
                            OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                            continue;
                        }

                        if (!this.students.ContainsKey(studentName))
                        {
                            this.students.Add(studentName, new Student(studentName));
                        }

                        if (!this.courses.ContainsKey(courseName))
                        {
                            this.courses.Add(courseName, new Course(courseName));
                        }

                        var course  = this.courses[courseName];
                        var student = this.students[studentName];

                        student.EnrollInCourse(course);
                        student.SetMarkOnCourse(courseName, scores);

                        course.EnrollStudent(student);
                    }
                }
                catch (FormatException fex)
                {
                    OutputWriter.DisplayException(fex.Message + $" at line: {index}");
                }
            }

            this.isDataInitialized = true;
            OutputWriter.WriteMessageLine("Data read!", LogColor);
        }
コード例 #9
0
        private void ReadData(string fileName)
        {
            //Advanced C#
            //If you follow the BashSoft.docx you'll end up nowhere in PartII - Problem 5 Test your code
            //You need to make it readable from static data like data.txt until you get to PartIV - Problem 18 Read database!

            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-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)";
                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 courseName   = currentMatch.Groups[1].Value;
                        string username     = currentMatch.Groups[2].Value;
                        string scoresStr    = currentMatch.Groups[3].Value;
                        try
                        {
                            int[] scores = scoresStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                           .Select(int.Parse)
                                           .ToArray();
                            if (scores.Any(x => x > 100 || x < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                            }
                            if (scores.Length > SoftUniCourse.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                                continue;
                            }
                            if (!this.students.ContainsKey(username))
                            {
                                this.students.Add(username, new SoftUniStudent(username));
                            }
                            if (!this.courses.ContainsKey(courseName))
                            {
                                this.courses.Add(courseName, new SoftUniCourse(courseName));
                            }

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

                            student.EnrollInCourse(course);
                            student.SetMarkOnCourse(courseName, scores);

                            course.EnrollStudent(student);
                        }
                        catch (FormatException fex)
                        {
                            OutputWriter.DisplayException(fex.Message + $"at line: {line}");
                        }
                    }
                }
            }
            else
            {
                throw new InvalidPathException();
            }
            isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLine("Data read!");
        }
コード例 #10
0
        private 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-Za-z]+\d{2}_\d{2,4})\s([\s0-9]+)";
                Regex  regex   = new Regex(pattern);

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


                foreach (string line in allInputLines)
                {
                    if (!string.IsNullOrEmpty(line) && regex.IsMatch(line))
                    {
                        Match currentMatch = regex.Match(line);

                        string courseName  = currentMatch.Groups[1].Value;
                        string studentName = currentMatch.Groups[2].Value;

                        string scoresStr = currentMatch.Groups[3].ToString();

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

                            if (scores.Any(x => x > 100 || x < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                                continue;
                            }

                            if (scores.Length > Course.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                                continue;
                            }

                            if (!this.students.ContainsKey(studentName))
                            {
                                this.students.Add(studentName, new Student(studentName));
                            }

                            if (!this.courses.ContainsKey(courseName))
                            {
                                this.courses.Add(courseName, new Course(courseName));
                            }

                            Course  course  = this.courses[courseName];
                            Student student = this.students[studentName];

                            student.EnrollInCourse(course);
                            student.SetMarkOnCourse(courseName, scores);

                            course.EnrollStudent(student);
                        }
                        catch (Exception fex)
                        {
                            OutputWriter.DisplayException($"{fex.Message} at line : {line}");
                        }
                    }
                }
            }
            else
            {
                throw new InvalidPathException();
                //OutputWriter.DisplayException(ExceptionMessages.InvalidPath);
            }

            isDataInitialized = true;
            OutputWriter.WriteMessageOnNewLine($"Data read!");
        }
コード例 #11
0
        private void ReadData(string fileName)
        {
            string path = SessionData.currentPath + "\\" + fileName;

            if (File.Exists(path))
            {
                OutputWriter.WriteMessageOnNewLine("Reading data...");

                //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   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]+)";
                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 courseName   = currentMatch.Groups[1].Value;
                        string username     = currentMatch.Groups[2].Value;
                        string scoreStr     = currentMatch.Groups[3].Value;

                        try
                        {
                            int[] scores = scoreStr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                           .Select(int.Parse)
                                           .ToArray();
                            if (scores.Any(x => x > 100 || x < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                                continue;
                            }

                            if (scores.Length > SoftUniCourse.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOfScores);
                                continue;
                            }

                            if (!this.students.ContainsKey(username))
                            {
                                this.students.Add(username, new SoftUniStudent(username));
                            }

                            if (!this.courses.ContainsKey(courseName))
                            {
                                this.courses.Add(courseName, new SoftUniCourse(courseName));
                            }

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

                            //if (student.enrolledCourses.ContainsKey(courseName))
                            //{
                            //    student.SetMarkOnCourse(courseName, scores);
                            //    continue;
                            //}

                            student.EnrollInCourse(course);
                            student.SetMarkOnCourse(courseName, scores);
                            course.EnrollStudent(student);
                        }
                        catch (FormatException fex)
                        {
                            OutputWriter.DisplayException(fex.Message + $"at line : {line}");
                        }
                    }
                }
                isDataInitialized = true;
                OutputWriter.WriteMessageOnNewLine("Data read!");
            }
            else
            {
                throw new IOException(ExceptionMessages.InvalidPath);
            }
        }
コード例 #12
0
        private void ReadData(string fileName)
        {
            string path = SessionData.currentPath + "\\" + fileName;

            if (File.Exists(path))
            {
                string pattern = @"(?<course>[A-Z][a-zA-Z#\+]*_[A-Z][a-z]{2}_\d{4})\s+(?<username>[A-Za-z]+\d{2}_\d{2,4})\s(?<score>[\s0-9]+)";

                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 courseName   = currentMatch.Groups["course"].Value;
                        string username     = currentMatch.Groups["username"].Value;
                        string scoreStr     = currentMatch.Groups["score"].Value;


                        try
                        {
                            int[] scores = scoreStr.Split(' ', System.StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

                            if (scores.Any(x => x > 100 || x < 0))
                            {
                                OutputWriter.DisplayException(ExceptionMessages.InvalidScore);
                                continue;
                            }

                            if (scores.Length > Course.NumberOfTasksOnExam)
                            {
                                OutputWriter.DisplayException(InvalidNumberOfScoresException.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 fex)
                        {
                            OutputWriter.DisplayException(fex.Message + $"at line : {line}");
                            throw;
                        }
                    }
                }
                isDatainitialized = true;
                OutputWriter.WriteMessageOnNewLine("Data read!");
            }
            else
            {
                throw new InvalidPathException();
            }
        }