Advertisement
viraco4a

Untitled

Jan 8th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Globalization;
  7.  
  8. namespace task_23
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. List<Student> MyListOfStudents = new List<Student>();
  15. for (int i = 0; ; i++)
  16. {
  17. Input inputLine = ReadInputLine();
  18. if (inputLine.CheckKeyWord)
  19. {
  20. break;
  21. }
  22.  
  23. bool newUniqueStudent = true;
  24. Student NewStudent = GetNewStudentAndAddDate(inputLine.input);
  25. Student upgradedStudent = new Student();
  26. foreach (Student item in MyListOfStudents)
  27. {
  28. if (item.name == NewStudent.name)
  29. {
  30. upgradedStudent = AddAdditionalDatesToExistingStudent(item, NewStudent);
  31. newUniqueStudent = false;
  32. }
  33. }
  34. if (newUniqueStudent)
  35. {
  36. MyListOfStudents.Add(NewStudent);
  37. }
  38. else
  39. {
  40. for (int j = 0; j < MyListOfStudents.Count; j++)
  41. {
  42. if (MyListOfStudents.ElementAt(j).name == NewStudent.name)
  43. {
  44. MyListOfStudents.RemoveAt(j);
  45. MyListOfStudents.Add(upgradedStudent);
  46. }
  47. }
  48. }
  49. }
  50.  
  51. for (int i = 0; ; i++)
  52. {
  53. Input inputLine = ReadInputLine();
  54. if (inputLine.CheckKeyWord)
  55. {
  56. MyListOfStudents = OrderListOfStudents(MyListOfStudents);
  57. PrintTheResult(MyListOfStudents);
  58. break;
  59. }
  60.  
  61. bool newUniqueStudentComment = false;
  62. Student NewStudent = GetNewStudentAndAddComment(MyListOfStudents, inputLine.input);
  63. if (NewStudent.ListOfComments != null)
  64. {
  65. Student upgradedStudent = new Student();
  66. foreach (Student item in MyListOfStudents)
  67. {
  68. if (item.name == NewStudent.name)
  69. {
  70. upgradedStudent = AddAdditionalCommentsToExistingStudent(item, NewStudent);
  71. newUniqueStudentComment = true;
  72. }
  73. }
  74. if (newUniqueStudentComment)
  75. {
  76. for (int j = 0; j < MyListOfStudents.Count; j++)
  77. {
  78. if (MyListOfStudents.ElementAt(j).name == NewStudent.name)
  79. {
  80. MyListOfStudents.RemoveAt(j);
  81. break;
  82. }
  83. }
  84. MyListOfStudents.Add(upgradedStudent);
  85. }
  86. }
  87. }
  88. }
  89.  
  90. static void PrintTheResult(List<Student> ListToPrint)
  91. {
  92. foreach (Student student in ListToPrint)
  93. {
  94. Console.WriteLine(student.name);
  95. Console.WriteLine("Comments:");
  96. if (student.ListOfComments != null)
  97. {
  98. foreach (string item in student.ListOfComments)
  99. {
  100. Console.WriteLine($"- {item}");
  101. }
  102. }
  103. Console.WriteLine("Dates attended:");
  104. Student orderedDatesInStudent = new Student();
  105. orderedDatesInStudent = SortDatesForStudent(student);
  106. foreach (DateTime item in orderedDatesInStudent.ListOfAttendanceDates)
  107. {
  108. Console.WriteLine($"-- {item.ToString("dd/MM/yyyy")}");
  109. }
  110. }
  111. }
  112.  
  113. static List<Student> OrderListOfStudents(List<Student> ListToOrder)
  114. {
  115. List<Student> OrderedList = new List<Student>();
  116. OrderedList = ListToOrder.OrderBy(o => o.name).ToList();
  117. return OrderedList;
  118. }
  119.  
  120. static Student AddAdditionalCommentsToExistingStudent(Student StudentWithDates, Student StudentWithComments)
  121. {
  122. Student upgradedStudent = new Student();
  123. upgradedStudent.name = StudentWithDates.name;
  124. upgradedStudent.ListOfAttendanceDates = StudentWithDates.ListOfAttendanceDates;
  125. upgradedStudent.ListOfComments = UpgradeListOfComments(StudentWithDates.ListOfComments, StudentWithComments.ListOfComments);
  126.  
  127. return upgradedStudent;
  128. }
  129.  
  130. static List<string> UpgradeListOfComments(List<string> oldList, List<string> newList)
  131. {
  132. List<string> UpgradedList = new List<string>();
  133. if (oldList != null)
  134. {
  135. UpgradedList = oldList;
  136. }
  137. for (int i = 0; i < newList.Count; i++)
  138. {
  139. UpgradedList.Add(newList.ElementAt(i));
  140. }
  141. return UpgradedList;
  142. }
  143.  
  144. static Student GetNewStudentAndAddComment(List<Student> ListToCheck, string input)
  145. {
  146. List<string> inputLine = input.Split('-').ToList();
  147. Student NewStudent = new Student();
  148. for (int i = 0; i < ListToCheck.Count; i++)
  149. {
  150. if (ListToCheck.ElementAt(i).name == inputLine[0])
  151. {
  152. NewStudent.name = inputLine[0];
  153. List<string> CommentToAdd = new List<string>();
  154. for (int j = 1; j < inputLine.Count; j++)
  155. {
  156. if (!CommentToAdd.Contains(inputLine[j]))
  157. {
  158. CommentToAdd.Add(inputLine[j]);
  159. }
  160. }
  161. NewStudent.ListOfComments = CommentToAdd;
  162. return NewStudent;
  163. }
  164. }
  165. return NewStudent;
  166. }
  167.  
  168. static Student SortDatesForStudent(Student studentToSortDate)
  169. {
  170. Student sortedStudent = new Student();
  171. sortedStudent.name = studentToSortDate.name;
  172. sortedStudent.ListOfAttendanceDates = studentToSortDate.ListOfAttendanceDates.OrderBy(o => o).ToList();
  173. return sortedStudent;
  174. }
  175.  
  176. static Student AddAdditionalDatesToExistingStudent(Student existingStudent, Student newStudent)
  177. {
  178. Student upgradedStudent = new Student();
  179. upgradedStudent.name = existingStudent.name;
  180. upgradedStudent.ListOfAttendanceDates = existingStudent.ListOfAttendanceDates.Union(newStudent.ListOfAttendanceDates).ToList();
  181.  
  182. return upgradedStudent;
  183. }
  184.  
  185. static Student GetNewStudentAndAddDate(string input)
  186. {
  187. string[] inputLine = input.Split(' ', ',').ToArray();
  188. Student NewStudent = new Student();
  189. NewStudent.name = inputLine[0];
  190. List<DateTime> datesToAdd = new List<DateTime>();
  191. for (int i = 1; i < inputLine.Length; i++)
  192. {
  193. datesToAdd.Add(DateTime.ParseExact(inputLine[i], "dd/MM/yyyy", CultureInfo.InvariantCulture));
  194. }
  195. NewStudent.ListOfAttendanceDates = datesToAdd;
  196. return NewStudent;
  197. }
  198.  
  199. static Input ReadInputLine()
  200. {
  201. Input inputLogic = new Input();
  202. string input = Console.ReadLine();
  203. bool WordForBreak = false;
  204. if (input == "end of dates" || input == "end of comments")
  205. {
  206. WordForBreak = true;
  207. }
  208. inputLogic.input = input;
  209. inputLogic.CheckKeyWord = WordForBreak;
  210. return inputLogic;
  211. }
  212.  
  213. }
  214.  
  215. public class Student
  216. {
  217. public string name { get; set; }
  218. public List<string> ListOfComments { get; set; }
  219. public List<DateTime> ListOfAttendanceDates { get; set; }
  220.  
  221. public List<string> GetListOfComments(List<string> listOfComments)
  222. {
  223. return ListOfComments;
  224. }
  225. }
  226.  
  227. public class Input
  228. {
  229. public bool CheckKeyWord { get; set; }
  230. public string input { get; set; }
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement