Advertisement
simonradev

ThePartyResevationFilterModule

Apr 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. namespace PartyReservationFilterModule
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class PartyReservationFilterModule
  8.     {
  9.         public static void Main()
  10.         {
  11.             Person[] names = SplitString(Console.ReadLine(), ' ').Select(n => new Person(n)).ToArray();
  12.  
  13.             string inputLine = Console.ReadLine();
  14.             while (inputLine != "Print")
  15.             {
  16.                 string[] commandInfo = SplitString(inputLine, ';');
  17.  
  18.                 string filterAction = commandInfo[0];
  19.                 string personNameInfo = commandInfo[1];
  20.                 string toMatch = string.Empty;
  21.  
  22.                 Filter filter = filterAction == "Add filter" ? Filter.AddFilter : Filter.RemoveFilter;
  23.                 Func<string, string, bool> funcToUse = default(Func<string, string, bool>);
  24.  
  25.                 if (personNameInfo == "Starts with")
  26.                 {
  27.                     toMatch = commandInfo[2];
  28.  
  29.                     funcToUse = Functions.StartsWith;
  30.                 }
  31.                 else if (personNameInfo == "Ends with")
  32.                 {
  33.                     toMatch = commandInfo[2];
  34.  
  35.                     funcToUse = Functions.EndsWith;
  36.                 }
  37.                 else if (personNameInfo == "Length")
  38.                 {
  39.                     toMatch = new string(' ', int.Parse(commandInfo[2]));
  40.  
  41.                     funcToUse = Functions.HasLenght;
  42.                 }
  43.                 else if (personNameInfo == "Contains")
  44.                 {
  45.                     toMatch = commandInfo[2];
  46.  
  47.                     funcToUse = Functions.Contains;
  48.                 }
  49.  
  50.                 Functions.ManipulateFilters(names, toMatch, funcToUse, filter);
  51.  
  52.                 inputLine = Console.ReadLine();
  53.             }
  54.  
  55.             string result = Functions.GetNonFilteredGuests(names, p => p.HasFilter);
  56.             Console.WriteLine(result);
  57.         }
  58.  
  59.         public static string[] SplitString(string toSplit, char toSplitBy)
  60.         {
  61.             return toSplit.Split(new[] { toSplitBy }, StringSplitOptions.RemoveEmptyEntries);
  62.         }
  63.     }
  64.  
  65.     public static class Functions
  66.     {
  67.         public static readonly Func<Person[], Func<Person, bool>, string> GetNonFilteredGuests = (names, func) =>
  68.                                {
  69.                                    StringBuilder result = new StringBuilder();
  70.                                    foreach (Person person in names)
  71.                                    {
  72.                                        if (func(person))
  73.                                        {
  74.                                            continue;
  75.                                        }
  76.  
  77.                                        result.Append(person.Name);
  78.                                        result.Append(" ");
  79.                                    }
  80.  
  81.                                    return result.ToString();
  82.                                };
  83.  
  84.         public static readonly Action<Person[], string, Func<string, string, bool>, Filter> ManipulateFilters = (names, toMatch, func, filter) =>
  85.                                {
  86.                                    for (int currElement = 0; currElement < names.Length; currElement++)
  87.                                    {
  88.                                        Person currPerson = names[currElement];
  89.  
  90.                                        if (func(currPerson.Name, toMatch))
  91.                                        {
  92.                                            currPerson.SetFilter(filter);
  93.                                        }
  94.                                    }
  95.                                };
  96.  
  97.         public static readonly Func<string, string, bool> StartsWith = (f, s)
  98.                                                 => f.Substring(0, s.Length) == s;
  99.  
  100.         public static readonly Func<string, string, bool> EndsWith = (f, s)
  101.                                                 => f.Substring(f.Length - s.Length, s.Length) == s;
  102.  
  103.         public static readonly Func<string, string, bool> HasLenght = (f, s)
  104.                                                 => f.Length == s.Length;
  105.  
  106.         public static readonly Func<string, string, bool> Contains = (f, s)
  107.                                                 => f.Contains(s);
  108.     }
  109.  
  110.     public class Person
  111.     {
  112.         private string name;
  113.         private bool hasFilter;
  114.  
  115.         public Person(string name)
  116.         {
  117.             this.name = name;
  118.             this.hasFilter = false;
  119.         }
  120.  
  121.         public string Name
  122.         {
  123.             get { return this.name; }
  124.             set { this.name = value; }
  125.         }
  126.  
  127.         public bool HasFilter
  128.         {
  129.             get { return this.hasFilter; }
  130.         }
  131.  
  132.         public void SetFilter(Filter filter)
  133.         {
  134.             if (filter == Filter.AddFilter)
  135.             {
  136.                 this.hasFilter = true;
  137.             }
  138.             else if (filter == Filter.RemoveFilter)
  139.             {
  140.                 this.hasFilter = false;
  141.             }
  142.         }
  143.     }
  144.  
  145.     public enum Filter
  146.     {
  147.         RemoveFilter,
  148.         AddFilter
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement