Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** A text file phones.txt holds information about people, their town and phone number:
- 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:
- find(name) – display all matching records by given name (first, middle, last or nickname)
- find(name, town) – display all matching records by given name and town
- */
- public class Person
- {
- public string Name { get; set; }
- public string Town { get; set; }
- public string TelNumber { get; set; }
- public Person(string name, string town, string telNumber)
- {
- this.Name = name;
- this.Town = town;
- this.TelNumber = telNumber;
- }
- public override string ToString()
- {
- StringBuilder result = new StringBuilder();
- result.Append(this.Name).Append(" | ");
- result.Append(this.Town).Append(" | ");
- result.Append(this.TelNumber);
- return result.ToString();
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Wintellect.PowerCollections;
- class PeopleFinder
- {
- public static string[] GetInput()
- {
- List<string> input = new List<string>();
- string line = Console.ReadLine();
- while (line != string.Empty)
- {
- input.Add(line);
- line = Console.ReadLine();
- }
- return input.ToArray();
- }
- static void Main(string[] args)
- {
- MultiDictionary<string, Person> PeopleNames = new MultiDictionary<string, Person>(true);
- MultiDictionary<string, Person> TownNames = new MultiDictionary<string, Person>(true);
- string[] input = GetInput();
- for (int i = 0; i < input.Length; i++)
- {
- string[] elements = input[i].Split('|');
- Person newPerson = new Person(elements[0].Trim(), elements[1].Trim(), elements[2].Trim());
- PeopleNames.Add(newPerson.Name, newPerson);
- TownNames.Add(newPerson.Town, newPerson);
- }
- string searchedName = GetSearchedName();
- HashSet<Person> peopleByName = GetPeopleByName(searchedName, PeopleNames);
- string searchedTown = GetSearchedTown();
- HashSet<Person> peopleByTown = GetPeopleByTown(searchedTown, TownNames);
- var result = peopleByName.Intersect(peopleByTown).ToList<Person>();
- foreach (var item in result)
- {
- Console.WriteLine(item);
- }
- //PrintResult(result);
- }
- private static void PrintResult(IEnumerable<Person> result)
- {
- foreach (var item in result)
- {
- Console.WriteLine(item);
- }
- }
- private static HashSet<Person> GetPeopleByTown(string searchedTown, MultiDictionary<string, Person> PeopleNames)
- {
- var searchedPeople = PeopleNames[searchedTown];
- HashSet<Person> foundPeople = new HashSet<Person>();
- foreach (var item in searchedPeople)
- {
- foundPeople.Add(item);
- }
- return foundPeople;
- }
- private static string GetSearchedTown()
- {
- return "Varna";
- }
- private static HashSet<Person> GetPeopleByName(string searchedName, MultiDictionary<string, Person> PeopleNames)
- {
- var searchedPeople = PeopleNames[searchedName];
- HashSet<Person> foundPeople = new HashSet<Person>();
- foreach (var item in searchedPeople)
- {
- foundPeople.Add(item);
- }
- return foundPeople;
- }
- private static string GetSearchedName()
- {
- return "Kireto";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment