Advertisement
social1986

Untitled

Feb 2nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. namespace _05._Filter_By_Age
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class StartUp
  8.     {
  9.  
  10.         public static void Main()
  11.         {
  12.             var n = int.Parse(Console.ReadLine());
  13.             var students = new Dictionary<string, int>();
  14.  
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 var currentStudent = Console.ReadLine().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  18.                 var name = currentStudent[0];
  19.                 var age = int.Parse(currentStudent[1]);
  20.                 if (!students.ContainsKey(name))
  21.                 {
  22.                     students[name] = age;
  23.                 }
  24.             }
  25.             var condition = Console.ReadLine();
  26.             var ageFilter = int.Parse(Console.ReadLine());
  27.             var format = Console.ReadLine();
  28.  
  29.             var filteredStudents = FilteringStudents(condition, students, ageFilter);
  30.  
  31.             PrintResult(students, format);
  32.         }
  33.  
  34.         public static Dictionary<string, int> FilteringStudents(string condition, Dictionary<string, int> students, int ageFilter)
  35.         {
  36.             switch (condition)
  37.             {
  38.                 case "older":
  39.                     return students.Where(p => p.Value >= ageFilter).ToDictionary(k => k.Key, v => v.Value);
  40.                 default:
  41.                     return students.Where(p => p.Value < ageFilter).ToDictionary(k => k.Key, v => v.Value);
  42.             }
  43.         }
  44.  
  45.         public static void FilteredStudents(string condition, Dictionary<string, int> students, int age)
  46.         {
  47.             var filteredStudents = FilteringStudents(condition, students, age);
  48.         }
  49.  
  50.         public static void PrintResult(Dictionary<string, int> filteredStudents, string format)
  51.         {
  52.             switch (format)
  53.             {
  54.                 case "name age":
  55.                     foreach (var student in filteredStudents)
  56.                     {
  57.                         Console.WriteLine($"{student.Key} - {student.Value}");
  58.                     }
  59.                     break;
  60.                 case "age":
  61.                     foreach (var student in filteredStudents)
  62.                     {
  63.                         Console.WriteLine(student.Value);
  64.                     }
  65.                     break;
  66.                 case "name":
  67.                     foreach (var student in filteredStudents)
  68.                     {
  69.                         Console.WriteLine(student.Key);
  70.                     }
  71.                     break;
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement