Advertisement
Danny_Berova

notAProblem

Feb 4th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. namespace BashSoft
  2. {
  3.     using System.Collections.Generic;
  4.     using System.IO;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     public static class StudentsRepository
  8.     {
  9.         public static bool isDatainitialized = false;
  10.         private static Dictionary<string, Dictionary<string, List<int>>> studentsByCourse;
  11.        
  12.         public static void InitializeData(string fileName)
  13.         {      
  14.             if (!isDatainitialized)
  15.             {
  16.                 OutputWriter.WriteMessageOnNewLine("Reading data...");
  17.                 studentsByCourse = new Dictionary<string, Dictionary<string, List<int>>>();
  18.                 ReadData(fileName);
  19.             }
  20.             else    
  21.             {
  22.                 OutputWriter.WriteMessageOnNewLine(ExceptionMessages.DataAlreadyInitializedException);
  23.             }
  24.         }
  25.        
  26.         private static void ReadData(string fileName)
  27.         {
  28.             string path = SessionData.currentPath + "\\" + fileName;
  29.  
  30.             if (File.Exists(path))
  31.             {
  32.                 string pattern = @"^(?<course>[A-Z][a-zA-Z#+]*_[A-Z][a-z]{2}_\d{4})\s+(?<username>[A-Z][a-z]{0,3}\d{2}_\d{2,4})\s+(?<score>\d+)$";
  33.                 Regex rgx = new Regex(pattern);
  34.                 string[] allInputLines = File.ReadAllLines(path);
  35.  
  36.                 for (int line = 0; line < allInputLines.Length; line++)
  37.                 {
  38.                     if (!string.IsNullOrEmpty(allInputLines[line]) && rgx.IsMatch(allInputLines[line]))
  39.                     {
  40.                         Match currentMatch = rgx.Match(allInputLines[line]);
  41.                         string courseName = currentMatch.Groups["course"].Value;
  42.                         string username = currentMatch.Groups["username"].Value;
  43.                         int studentScoreOnTask;
  44.                         bool hasParsedScore = int.TryParse(currentMatch.Groups["score"].Value, out studentScoreOnTask);
  45.                        
  46.                         if (hasParsedScore && studentScoreOnTask >= 0 && studentScoreOnTask <= 100)
  47.                         {
  48.                             if (!studentsByCourse.ContainsKey(courseName))
  49.                             {
  50.                                 studentsByCourse.Add(courseName, new Dictionary<string, List<int>>());
  51.                             }
  52.                             if (!studentsByCourse[courseName].ContainsKey(username))
  53.                             {
  54.                                 studentsByCourse[courseName].Add(username, new List<int>());
  55.                             }
  56.                         }
  57.                         studentsByCourse[courseName][username].Add(studentScoreOnTask);
  58.                     }
  59.                 }
  60.                 isDatainitialized = true;
  61.                 OutputWriter.WriteMessageOnNewLine("Data read!");
  62.             }        
  63.             else
  64.             {
  65.                 OutputWriter.DisplayException(ExceptionMessages.InvalidPath);
  66.             }
  67.         }
  68.  
  69.         private static bool IsQueryForCoursePossible(string courseName)
  70.         {
  71.             if (isDatainitialized)
  72.             {
  73.                 if (studentsByCourse.ContainsKey(courseName))
  74.                 {
  75.                     return true;
  76.                 }
  77.                 else
  78.                 {
  79.                     OutputWriter.DisplayException(ExceptionMessages.InexistingCourseInDataBase);
  80.                 }
  81.             }
  82.             else
  83.             {
  84.                 OutputWriter.DisplayException(ExceptionMessages.DataNotInitializedExceptionMessage);
  85.             }
  86.        
  87.             return false;
  88.         }
  89.  
  90.         private static bool IsQueryForStudentPossiblе(string courseName, string studentUsername)
  91.         {
  92.             if (IsQueryForCoursePossible(courseName) && studentsByCourse[courseName].ContainsKey(studentUsername))
  93.             {
  94.                 return true;
  95.             }
  96.             else
  97.             {
  98.                 OutputWriter.DisplayException(ExceptionMessages.InexistingStudentInDataBase);
  99.             }
  100.        
  101.             return false;
  102.         }
  103.  
  104.         public static void GetStudentScoresFromCourse(string courseName, string username)
  105.         {
  106.             if (IsQueryForStudentPossiblе(courseName, username))
  107.             {
  108.                 OutputWriter.PrintStudent(new KeyValuePair<string, List<int>>(username, studentsByCourse[courseName][username]));
  109.             }
  110.         }
  111.  
  112.         public static void GetAllStudentsFromCourse(string courseName)
  113.         {
  114.             if (IsQueryForCoursePossible(courseName))
  115.             {
  116.                 OutputWriter.WriteMessageOnNewLine($"{courseName}:");
  117.                 foreach (var studentMarksEntry in studentsByCourse[courseName])
  118.                 {
  119.                     OutputWriter.PrintStudent(studentMarksEntry);
  120.                 }
  121.             }
  122.         }
  123.  
  124.         public static void FilterAndTake(string courseName, string givenFilter, int? studentsToTake = null)
  125.         {
  126.             if (IsQueryForCoursePossible(courseName))
  127.             {
  128.                 if (studentsToTake == null)
  129.                 {
  130.                     studentsToTake = studentsByCourse[courseName].Count;
  131.                 }
  132.                 RepositoryFilters.FilterAndTake(studentsByCourse[courseName], givenFilter, studentsToTake.Value);
  133.             }
  134.         }
  135.  
  136.         public static void OrderAndTake(string courseName, string comparison, int? studentsToTake = null)
  137.         {
  138.             if (IsQueryForCoursePossible(courseName))
  139.             {
  140.                 if (studentsToTake == null)
  141.                 {
  142.                     studentsToTake = studentsByCourse[courseName].Count;
  143.                 }
  144.                 RepositorySorters.OrderAndTake(studentsByCourse[courseName], comparison, studentsToTake.Value);
  145.             }
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement