Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ParkingValidation
  6. {
  7. class ParkingValidation
  8. {
  9. static void Main()
  10. {
  11. var n = int.Parse(Console.ReadLine());
  12. var data = new Dictionary<string, string>();
  13. for (int i = 0; i < n; i++)
  14. {
  15. var input = Console.ReadLine()
  16. .Split(' ')
  17. .ToArray();
  18.  
  19. var user = input[1];
  20.  
  21. switch (input[0])
  22. {
  23. case "register":
  24. var plate = input[2];
  25. if (data.ContainsKey(user))
  26. {
  27. Console.WriteLine($"ERROR: already registered with plate number {plate}");
  28. }
  29. else if (IsInvalidLicense(plate))
  30. {
  31. Console.WriteLine($"ERROR: invalid license plate {plate}");
  32. }
  33. else if (data.ContainsValue(plate))
  34. {
  35. Console.WriteLine($"ERROR: license plate {plate} is busy");
  36. }
  37. else
  38. {
  39. data[user] = plate;
  40. Console.WriteLine($"{user} registered {plate} successfully");
  41. }
  42. break;
  43. case "unregister":
  44. if (!data.ContainsKey(user))
  45. {
  46. Console.WriteLine($"ERROR: user {user} not found");
  47. }
  48. else
  49. {
  50. data.Remove(user);
  51. Console.WriteLine($"user {user} unregistered successfully");
  52. }
  53. break;
  54.  
  55. }
  56. }
  57. foreach (var item in data)
  58. {
  59. Console.WriteLine($"{item.Key} => {item.Value}");
  60. }
  61. }
  62.  
  63. public static bool IsInvalidLicense(string license)
  64. {
  65. if (license.Length != 8)
  66. {
  67. return true;
  68. }
  69. for (int i = 2; i < license.Length-2; i++)
  70. {
  71. if (!int.TryParse(license[i].ToString(), out int result))
  72. {
  73. return true;
  74. }
  75. }
  76. if (license[0] < 65 || license[0] > 90) return true;
  77. if (license[1] < 65 || license[1] > 90) return true;
  78. if (license[6] < 65 || license[6] > 90) return true;
  79. if (license[7] < 65 || license[7] > 90) return true;
  80. return false;
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement