// Main method in this class, used for reading data and accepting a fileName string as a parameter private void ReadData(string fileName) { string path = SessionData.currentPath + @"\" + fileName; if (File.Exists(path)) { // Regex pattern used for detecting all the valid input lines 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 username = currentMatch.Groups[2].Value; string studentsStr = currentMatch.Groups[3].Value; try { int[] scores = studentsStr.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 > SoftUniCourse.MaxScoreOnExamTask) { 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)); } // Taking the currently needed course and student ICourse course = this.courses[courseName]; IStudent student = this.students[username]; // Using the previously created methods in the Student and Course classes 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!"); }
private void ReadData(string fileName) { string path = SessionData.CurrentPath + "\\" + fileName; if (File.Exists(path)) { OutputWriter.WriteMessageOnNewLine("Reading data..."); // File exists 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++) { try { if (!string.IsNullOrEmpty(allInputLines[line]) && regex.IsMatch(allInputLines[line])) { Match currMatch = regex.Match(allInputLines[line]); string courseName = currMatch.Groups[1].Value; string userName = currMatch.Groups[2].Value; string scoresStr = currMatch.Groups[3].Value; int[] scores = scoresStr.Split(" ".ToCharArray(), 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)); } ICourse course = this.courses[courseName]; IStudent student = this.students[userName]; student.EnrollInCourse(course); student.SetMarksInCourse(courseName, scores); course.EnrollStudent(student); } } catch (FormatException e) { throw new FormatException(e.Message + $" at line: {line + 1}!"); } } OutputWriter.WriteMessageOnNewLine("Data read!"); } else { try { throw new InvalidPathException(); } catch (InvalidPathException ex) { OutputWriter.WriteMessage(ex.Message); } } }
private void ReadData(string fileName) { string 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 (int i = 0; i < allInputLines.Length; i++) { if (!string.IsNullOrEmpty(allInputLines[i]) && rgx.IsMatch(allInputLines[i])) { Match currentMatch = rgx.Match(allInputLines[i]); 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(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)); } 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 : {i}"); } } } } else { OutputWriter.WriteMessageOnNewLine(ExceptionMessages.InvalidPath); return; } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
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!"); }
private void ReadData(string fileName) { string path = SessionData.currentPath + "\\" + fileName; if (File.Exists(path)) { var 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])) { var currentMatch = regex.Match(allInputLines[line]); string course = currentMatch.Groups[1].Value; var courseYear = int.Parse(course.Substring(course.LastIndexOf('_') + 1)); if (courseYear < 2014 || courseYear > DateTime.Now.Year) { continue; } var student = currentMatch.Groups[2].Value; var scoresStr = currentMatch.Groups[3].Value; try { int[] scores = scoresStr .Split(' ', StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse) .ToArray(); if (scores.Any(s => s < 0 || s > 100)) { throw new InvalidScoreException(); } if (scores.Length > SoftUniCourse.NumberOfTasksOnExam) { throw new InvalidNumberOfScoresException(); } if (!this.students.ContainsKey(student)) { this.students[student] = new SoftUniStudent(student); } if (!this.courses.ContainsKey(course)) { this.courses[course] = new SoftUniCourse(course); } ICourse currentCourse = this.courses[course]; IStudent currentStudent = this.students[student]; currentStudent.EnrollInCourse(currentCourse); currentStudent.SetMarksOnCourse(course, scores); currentCourse.EnrollStudent(currentStudent); } catch (InvalidNumberOfScoresException inose) { OutputWriter.DisplayException(inose.Message); } catch (InvalidScoreException ise) { OutputWriter.DisplayException(ise.Message); } catch (FormatException fex) { OutputWriter.DisplayException(fex.Message + $".at line : {line}"); } } } this.isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { throw new InvalidPathException(); } }
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)); } 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}"); } } } } else { throw new InvalidPathException(); } this.isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
private void ReadData(string fileName) { var path = SessionData.currentPath + "\\" + fileName; if (File.Exists(path)) { var 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); var 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]); var scoresStr = currentMatch.Groups[3].Value; try { var scores = scoresStr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse).ToArray(); var currCourse = currentMatch.Groups[1].Value; var currStudent = currentMatch.Groups[2].Value; if (scores.Any(x => x > 100 || x < 0)) { OutputWriter.DisplayException(ExceptionMessages.InvalidScore); } if (scores.Length > SoftUniCourse.NumberOfTasksOnExam) { OutputWriter.DisplayException(ExceptionMessages.InvalidNumberOrScores); } if (!this.students.ContainsKey(currStudent)) { this.students.Add(currStudent, new SoftUniStudent(currStudent)); } if (!this.courses.ContainsKey(currStudent)) { this.courses.Add(currStudent, new SoftUniCourse(currStudent)); } ICourse course = this.courses[currCourse]; IStudent student = this.students[currStudent]; student.EnrollInCourse(course); student.SetMarkOnCourse(currCourse, scores); course.EnrollStudent(student); } catch (FormatException fex) { OutputWriter.DisplayException(fex.Message + $"at line : {line}"); } } } } else { throw new Exception(ExceptionMessages.InvalidPath); } IsDataInitialised = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
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(); } }
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!"); }
private void ReadData(string fileName) { string path = SessionData.currentPath + "\\" + fileName; if (!File.Exists(path)) { throw new InvalidPathException(); } // Course name – starts with a capital letter and may contain only small and capital English letters, plus ‘+’ or hashtag ‘#’. // Course instance – should be in the format ‘Feb_2015’, e.g.containing the first three letters of the month, starting with a capital letter, followed by an underscore and the year. The year should be between 2014 and the current year. // regex: ([A-Z][a-zA-Z#+]*_[A-Z][a-z]{2}_\d{4})\s+ // Username – starts with a capital letter and should be followed by at most 3 small letters after that. Then it should have 2 digits, followed by an underscore, followed by two to four digits. // ([A-Z][a-z]{0,3}\d{2}_\d{2,4}) // 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 scoresStr = currentMatch.Groups[3].Value; try { int[] scores = scoresStr.Split(' ', StringSplitOptions.RemoveEmptyEntries) .Select(int.Parse).ToArray(); if (scores.Any(x => x > 100 || x < 0)) { OutputWriter.DisplayException(ExceptionMessages.InvalidScore); continue; } if (scores.Length > Course.NumberOfTaskOnExam) { 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)); } 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}"); } } } isDataInitialzed = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
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!"); }
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(); } }