Advertisement
desislava_topuzakova

10. The Party Reservation Filter Module

Jun 3rd, 2022
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ThePartyReservationFilterModule
  6. {
  7. class ThePartyReservationFilterModule
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> partyPeople = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList();
  12. Dictionary<string,Predicate<string>> filters = new Dictionary<string, Predicate<string>>();
  13. string input;
  14. while ((input = Console.ReadLine()) != "Print")
  15. {
  16. string[] addRemFilter = input.Split(";", StringSplitOptions.RemoveEmptyEntries);
  17. string addRemove = addRemFilter[0];
  18. string condition = addRemFilter[1];
  19. string argument = addRemFilter[2];
  20. switch (addRemove)
  21. {
  22. case "Add filter":
  23. Predicate<string> currentPredicate = GetPredicate(condition, argument);
  24. filters.Add(argument, currentPredicate);
  25. break;
  26. case "Remove filter":
  27. currentPredicate = GetPredicate(condition, argument);
  28. filters.Remove(argument);
  29. break;
  30. }
  31. }
  32.  
  33. foreach (var filter in filters)
  34. {
  35. partyPeople.RemoveAll(filter.Value);
  36. }
  37. Console.WriteLine(string.Join(" ", partyPeople));
  38. }
  39. private static Predicate<string> GetPredicate(string commandType, string arg)
  40. {
  41. switch (commandType)
  42. {
  43. case "Starts with":
  44. return (name) => name.StartsWith(arg);
  45. case "Ends with":
  46. return (name) => name.EndsWith(arg);
  47. case "Length":
  48. return (name) => name.Length == int.Parse(arg);
  49. case "Contains":
  50. return (name) => name.Contains(arg);
  51. default:
  52. throw new ArgumentException("Invalid command type: " + commandType);
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement