sashomaga

using Wintellect.PowerCollections

Jun 8th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. /** A text file phones.txt holds information about people, their town and phone number:
  2. Duplicates can occur in people names, towns and phone numbers. Write a program to read the phones file and execute a sequence of commands given in the file commands.txt:
  3. find(name) – display all matching records by given name (first, middle, last or nickname)
  4. find(name, town) – display all matching records by given name and town
  5. */
  6.  
  7. public class Person
  8. {
  9.     public string Name { get; set; }
  10.     public string Town { get; set; }
  11.     public string TelNumber { get; set; }
  12.  
  13.     public Person(string name, string town, string telNumber)
  14.     {
  15.         this.Name = name;
  16.         this.Town = town;
  17.         this.TelNumber = telNumber;
  18.     }
  19.  
  20.     public override string ToString()
  21.     {
  22.         StringBuilder result = new StringBuilder();
  23.         result.Append(this.Name).Append(" | ");
  24.         result.Append(this.Town).Append(" | ");
  25.         result.Append(this.TelNumber);
  26.  
  27.         return result.ToString();
  28.     }
  29. }
  30.  
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Linq;
  34. using Wintellect.PowerCollections;
  35.  
  36. class PeopleFinder
  37. {
  38.     public static string[] GetInput()
  39.     {
  40.         List<string> input = new List<string>();
  41.         string line = Console.ReadLine();
  42.         while (line != string.Empty)
  43.         {
  44.             input.Add(line);
  45.             line = Console.ReadLine();
  46.         }
  47.  
  48.         return input.ToArray();
  49.     }
  50.  
  51.     static void Main(string[] args)
  52.     {
  53.         MultiDictionary<string, Person> PeopleNames = new MultiDictionary<string, Person>(true);
  54.         MultiDictionary<string, Person> TownNames = new MultiDictionary<string, Person>(true);
  55.  
  56.  
  57.         string[] input = GetInput();
  58.         for (int i = 0; i < input.Length; i++)
  59.         {
  60.             string[] elements = input[i].Split('|');
  61.             Person newPerson = new Person(elements[0].Trim(), elements[1].Trim(), elements[2].Trim());
  62.             PeopleNames.Add(newPerson.Name, newPerson);
  63.             TownNames.Add(newPerson.Town, newPerson);
  64.         }
  65.  
  66.         string searchedName = GetSearchedName();
  67.         HashSet<Person> peopleByName = GetPeopleByName(searchedName, PeopleNames);
  68.  
  69.         string searchedTown = GetSearchedTown();
  70.         HashSet<Person> peopleByTown = GetPeopleByTown(searchedTown, TownNames);
  71.  
  72.         var result = peopleByName.Intersect(peopleByTown).ToList<Person>();
  73.         foreach (var item in result)
  74.         {
  75.             Console.WriteLine(item);
  76.         }
  77.         //PrintResult(result);
  78.     }
  79.  
  80.     private static void PrintResult(IEnumerable<Person> result)
  81.     {
  82.         foreach (var item in result)
  83.         {
  84.             Console.WriteLine(item);
  85.         }
  86.     }
  87.  
  88.     private static HashSet<Person> GetPeopleByTown(string searchedTown, MultiDictionary<string, Person> PeopleNames)
  89.     {
  90.         var searchedPeople = PeopleNames[searchedTown];
  91.         HashSet<Person> foundPeople = new HashSet<Person>();
  92.         foreach (var item in searchedPeople)
  93.         {
  94.             foundPeople.Add(item);
  95.         }
  96.  
  97.         return foundPeople;
  98.     }
  99.  
  100.     private static string GetSearchedTown()
  101.     {
  102.         return "Varna";
  103.     }
  104.  
  105.     private static HashSet<Person> GetPeopleByName(string searchedName, MultiDictionary<string, Person> PeopleNames)
  106.     {
  107.         var searchedPeople = PeopleNames[searchedName];
  108.         HashSet<Person> foundPeople = new HashSet<Person>();
  109.         foreach (var item in searchedPeople)
  110.         {
  111.             foundPeople.Add(item);
  112.         }
  113.  
  114.         return foundPeople;
  115.     }
  116.  
  117.     private static string GetSearchedName()
  118.     {
  119.         return "Kireto";
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment