hi6tnikyt87

Untitled

Jan 25th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. namespace _5.FilterByAge
  2. {
  3. class Student
  4. {
  5. public Student(string name, int age)
  6. {
  7. Name = name;
  8. Age = age;
  9. }
  10. public string Name { get; set; }
  11. public int Age { get; set; }
  12.  
  13. internal class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. int n = int.Parse(Console.ReadLine());
  18. List<Student> students = new List<Student>();
  19.  
  20. for (int i = 0; i < n; i++)
  21. {
  22. string[] input = Console.ReadLine().Split(", ");
  23. string name = input[0];
  24. int age = int.Parse(input[1]);
  25.  
  26. Student student = new Student(name, age);
  27. students.Add(student);
  28.  
  29. }
  30. int age2 = int.Parse(Console.ReadLine());
  31. string filterType = Console.ReadLine();
  32.  
  33. Func<Student, bool> filter = GetFilter(filterType, age2);
  34. students = students.Where(filter).ToList();
  35.  
  36. Func<Student, string> formatter = GetFormatter(Console.ReadLine());
  37. foreach (Student student in students)
  38. {
  39. Console.WriteLine(formatter(student));
  40. }
  41.  
  42. Func<Student, string> GetFormatter(string type)
  43. {
  44. switch (type)
  45. {
  46. case "name":
  47. return s => s.Name;
  48. case "age":
  49. return s => s.Age.ToString();
  50. case "name age":
  51. return student => $"{student.Name} {student.Age}";
  52. default:
  53. return null;
  54. }
  55. }
  56.  
  57. Func<Student, bool> GetFilter(string type, int age)
  58. {
  59. if (type == "older")
  60. {
  61. return s => s.Age > age;
  62. }
  63. else
  64. {
  65. return s => s.Age < age;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment