private void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayException($"The command '{input}' is invalid."); }
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])) { 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 > 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)); } 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(); } isDataInilized = 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-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(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.SetMarkOnCourse(courseName, scores); course.EnrollStudent(student); } catch (FormatException fex) { OutputWriter.DisplayException(fex.Message + $"at line : {line}"); } //int studentScoreOnTask; //bool hasParsedScore = int.TryParse(currentMatch.Groups[3].Value, out studentScoreOnTask); //if (hasParsedScore && studentScoreOnTask >= 0 && studentScoreOnTask <= 100) //{ // if (!studentsByCourse.ContainsKey(courseName)) // { // studentsByCourse.Add(courseName, new Dictionary<string, List<int>>()); // } // if (!studentsByCourse[courseName].ContainsKey(username)) // { // studentsByCourse[courseName].Add(username, new List<int>()); // } // studentsByCourse[courseName][username].Add(studentScoreOnTask); //} } } } else { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }
public static void InterpredCommand(string input) { List <string> commandParameters = input.Split().ToList(); string command = commandParameters[0].ToLower(); commandParameters.RemoveAt(0); switch (command) { case "open": if (ValidateCommandLength(command, commandParameters, 1)) { OpenFile(commandParameters); } break; case "mkdir": if (ValidateCommandLength(command, commandParameters, 1)) { CreateDirectory(input, commandParameters); } break; case "ls": if (commandParameters.Count() != 0) { if (!ValidateCommandLength(input, commandParameters, 1)) { break; } else { bool hasParsed = int.TryParse(commandParameters[0], out int depth); if (hasParsed) { IOManager.TraverseDirectory(depth); } else { OutputWriter.DisplayException(ExceptionMessages.UnableToParseNumber); } } } else { IOManager.TraverseDirectory(0); } break; case "cmp": if (ValidateCommandLength(command, commandParameters, 2)) { CompareFiles(commandParameters); } break; case "cdrel": if (ValidateCommandLength(command, commandParameters, 1)) { ChangeDirectoryRelative(commandParameters); } break; case "cdabs": if (ValidateCommandLength(command, commandParameters, 1)) { ChangeDirectoryAbsolute(commandParameters); } break; case "readdb": if (ValidateCommandLength(command, commandParameters, 1)) { ReadDatabaseFromFile(commandParameters); } break; case "help": DisplayHelp(); break; case "filter": if (ValidateCommandLength(command, commandParameters, 4)) { string courseName = commandParameters[0]; string filter = commandParameters[1].ToLower(); string takeCommand = commandParameters[2].ToLower(); string takeQuantity = commandParameters[3].ToLower(); TryParseParametersForFilterAndTake(takeCommand, takeQuantity, courseName, filter); } break; case "order": if (ValidateCommandLength(command, commandParameters, 4)) { string courseName = commandParameters[0]; string orderType = commandParameters[1].ToLower(); string orderCommand = commandParameters[2].ToLower(); string orderQuantity = commandParameters[3].ToLower(); TryParseParametersForOrderAndTake(orderCommand, orderQuantity, courseName, orderType); } break; case "download": if (ValidateCommandLength(command, commandParameters, 2)) { DownloadFile(commandParameters); } break; case "downloadasynch": DownloadFileAsync(commandParameters); break; case "show": TryShowWantedData(commandParameters, command); break; default: OutputWriter.DisplayException(ExceptionMessages.InvalidCommand, (input)); break; } }
public static void InterpredCommand(string input) { string[] data = input.Split(' '); string command = data[0]; int indexOfFirstSpace = input.IndexOf(' '); string inputAfterCommand = input.Substring(indexOfFirstSpace + 1); switch (command) { case "mkdir": if (data.Length == 2) { IOManager.CreateDirectoryInCurrentFolder(data[1]); } else { OutputWriter.DisplayException(ExceptionMessages.InvalidCommandParams); } break; case "ls": if (data.Length == 1) { IOManager.ShowDirectory(); } else { OutputWriter.DisplayException(command + ExceptionMessages.UnNeededParameters); } break; case "clear": if (data.Length == 1) { Console.Clear(); } else { OutputWriter.DisplayException(command + ExceptionMessages.UnNeededParameters); } break; case "cd": if (data.Length >= 2) { IOManager.ChangeCurrentDirectoryRelative(inputAfterCommand); } else { OutputWriter.DisplayException(command + ExceptionMessages.InvalidCommandParams); } break; case "readDb": StudentsRepository.InitializeData(inputAfterCommand); break; case "filter": if (data.Length == 2) { StudentsRepository.GetAllStudentsFromCourse(data[1]); } break; case "download": if (data.Length > 1) { IOManager.DownloadFile(inputAfterCommand); } else { OutputWriter.DisplayException(ExceptionMessages.MissingURL); } break; case "open": if (data.Length == 2) { IOManager.OpenFile(data[1]); } else { OutputWriter.DisplayException(ExceptionMessages.MissingURL); } break; case "help": if (data.Length == 1) { IOManager.Help(); } else { OutputWriter.DisplayException(command + ExceptionMessages.UnNeededParameters); } break; case "s": if (data.Length == 2) { PremiumMobileRepository.SummarizeEmails(data[1]); } break; default: OutputWriter.DisplayException(ExceptionMessages.InvalidCommand); break; } }
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++) { 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(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 softUniCourse = this.courses[courseName]; IStudent softUniStudent = this.students[username]; softUniStudent.EnrollInCourse(softUniCourse); softUniStudent.SetMarkOnCourse(courseName, scores); softUniCourse.EnrollStudent(softUniStudent); } catch (FormatException fex) { throw new FormatException(fex.Message + $"at line : {line}"); } } } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { throw new InvalidPathException(); } }
public static void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayException(string.Format(ExceptionMessages.InvalidCommand, input)); }
private static void DisplayInvalidCommandMessage(string input) { OutputWriter.DisplayException($"The command {input} is invalid"); }
private static 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-Z][a-z]{0,3}\d{2}_\d{2,4})\s+(\d+)"; 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 course = currentMatch.Groups[1].Value; string student = currentMatch.Groups[2].Value; int studentScoreOnTask; bool hasParsedScore = int.TryParse(currentMatch.Groups[3].Value, out studentScoreOnTask); if (hasParsedScore && studentScoreOnTask >= 0 && studentScoreOnTask <= 100) { if (studentsByCourse.ContainsKey(course)) { if (studentsByCourse[course].ContainsKey(student)) { studentsByCourse[course][student].Add(studentScoreOnTask); } else { studentsByCourse[course].Add(student, new List <int>() { studentScoreOnTask }); } } else { studentsByCourse.Add(course, new Dictionary <string, List <int> > { { student, new List <int>() { studentScoreOnTask } } }); } } } } } else { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine($"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!"); }
public static void InterpredCommand(string input) { string[] data = input.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string command = data[0]; switch (command) { case "open": TryOpenFile(input, data); break; case "mkdir": TryCreateDirectory(data); TryOpenFile(input, data); break; case "ls": TryTraverseFolder(input, data); break; case "cmp": TryCompareFiles(input, data); break; case "cdRel": TryChangePathRelatively(input, data); break; case "cdAbs": TryChangePathAbsolute(input, data); break; case "readDb": TryReadDatabaseFromFile(input, data); break; case "help": TryGetHelp(input, data); break; case "show": TryShowWantedData(input, data); break; case "filter": TryFilterAndTake(input, data); break; case "order": TryOrderAndTake(input, data); break; case "decOrder": //TODO: break; case "download": //TODO: break; case "downloadAsynch": //TODO: break; default: OutputWriter.DisplayException(string.Format(ExceptionMessages.InvalidCommand, input)); break; } }
public void ReadData(string fileName) { string path = $"{SessionData.currentPath}\\{fileName}"; if (File.Exists(path)) { string pattern = @"(?<Course>[A-Z][a-z#+A-Z]*_[A-Z][a-z]{2}_\d{4})\s+(?<Student>[A-Za-z]+\d{2}_\d{2,4})\s(?<Mark>[\s0-9]+)"; Regex rgx = new Regex(pattern); string[] allInputLines = File.ReadAllLines(path); for (int line = 0; line < allInputLines.Length; line++) { if (!string.IsNullOrWhiteSpace(allInputLines[line]) && rgx.IsMatch(allInputLines[line])) { Match currentMatch = rgx.Match(allInputLines[line]); string courseName = currentMatch.Groups["Course"].Value; string userName = currentMatch.Groups["Student"].Value; string scoresStr = currentMatch.Groups["Mark"].Value; try { int[] scores = scoresStr.Split(new[] { ' ' }, 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.SetMarkOnCourse(courseName, scores); course.EnrollStudent(student); } catch (FormatException fex) { OutputWriter.DisplayException(fex.Message + $"at line : {line}"); } } } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); } else { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } }
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]+)"; var regex = new Regex(pattern); var allInputLines = File.ReadAllLines(path); for (int line = 0; line < allInputLines.Length; line++) { try { if (!String.IsNullOrEmpty(allInputLines[line]) && regex.IsMatch(allInputLines[line])) { var currentMatch = regex.Match(allInputLines[line]); var courseName = currentMatch.Groups[1].Value; var username = currentMatch.Groups[2].Value; var scoresStr = currentMatch.Groups[3].Value; var scores = scoresStr.Split(" ", 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.SetGradesOnCourse(courseName, scores); course.EnrollStudent(student); } } catch (FormatException ex) { OutputWriter.DisplayException(ex.Message + $"at line: {line}"); } } } else { throw new InvalidPathException(); } isDataInitialized = true; OutputWriter.WriteMessageOnNewLine("Data read!"); }