Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. namespace ConsoleApplication1
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8.  
  9. public class Person
  10. {
  11. public Person(string name, string birthDay)
  12. {
  13. this.Name = name;
  14. this.Birthday = birthDay;
  15. this.Parents = new List<Person>();
  16. this.Children = new List<Person>();
  17. }
  18.  
  19. public string Name { get; }
  20.  
  21. public string Birthday { get; }
  22.  
  23. public List<Person> Parents { get; }
  24.  
  25. public List<Person> Children { get; }
  26.  
  27. public override string ToString()
  28. {
  29. return $"{this.Name} {this.Birthday}";
  30. }
  31. }
  32.  
  33. public class Startup
  34. {
  35. public static void Main()
  36. {
  37. // Store ppl with full information
  38. var people = new List<Person>();
  39.  
  40. // Store string for ppl with partial information for later use
  41. var storePeople = new List<string>();
  42.  
  43. var person = Console.ReadLine();
  44.  
  45. var inputLine = Console.ReadLine();
  46.  
  47. while (!inputLine.Equals("End"))
  48. {
  49. var info = Regex.Split(inputLine, " - ");
  50.  
  51. if (info.Length == 1) // Full person information given
  52. {
  53. var lastIndexOfSpace = inputLine.LastIndexOf(" ", StringComparison.Ordinal);
  54.  
  55. var name = inputLine.Substring(0, lastIndexOfSpace);
  56. var birthday = inputLine.Substring(lastIndexOfSpace + 1);
  57.  
  58. var newPerson = new Person(name, birthday);
  59.  
  60. people.Add(newPerson);
  61. }
  62. else
  63. {
  64. storePeople.Add(inputLine);
  65. }
  66.  
  67. inputLine = Console.ReadLine();
  68. }
  69.  
  70. // We will see what we have in storePeople for our parent/children relations
  71. foreach (var storePerson in storePeople)
  72. {
  73. Person parent;
  74. Person children;
  75.  
  76. var info = Regex.Split(storePerson, " - ");
  77.  
  78. if (info[0].Contains('/') && info[1].Contains('/')) // Both inputs are dates of birthday
  79. {
  80. var parentBirhtday = info[0];
  81. var childrenBirthday = info[1];
  82.  
  83. parent = people
  84. .First(p => p.Birthday.Equals(parentBirhtday)); // We searcg for the current person in our list
  85. children = people
  86. .First(p => p.Birthday.Equals(childrenBirthday)); // We searcg for the current person in our list
  87. }
  88. else if (info[0].Contains('/') || info[1].Contains('/')) // One of the inputs is date
  89. {
  90. var name = string.Empty;
  91. var birthday = string.Empty;
  92.  
  93. if (info[0].Contains('/')) // First is date
  94. {
  95. birthday = info[0];
  96. name = info[1];
  97.  
  98. parent = people
  99. .First(p => p.Birthday.Equals(birthday));
  100. children = people
  101. .First(p => p.Name.Equals(name));
  102. }
  103. else // Second is date
  104. {
  105. birthday = info[1];
  106. name = info[0];
  107.  
  108. children = people
  109. .First(p => p.Birthday.Equals(birthday));
  110. parent = people
  111. .First(p => p.Name.Equals(name));
  112. }
  113. }
  114. else // Both are names
  115. {
  116. var parentName = info[0];
  117. var childrenName = info[1];
  118.  
  119. parent = people
  120. .First(p => p.Name.Equals(parentName));
  121. children = people
  122. .First(p => p.Name.Equals(childrenName));
  123. }
  124.  
  125. if (!parent.Children.Contains(children))
  126. {
  127. parent.Children.Add(children);
  128. }
  129.  
  130. if (!children.Parents.Contains(parent))
  131. {
  132. children.Parents.Add(parent);
  133. }
  134. }
  135.  
  136. Person ourPerson;
  137.  
  138. // Chech if the info for our person is Name or Date
  139. if (person.Contains('/'))
  140. {
  141. ourPerson = people.First(p => p.Birthday.Equals(person));
  142. }
  143. else
  144. {
  145. ourPerson = people.First(p => p.Name.Equals(person));
  146. }
  147.  
  148. var result = new StringBuilder();
  149.  
  150. result.AppendLine(ourPerson.ToString());
  151.  
  152. result.AppendLine("Parents:");
  153. foreach (var parent in ourPerson.Parents)
  154. {
  155. result.AppendLine(parent.ToString());
  156. }
  157.  
  158. result.AppendLine("Children:");
  159. foreach (var child in ourPerson.Children)
  160. {
  161. result.AppendLine(child.ToString());
  162. }
  163.  
  164. Console.Write(result);
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement