Advertisement
simonradev

PartyReservationFilterModule

Jun 13th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. namespace _11.Party_Reservation_Filter_Module
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.    
  9.     public class Program
  10.     {
  11.         private static Dictionary<string, Func<string, string, bool>> allCriterias;
  12.         private static List<KeyValuePair<string, string>> allFilters;
  13.  
  14.         public static void Main()
  15.         {
  16.             string[] allGuests = SplitStringToArray(Console.ReadLine(), ' ');
  17.            
  18.             allFilters = new List<KeyValuePair<string, string>>();
  19.  
  20.             Dictionary<string, Action<KeyValuePair<string, string>>> filterManager = new Dictionary<string, Action<KeyValuePair<string, string>>>
  21.             {
  22.                 ["Add filter"] = kvp => allFilters.Add(kvp),
  23.                 ["Remove filter"] = kvp => allFilters.Remove(kvp),
  24.             };
  25.            
  26.             string inputLine;
  27.             while ((inputLine = Console.ReadLine()) != "Print")
  28.             {
  29.                 string[] commandArgs = SplitStringToArray(inputLine, ';');
  30.  
  31.                 string filterManageInfo = commandArgs[0];
  32.                 string criteria = commandArgs[1];
  33.                 string toMatch = commandArgs[2];
  34.  
  35.                 filterManager[filterManageInfo](new KeyValuePair<string, string>(criteria, toMatch));
  36.             }
  37.            
  38.             allCriterias = new Dictionary<string, Func<string, string, bool>>
  39.             {
  40.                 ["Starts with"] = (n, s) => n.StartsWith(s),
  41.                 ["Ends with"] = (n, s) => n.EndsWith(s),
  42.                 ["Length"] = (n, s) => n.Length == int.Parse(s),
  43.                 ["Contains"] = (n, s) => n.Contains(s)
  44.             };
  45.  
  46.             Console.WriteLine(string.Join(" ", allGuests.Where(g => !GuestIsFiltered(g))));
  47.         }
  48.  
  49.         private static bool GuestIsFiltered(string guest)
  50.         {
  51.             bool isFiltered = false;
  52.  
  53.             foreach (KeyValuePair<string, string> kvp in allFilters)
  54.             {
  55.                 if (allCriterias[kvp.Key](guest, kvp.Value))
  56.                 {
  57.                     isFiltered = true;
  58.                     break;
  59.                 }
  60.             }
  61.  
  62.             return isFiltered;
  63.         }
  64.  
  65.         private static string[] SplitStringToArray(string toSplit, char toSplitBy)
  66.         {
  67.             return toSplit.Split(new[] { toSplitBy }, StringSplitOptions.RemoveEmptyEntries);
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement