/// <summary> /// Adds a person to list. /// </summary> public void AddPerson() { Person newPerson = NewPersonCreator(); bool key = false; foreach (Person p in list) { if (p.LastName == newPerson.LastName) { key = true; } } if (key) { SameLastNamesHappenedEventArgs args = new SameLastNamesHappenedEventArgs(); args.LastName = newPerson.LastName; OnSameLastNamesHappened(args); } list.Add(newPerson); Console.WriteLine("Successfully added."); }
private void Receiver_SameLastNamesHappened(object sender, SameLastNamesHappenedEventArgs eventArgs) { string sameLastName = eventArgs.LastName; Console.WriteLine($"Similar last name '{sameLastName}' has detected. Here's list of namesake:"); List <Person> newList = new List <Person>(); foreach (Person p in ListOfPersonas.GetListOfPersonas().GetPersonas()) { if (p.LastName == sameLastName) { p.OutputPerson(); newList.Add(p); } } string path = @"D:\Workspace\TAT\DEV-8\DEV-8\listOfPersonas.json"; using (StreamWriter file = File.CreateText(path)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, newList); } }
/// <summary> /// Invokes event SameLastNamesHappened. /// </summary> /// <param name="eventArgs">Event arguments.</param> public virtual void OnSameLastNamesHappened(SameLastNamesHappenedEventArgs eventArgs) { SameLastNamesHappened?.Invoke(this, eventArgs); }