Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _5.FilterByAge
- {
- class Student
- {
- public Student(string name, int age)
- {
- Name = name;
- Age = age;
- }
- public string Name { get; set; }
- public int Age { get; set; }
- internal class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- List<Student> students = new List<Student>();
- for (int i = 0; i < n; i++)
- {
- string[] input = Console.ReadLine().Split(", ");
- string name = input[0];
- int age = int.Parse(input[1]);
- Student student = new Student(name, age);
- students.Add(student);
- }
- int age2 = int.Parse(Console.ReadLine());
- string filterType = Console.ReadLine();
- Func<Student, bool> filter = GetFilter(filterType, age2);
- students = students.Where(filter).ToList();
- Func<Student, string> formatter = GetFormatter(Console.ReadLine());
- foreach (Student student in students)
- {
- Console.WriteLine(formatter(student));
- }
- Func<Student, string> GetFormatter(string type)
- {
- switch (type)
- {
- case "name":
- return s => s.Name;
- case "age":
- return s => s.Age.ToString();
- case "name age":
- return student => $"{student.Name} {student.Age}";
- default:
- return null;
- }
- }
- Func<Student, bool> GetFilter(string type, int age)
- {
- if (type == "older")
- {
- return s => s.Age > age;
- }
- else
- {
- return s => s.Age < age;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment