Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace PartyReservationFilterModule
- {
- using System;
- using System.Linq;
- using System.Text;
- public class PartyReservationFilterModule
- {
- public static void Main()
- {
- Person[] names = SplitString(Console.ReadLine(), ' ').Select(n => new Person(n)).ToArray();
- string inputLine = Console.ReadLine();
- while (inputLine != "Print")
- {
- string[] commandInfo = SplitString(inputLine, ';');
- string filterAction = commandInfo[0];
- string personNameInfo = commandInfo[1];
- string toMatch = string.Empty;
- Filter filter = filterAction == "Add filter" ? Filter.AddFilter : Filter.RemoveFilter;
- Func<string, string, bool> funcToUse = default(Func<string, string, bool>);
- if (personNameInfo == "Starts with")
- {
- toMatch = commandInfo[2];
- funcToUse = Functions.StartsWith;
- }
- else if (personNameInfo == "Ends with")
- {
- toMatch = commandInfo[2];
- funcToUse = Functions.EndsWith;
- }
- else if (personNameInfo == "Length")
- {
- toMatch = new string(' ', int.Parse(commandInfo[2]));
- funcToUse = Functions.HasLenght;
- }
- else if (personNameInfo == "Contains")
- {
- toMatch = commandInfo[2];
- funcToUse = Functions.Contains;
- }
- Functions.ManipulateFilters(names, toMatch, funcToUse, filter);
- inputLine = Console.ReadLine();
- }
- string result = Functions.GetNonFilteredGuests(names, p => p.HasFilter);
- Console.WriteLine(result);
- }
- public static string[] SplitString(string toSplit, char toSplitBy)
- {
- return toSplit.Split(new[] { toSplitBy }, StringSplitOptions.RemoveEmptyEntries);
- }
- }
- public static class Functions
- {
- public static readonly Func<Person[], Func<Person, bool>, string> GetNonFilteredGuests = (names, func) =>
- {
- StringBuilder result = new StringBuilder();
- foreach (Person person in names)
- {
- if (func(person))
- {
- continue;
- }
- result.Append(person.Name);
- result.Append(" ");
- }
- return result.ToString();
- };
- public static readonly Action<Person[], string, Func<string, string, bool>, Filter> ManipulateFilters = (names, toMatch, func, filter) =>
- {
- for (int currElement = 0; currElement < names.Length; currElement++)
- {
- Person currPerson = names[currElement];
- if (func(currPerson.Name, toMatch))
- {
- currPerson.SetFilter(filter);
- }
- }
- };
- public static readonly Func<string, string, bool> StartsWith = (f, s)
- => f.Substring(0, s.Length) == s;
- public static readonly Func<string, string, bool> EndsWith = (f, s)
- => f.Substring(f.Length - s.Length, s.Length) == s;
- public static readonly Func<string, string, bool> HasLenght = (f, s)
- => f.Length == s.Length;
- public static readonly Func<string, string, bool> Contains = (f, s)
- => f.Contains(s);
- }
- public class Person
- {
- private string name;
- private bool hasFilter;
- public Person(string name)
- {
- this.name = name;
- this.hasFilter = false;
- }
- public string Name
- {
- get { return this.name; }
- set { this.name = value; }
- }
- public bool HasFilter
- {
- get { return this.hasFilter; }
- }
- public void SetFilter(Filter filter)
- {
- if (filter == Filter.AddFilter)
- {
- this.hasFilter = true;
- }
- else if (filter == Filter.RemoveFilter)
- {
- this.hasFilter = false;
- }
- }
- }
- public enum Filter
- {
- RemoveFilter,
- AddFilter
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement