Advertisement
Prohause

Mentor Group Refactored

Oct 20th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. namespace DefiningClasses
  7. {
  8. public class StartUp
  9. {
  10. public static void Main(string[] args)
  11. {
  12. var dictOfStudents = new SortedDictionary<string, Student>();
  13.  
  14. var inputNameAndDate = Console.ReadLine();
  15. while (inputNameAndDate != "end of dates")
  16. {
  17. var inputTokens = inputNameAndDate
  18. .Split(' ', ',')
  19. .ToList();
  20. var userName = inputTokens[0];
  21. if (!dictOfStudents.ContainsKey(userName))
  22. {
  23. dictOfStudents.Add(userName, new Student());
  24. dictOfStudents[userName].StudentName = userName;
  25. dictOfStudents[userName].AttendingDates = new List<DateTime>();
  26. }
  27. //var currentStudent = new Student();
  28. //currentStudent.StudentName = userName;
  29. //currentStudent.AttendingDates = new List<DateTime>();
  30.  
  31. int numberOfDates = inputTokens.Count();
  32. for (int index = 1; index < numberOfDates; index++)
  33. {
  34. dictOfStudents[userName].AttendingDates.Add(DateTime.ParseExact(inputTokens[index], "dd/MM/yyyy", CultureInfo.InvariantCulture));
  35. }
  36.  
  37. //dictOfStudents[userName] = currentStudent;
  38.  
  39. inputNameAndDate = Console.ReadLine();
  40. }
  41.  
  42. // comment section
  43. string comment = Console.ReadLine();
  44. while (comment != "end of comments")
  45. {
  46. var commentTokens = comment.Split('-').ToArray();
  47. string userName = commentTokens[0];
  48.  
  49. bool dictContainsUserName = dictOfStudents.ContainsKey(userName);
  50. if (dictContainsUserName == false)
  51. {
  52. comment = Console.ReadLine();
  53. continue;
  54. }
  55. else // it does exist
  56. {
  57. var currentStudent = dictOfStudents[userName];
  58.  
  59. bool alreadyHaveComments = currentStudent.Comment != null;
  60. if (alreadyHaveComments == true)
  61. {
  62. currentStudent.Comment.Add(commentTokens[1]);
  63. }
  64. else
  65. {
  66. currentStudent.Comment = new List<string>();
  67. currentStudent.Comment.Add(commentTokens[1]);
  68. }
  69. }
  70.  
  71. comment = Console.ReadLine();
  72. }
  73.  
  74. // sorting Dates and printing
  75. foreach (var student in dictOfStudents.Keys)
  76. {
  77. var currentStudent = dictOfStudents[student];
  78.  
  79. Console.WriteLine(currentStudent.StudentName);
  80. Console.WriteLine("Comments:");
  81. if (currentStudent.Comment != null)
  82. {
  83. foreach (var currentComment in currentStudent.Comment)
  84. {
  85. Console.WriteLine($"- {currentComment}");
  86. }
  87. }
  88.  
  89. Console.WriteLine("Dates attended:");
  90. foreach (var date in currentStudent.AttendingDates.OrderBy(x => x))
  91. {
  92. Console.WriteLine($"-- {date:dd/MM/yyyy}");
  93. }
  94. }
  95. }
  96. }
  97.  
  98. public class Student
  99. {
  100. public string StudentName
  101. {
  102. get; set;
  103. }
  104.  
  105. public List<DateTime> AttendingDates
  106. {
  107. get; set;
  108. }
  109.  
  110. public List<string> Comment
  111. {
  112. get; set;
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement