SHARE
TWEET

Untitled

a guest May 30th, 2017 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class PersonParents
  7. {
  8.     private string name;
  9.     private string bornDate;
  10.  
  11.     public PersonParents()
  12.     {
  13.         this.name = string.Empty;
  14.         this.BornDate = string.Empty;
  15.     }
  16.  
  17.     public string Name
  18.     {
  19.         get { return name; }
  20.         set { name = value; }
  21.     }
  22.  
  23.     public string BornDate
  24.     {
  25.         get { return bornDate; }
  26.         set { bornDate = value; }
  27.     }
  28. }
  29.  
  30. public class PersonChildren
  31. {
  32.     private string name;
  33.     private string bornDate;
  34.  
  35.     public PersonChildren()
  36.     {
  37.         this.name = string.Empty;
  38.         this.bornDate = string.Empty;
  39.     }
  40.  
  41.     public string BornDate
  42.     {
  43.         get { return bornDate; }
  44.         set { bornDate = value; }
  45.     }
  46.  
  47.     public string Name
  48.     {
  49.         get { return name; }
  50.         set { name = value; }
  51.     }
  52. }
  53.  
  54. public class Person
  55. {
  56.     private string name;
  57.     private string bornDate;
  58.     private List<PersonChildren> children;
  59.     private List<PersonParents> parents;
  60.  
  61.     public Person()
  62.     {
  63.         this.Children = new List<PersonChildren>();
  64.         this.Parents = new List<PersonParents>();
  65.     }
  66.  
  67.     public string Name
  68.     {
  69.         get { return name; }
  70.         set { name = value; }
  71.     }
  72.  
  73.     public string BornDate
  74.     {
  75.         get { return bornDate; }
  76.         set { bornDate = value; }
  77.     }
  78.  
  79.     public List<PersonChildren> Children
  80.     {
  81.         get { return children; }
  82.         set { children = value; }
  83.     }
  84.  
  85.     public List<PersonParents> Parents
  86.     {
  87.         get { return parents; }
  88.         set { parents = value; }
  89.     }
  90. }
  91.  
  92.  
  93. public class FamilyTree
  94. {
  95.     private static Person person = new Person();
  96.  
  97.     public static void Main()
  98.     {
  99.         var searchedPerson = Console.ReadLine();
  100.         var input = Console.ReadLine();
  101.         var nameNameData = new Dictionary<string, string>();
  102.         var nameDateData = new Dictionary<string, string>();
  103.         var dateNameData = new Dictionary<string, string>();
  104.         var dateDateData = new Dictionary<string, string>();
  105.         var peopleWithDates = new Dictionary<string, string>();
  106.         var savedInput = new List<string>();
  107.  
  108.         // Checking what kind of input we have and putting it in the correct Dictionary for him
  109.         while (!input.Equals("End"))
  110.         {
  111.             if (input.Contains("-"))
  112.             {
  113.                 savedInput.Add(input);
  114.                 var relatives = input.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries).Select(a => a.Trim()).ToArray();
  115.  
  116.                 if (!relatives[0].Contains("/"))
  117.                 {
  118.                     if (!relatives[1].Contains("/"))
  119.                     {
  120.                         nameNameData[relatives[0]] = relatives[1];
  121.                     }
  122.                     else
  123.                     {
  124.                         nameDateData[relatives[0]] = relatives[1];
  125.                     }
  126.                 }
  127.                 else
  128.                 {
  129.                     if (!relatives[1].Contains("/"))
  130.                     {
  131.                         dateNameData[relatives[0]] = relatives[1];
  132.                     }
  133.                     else if (relatives[1].Contains("/"))
  134.                     {
  135.                         dateDateData[relatives[0]] = relatives[1];
  136.                     }
  137.                 }
  138.             }
  139.             else
  140.             {
  141.                 var personNameDate = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(a => a.Trim()).ToArray();
  142.                 peopleWithDates[$"{personNameDate[0]} {personNameDate[1]}"] = personNameDate[2];
  143.             }
  144.             input = Console.ReadLine();
  145.         }
  146.  
  147.         LocateNameDateForSearchedPerson(searchedPerson, peopleWithDates);
  148.  
  149.         FindPersonChildren(nameNameData, nameDateData, dateNameData, dateDateData, peopleWithDates);
  150.  
  151.         FindPersonParrents(nameNameData, nameDateData, dateNameData, dateDateData, peopleWithDates);
  152.         Console.WriteLine();
  153.  
  154.         PrintSearchedResult(person, savedInput);
  155.     }
  156.  
  157.  
  158.     private static void PrintSearchedResult(Person person, List<string> savedInput)
  159.     {
  160.         Console.WriteLine($"{person.Name} {person.BornDate}");
  161.         Console.WriteLine("Parents:");
  162.         var parents = new StringBuilder();
  163.         var children = new StringBuilder();
  164.  
  165.         // Go through every input line, in the order they were entered, and find if the String is actually a child or parent of the ones we already
  166.         // found and know for the person we are looking for.
  167.         for (int i = 0; i < savedInput.Count; i++)
  168.         {
  169.             foreach (var parent in person.Parents)
  170.             {
  171.                 if (savedInput[i].Contains(parent.Name) || savedInput[i].Contains(parent.BornDate))
  172.                 {
  173.                     // additional Check, because it's writing duplicates
  174.                     if (!parents.ToString().Contains($"{parent.Name} {parent.BornDate}"))
  175.                     {
  176.                         parents.AppendLine($"{parent.Name} {parent.BornDate}");
  177.                     }
  178.                 }
  179.             }
  180.             foreach (var child in person.Children)
  181.             {
  182.                 if (savedInput[i].Contains(child.Name) || savedInput[i].Contains(child.BornDate))
  183.                 {
  184.                     // additional Check, because it's writing duplicates
  185.                     if (!children.ToString().Contains($"{child.Name} {child.BornDate}"))
  186.                     {
  187.                         children.AppendLine($"{child.Name} {child.BornDate}");
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.  
  193.         Console.Write(parents);
  194.         Console.WriteLine("Children:");
  195.         Console.WriteLine(children);
  196.     }
  197.  
  198.     // Iterate through all the Dictionaries and find input which is the same as Searched person Name or Date (For Parents)
  199.     private static void FindPersonParrents(Dictionary<string, string> nameNameData, Dictionary<string, string> nameDateData,
  200.         Dictionary<string, string> dateNameData, Dictionary<string, string> dateDateData, Dictionary<string, string> peopleWithDates)
  201.     {
  202.         var parentsFromNameNameData = nameNameData.Where(p => p.Value.Equals(person.Name));
  203.         var parentsFromNameDateData = nameDateData.Where(p => p.Value.Equals(person.BornDate));
  204.         var parentsFromDateNameData = dateNameData.Where(d => d.Value.Equals(person.Name));
  205.         var parentsFromDateDateData = dateDateData.Where(d => d.Value.Equals(person.BornDate));
  206.  
  207.         if (parentsFromNameNameData.Count() > 0)
  208.         {
  209.             foreach (var par in parentsFromNameNameData)
  210.             {
  211.                 var parent = new PersonParents();
  212.                 parent.Name = par.Key;
  213.                 var parentDate = peopleWithDates.FirstOrDefault(p => p.Key.Equals(par.Key)).Value;
  214.                 parent.BornDate = parentDate;
  215.                 person.Parents.Add(parent);
  216.             }
  217.         }
  218.         if (parentsFromNameDateData.Count() > 0)
  219.         {
  220.             foreach (var par in parentsFromNameDateData)
  221.             {
  222.                 var parent = new PersonParents();
  223.                 parent.Name = par.Key;
  224.                 var parentDate = peopleWithDates.FirstOrDefault(p => p.Key.Equals(par.Key)).Value;
  225.                 parent.BornDate = parentDate;
  226.                 person.Parents.Add(parent);
  227.             }
  228.         }
  229.         if (parentsFromDateNameData.Count() > 0)
  230.         {
  231.             foreach (var par in parentsFromDateNameData)
  232.             {
  233.                 var parent = new PersonParents();
  234.                 parent.BornDate = par.Key;
  235.                 var parentName = peopleWithDates.FirstOrDefault(p => p.Value.Equals(par.Key)).Key;
  236.                 parent.Name = parentName;
  237.                 person.Parents.Add(parent);
  238.             }
  239.         }
  240.         if (parentsFromDateDateData.Count() > 0)
  241.         {
  242.             foreach (var par in parentsFromDateDateData)
  243.             {
  244.                 var parent = new PersonParents();
  245.                 parent.BornDate = par.Key;
  246.                 var parentName = peopleWithDates.FirstOrDefault(p => p.Value.Equals(par.Key)).Key;
  247.                 parent.Name = parentName;
  248.                 person.Parents.Add(parent);
  249.             }
  250.         }
  251.     }
  252.  
  253.     // Iterate through all the Dictionaries and find input which is the same as Searched person Name or Date (For Children)
  254.     private static void FindPersonChildren(Dictionary<string, string> nameNameData, Dictionary<string, string> nameDateData,
  255.         Dictionary<string, string> dateNameData, Dictionary<string, string> dateDateData, Dictionary<string, string> peopleWithDates)
  256.     {
  257.         var childrenFromNameNameData = nameNameData.Where(p => p.Key.Equals(person.Name));
  258.         var childrenFromNameDateData = nameDateData.Where(p => p.Key.Equals(person.Name));
  259.         var childrenFromDateNameData = dateNameData.Where(d => d.Key.Equals(person.BornDate));
  260.         var childrenFromDateDateData = dateDateData.Where(d => d.Key.Equals(person.BornDate));
  261.  
  262.         if (childrenFromNameNameData.Count() > 0)
  263.         {
  264.             foreach (var child in childrenFromNameNameData)
  265.             {
  266.                 var children = new PersonChildren();
  267.                 children.Name = child.Value;
  268.                 var childDate = peopleWithDates.FirstOrDefault(n => n.Key.Equals(child.Value)).Value;
  269.                 children.BornDate = childDate;
  270.                 person.Children.Add(children);
  271.             }
  272.         }
  273.         if (childrenFromNameDateData.Count() > 0)
  274.         {
  275.             foreach (var child in childrenFromNameDateData)
  276.             {
  277.                 var children = new PersonChildren();
  278.                 children.BornDate = child.Value;
  279.                 var childName = peopleWithDates.FirstOrDefault(n => n.Value.Equals(child.Value)).Key;
  280.                 children.Name = childName;
  281.                 person.Children.Add(children);
  282.             }
  283.         }
  284.         if (childrenFromDateNameData.Count() > 0)
  285.         {
  286.             foreach (var child in childrenFromDateNameData)
  287.             {
  288.                 var children = new PersonChildren();
  289.                 children.Name = child.Value;
  290.                 var childDate = peopleWithDates.FirstOrDefault(n => n.Key.Equals(child.Value)).Value;
  291.                 children.BornDate = childDate;
  292.                 person.Children.Add(children);
  293.             }
  294.         }
  295.         if (childrenFromDateDateData.Count() > 0)
  296.         {
  297.             foreach (var child in childrenFromDateDateData)
  298.             {
  299.                 var children = new PersonChildren();
  300.                 children.BornDate = child.Value;
  301.                 var childName = peopleWithDates.FirstOrDefault(n => n.Value.Equals(child.Value)).Key;
  302.                 children.Name = childName;
  303.                 person.Children.Add(children);
  304.             }
  305.         }
  306.     }
  307.  
  308.     // Find the Date or the Name for the Searched person, depending on what input it was given for him
  309.     private static void LocateNameDateForSearchedPerson(string searchedPerson, Dictionary<string, string> peopleWithDates)
  310.     {
  311.         if (searchedPerson.Contains("/"))
  312.         {
  313.             person.BornDate = searchedPerson;
  314.             var personName = peopleWithDates.SingleOrDefault(d => d.Value.Equals(searchedPerson)).Key;
  315.             person.Name = personName;
  316.         }
  317.         else
  318.         {
  319.             person.Name = searchedPerson;
  320.             var personDate = peopleWithDates.SingleOrDefault(n => n.Key.Equals(searchedPerson)).Value;
  321.             person.BornDate = personDate;
  322.         }
  323.     }
  324. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top