Advertisement
yanchevilian

Filter By Age Vol. 2 (with Class Person) / C# Advanced

Sep 6th, 2021
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security;
  5.  
  6. namespace _5._Filter_by_Age
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Func<Person, int, bool> youngerFunc = (person, givenAge) => person.Age < givenAge;
  13.             Func<Person, int, bool> olderFunc = (person, givenAge) => person.Age >= givenAge;
  14.  
  15.             int n = int.Parse(Console.ReadLine());
  16.             List<Person> allPersons = new List<Person>();
  17.  
  18.             for (int i = 0; i < n; i++)
  19.             {
  20.                 string[] cmdArr = Console.ReadLine().Split(", ", StringSplitOptions.RemoveEmptyEntries);
  21.                 string name = cmdArr[0];
  22.                 int age = int.Parse(cmdArr[1]);
  23.  
  24.                 allPersons.Add(new Person(name, age));
  25.             }
  26.  
  27.             string condition = Console.ReadLine();
  28.             int ageFilter = int.Parse(Console.ReadLine());
  29.             string[] printFilter = Console.ReadLine()
  30.                                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  31.  
  32.             switch (condition)
  33.             {
  34.                 case "younger":
  35.                     allPersons = allPersons.Where(p => youngerFunc(p, ageFilter)).ToList();
  36.                     break;
  37.                 case "older":
  38.                     allPersons = allPersons.Where(p => olderFunc(p, ageFilter)).ToList();
  39.                     break;
  40.             }
  41.  
  42.             foreach (var person in allPersons)
  43.             {
  44.                 List<string> output = new List<string>();
  45.                 if (printFilter.Contains("name"))
  46.                 {
  47.                     output.Add(person.Name);
  48.                 }
  49.  
  50.                 if (printFilter.Contains("age"))
  51.                 {
  52.                     output.Add(person.Age.ToString());
  53.                 }
  54.  
  55.                 Console.WriteLine(string.Join(" - ", output));
  56.             }
  57.         }
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement