Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace FilterByAge
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var n = int.Parse(Console.ReadLine());
  12. var people = new List<KeyValuePair<string, int>>();
  13. for (int i = 0; i < n; i++)
  14. {
  15. var input = Console.ReadLine()
  16. .Split(new[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries)
  17. .ToList();
  18. var name = input[0];
  19. var personAge = int.Parse(input[1]);
  20. people.Add(new KeyValuePair<string, int>(name, personAge));
  21. }
  22. var condition = Console.ReadLine();
  23. var age = int.Parse(Console.ReadLine());
  24. var format = Console.ReadLine();
  25. //Implementation of these methos on next slides
  26. Func<int, bool> tester = CreateTester(condition, age);
  27. Action<KeyValuePair<string, int>> printer =
  28. CreatePrinter(format);
  29.  
  30. PrintFilteredStudent(people, tester, printer);
  31.  
  32.  
  33. }
  34.  
  35. private static void PrintFilteredStudent
  36. (List<KeyValuePair<string, int>> people, Func<int, bool> tester, Action<KeyValuePair<string, int>> printer)
  37. {
  38. foreach (var person in people.Where(x => tester(x.Value)))
  39. {
  40. printer(person);
  41. }
  42. }
  43.  
  44. public static Func<int, bool> CreateTester
  45. (string condition, int age)
  46. {
  47. switch (condition)
  48. {
  49. case "younger": return x => x < age;
  50. case "older": return x => x >= age;
  51. default: return null;
  52. }
  53. }
  54. public static Action<KeyValuePair<string, int>>
  55. CreatePrinter(string format)
  56. {
  57. switch (format)
  58. {
  59. case "name":
  60. return person => Console.WriteLine($"{person.Key}");
  61. case "age":
  62. return person => Console.WriteLine($"{person.Value}");
  63. case "name age":
  64. return person =>
  65. Console.WriteLine($"{person.Key} - {person.Value}");
  66. default: return null;
  67. }
  68. }
  69.  
  70.  
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement