Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. namespace P05_ParkingValidation
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. public class P05_ParkingValidation
  7. {
  8. public static void Main()
  9. {
  10. var n = int.Parse(Console.ReadLine());
  11. var dataBase = new Dictionary<string, string>();
  12.  
  13. for (int i = 0; i < n; i++)
  14. {
  15. var input = Console.ReadLine().Split();
  16. var command = input[0].ToLower();
  17. var user = input[1];
  18.  
  19. if (command == "unregister")
  20. {
  21. Unregister(dataBase, user);
  22. }
  23.  
  24. else if (command == "register")
  25. {
  26. var plate = input[2];
  27. Register(dataBase, user, plate);
  28. }
  29. }
  30.  
  31. foreach (var item in dataBase)
  32. {
  33. Console.WriteLine(item.Key + " => " + item.Value);
  34. }
  35. }
  36.  
  37. public static void Register(Dictionary<string, string> dataBase, string user, string plate)
  38. {
  39. // if a user tries to register another license plate,
  40. // using the same username:
  41.  
  42. if (dataBase.ContainsKey(user) && (dataBase[user] != plate))
  43. {
  44. Console.WriteLine($"ERROR: already registered " +
  45. $"with plate number {dataBase[user]}");
  46. return;
  47. }
  48.  
  49. // if the license plate is invalid:
  50.  
  51. if (!CheckIfValid(plate))
  52. {
  53. Console.WriteLine($"ERROR: invalid license plate {plate}");
  54. return;
  55. }
  56.  
  57. // if the user tries to register someone else's license plate:
  58.  
  59. foreach (var item in dataBase)
  60. {
  61. if (item.Key != user && item.Value == plate)
  62. {
  63. Console.WriteLine($"ERROR: license plate {plate} is busy");
  64. return;
  65. }
  66. }
  67.  
  68. // if checks pass successfully:
  69.  
  70. dataBase[user] = plate;
  71. Console.WriteLine($"{user} registered {plate} successfully");
  72. }
  73.  
  74. public static void Unregister(Dictionary<string, string> dataBase, string user)
  75. {
  76. if (!dataBase.ContainsKey(user))
  77. {
  78. Console.WriteLine($"ERROR: user {user} not found");
  79. }
  80. else
  81. {
  82. dataBase.Remove(user);
  83. Console.WriteLine($"user {user} unregistered successfully");
  84. }
  85. }
  86.  
  87. public static bool CheckIfValid(string plate)
  88. {
  89. // check plate length:
  90.  
  91. if (plate.Length != 8)
  92. {
  93. return false;
  94. }
  95.  
  96. // check plate letters:
  97.  
  98. var plateLetters = new char[]
  99. {
  100. plate[0],
  101. plate[1],
  102. plate[6],
  103. plate[7],
  104. };
  105.  
  106. foreach (var letter in plateLetters)
  107. {
  108. if (letter < 65 || letter > 90)
  109. {
  110. return false;
  111. }
  112. }
  113.  
  114. // check plate digits:
  115.  
  116. for (int i = 2; i < 6; i++)
  117. {
  118. if (plate[i] < 48 || plate[i] > 57)
  119. {
  120. return false;
  121. }
  122. }
  123.  
  124. return true;
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement