Advertisement
sivancheva

ParkingValidation

Sep 17th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _22_05_ParkingValidation
  9. {
  10. class ParkingValidation
  11. {
  12. static void Main(string[] args)
  13. {
  14. int n = int.Parse(Console.ReadLine());
  15. var database = new Dictionary<string, string>();
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. var input = Console.ReadLine();
  20. var inputArray = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  21. .Select(s => s.Trim())
  22. .ToArray();
  23. var command = inputArray[0].ToLower();
  24. var userName = inputArray[1];
  25. var plate = inputArray[2];
  26.  
  27. var paternPlate = new Regex(@"^[A - Z]{ 2}\d{ 4}[A-Z]{2}$");
  28.  
  29.  
  30. if (command == "register")
  31. {
  32. var match = paternPlate.Match(plate);
  33. if (!match.Success)
  34. {
  35. Console.WriteLine($"ERROR: invalid license plate {plate}");
  36. continue;
  37. }
  38. else if (!database.ContainsKey(userName))
  39. {
  40. database.Add(userName, plate);
  41. Console.WriteLine($"{userName} registered {plate} successfully");
  42. }
  43. else if (database.ContainsKey(userName) && database.ContainsValue(plate))
  44. {
  45. Console.WriteLine($"ERROR: already registered with plate number {database[userName]}");
  46. }
  47. else if (database.ContainsValue(plate))
  48. {
  49. Console.WriteLine($"ERROR: license plate {plate} is busy");
  50. }
  51.  
  52. }
  53.  
  54. else if (command == "unregister")
  55. {
  56. if (!database.ContainsKey(userName))
  57. {
  58. Console.WriteLine($"ERROR: user {userName} not found");
  59. }
  60. else
  61. {
  62. database.Remove(database[userName]);
  63. Console.WriteLine($"user {userName} unregistered successfully");
  64. }
  65. }
  66.  
  67. }
  68. foreach (var pair in database)
  69. {
  70. Console.WriteLine($"{pair.Key} => {pair.Value}");
  71. }
  72.  
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement