Advertisement
social1986

Untitled

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