Advertisement
Guest User

ParkingValidation

a guest
Feb 18th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ParkingValidation
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int n = int.Parse(Console.ReadLine());
  12.  
  13. var userBook = new Dictionary<string, string>();
  14.  
  15. for (int i = 0; i < n; i++)
  16. {
  17. string[] text = Console.ReadLine().Split().ToArray();
  18. string instruction = text[0];
  19. string userName = text[1];
  20. if (instruction == "register")
  21. {
  22.  
  23. string licensePlate = text[2];
  24.  
  25. if (userBook.ContainsKey(userName) == false)
  26. {
  27. if (LicenseValidation(licensePlate) == false)
  28. {
  29. Console.WriteLine($"ERROR: invalid license plate {licensePlate}");
  30.  
  31. }
  32. else if (userBook.ContainsValue(licensePlate))
  33. {
  34. Console.WriteLine($"ERROR: license plate {licensePlate} is busy");
  35.  
  36. }
  37. else
  38. {
  39. userBook.Add(userName, licensePlate);
  40. Console.WriteLine($"{userName} registered {licensePlate} successfully");
  41. }
  42. }
  43. else
  44. {
  45. Console.WriteLine($"ERROR: already registered with plate number {userBook[userName]}");
  46.  
  47. }
  48. }
  49. else if (instruction == "unregister")
  50. {
  51. if (userBook.ContainsKey(userName) == false)
  52. {
  53. Console.WriteLine($"ERROR: user {userName} not found");
  54. }
  55. else
  56. {
  57. userBook.Remove(userName);
  58. Console.WriteLine($"user {userName} unregistered successfully");
  59. }
  60. }
  61. }
  62. foreach (var pair in userBook)
  63. {
  64. Console.WriteLine($"{pair.Key} => {pair.Value}");
  65. }
  66. }
  67.  
  68. static bool LicenseValidation(string licensePlate)
  69. {
  70.  
  71. var firstLast = licensePlate.Substring(0, 2) + licensePlate.Substring(6, 2);
  72. bool firstLastValidation = false;
  73.  
  74. foreach (var symbol in firstLast)
  75. {
  76. if (symbol >= 'A' && symbol <= 'Z')
  77. {
  78. firstLastValidation = true;
  79. }
  80. else
  81. {
  82. firstLastValidation = false;
  83. break;
  84. }
  85. }
  86.  
  87.  
  88. var middleNumbers = licensePlate.Substring(2, 4);
  89. bool middleValidation = false;
  90.  
  91. foreach (var number in middleNumbers)
  92. {
  93. if (number >= '1' && number <= '9')
  94. {
  95. middleValidation = true;
  96. }
  97. else
  98. {
  99. middleValidation = false;
  100. break;
  101. }
  102. }
  103.  
  104. if (middleValidation && firstLastValidation == true)
  105. {
  106. return true;
  107. }
  108. else
  109. {
  110. return false;
  111. }
  112.  
  113. }
  114.  
  115.  
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement