Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _05.Parking_Validation
  8. {
  9. class ParkingValidation
  10. {
  11. static void Main(string[] args)
  12. {
  13. //условирето е на дъното на кода
  14.  
  15. int numberOfRegistrations = int.Parse(Console.ReadLine());
  16. var registrationDatabase = new Dictionary<string, string>();
  17. for (int i = 0; i < numberOfRegistrations; i++)
  18. {
  19. var registrationInput = Console.ReadLine().Split().ToList();
  20.  
  21.  
  22.  
  23. if(registrationInput[0] == "register")
  24. {
  25. CheckPlate(registrationInput);
  26.  
  27.  
  28. bool canContinue = true;
  29. string name = registrationInput[1];
  30. string carNumber = registrationInput[2];
  31. var firstTwoLetters = registrationInput[2].Take(2).ToList();
  32. var lastTwoLetters = registrationInput[2].Skip(6).Take(2).ToList();
  33.  
  34. if (!registrationDatabase.ContainsKey(name))
  35. {
  36. registrationDatabase.Add(name, carNumber);
  37. }
  38. else if (registrationDatabase.ContainsKey(name))
  39. {
  40. Console.WriteLine($"ERROR: already registered with plate number {carNumber}");
  41. canContinue = false;
  42. }
  43. else if (registrationDatabase.ContainsValue(carNumber))
  44. {
  45.  
  46. canContinue = false;
  47. }
  48. if(canContinue)
  49. {
  50. Console.WriteLine($"{name} registred {carNumber} successfully");
  51. }
  52. }
  53.  
  54. if(registrationInput[0] == "unregister")
  55. {
  56. string name = registrationInput[1];
  57.  
  58. if (!registrationDatabase.ContainsKey(name))
  59. {
  60. Console.WriteLine($"ERROR: user {name} not found");
  61. }
  62. else
  63. {
  64. registrationDatabase.Remove(name);
  65. Console.WriteLine($"user {name} unregistered successfully");
  66. }
  67. }
  68. }
  69.  
  70. foreach(var parkingUser in registrationDatabase)
  71. {
  72. var name = parkingUser.Key;
  73. var carNumber = parkingUser.Value;
  74. Console.WriteLine($"{name} => {carNumber}");
  75. }
  76. }
  77.  
  78. private static void CheckPlate(List<string> registrationInput)
  79. {
  80. string carNumber = registrationInput[2];
  81. var firstTwoLetters = registrationInput[2].Take(2).ToList();
  82. var lastTwoLetters = registrationInput[2].Skip(6).Take(2).ToList();
  83. var digitsInNumber = carNumber.Skip(2).Take(4).ToList();
  84. firstTwoLetters.AddRange(lastTwoLetters);
  85.  
  86. if (carNumber.Length < 8 || carNumber.Length > 8)
  87. {
  88. Console.WriteLine($"ERROR: invalid license plate {carNumber}");
  89. return;
  90. }
  91.  
  92. for (int i = 0; i < firstTwoLetters.Count; i++)
  93. {
  94. if (!char.IsLetter(firstTwoLetters[i])){
  95.  
  96. Console.WriteLine($"ERROR: invalid license plate {carNumber}");
  97. return;
  98. }
  99. if (!char.IsUpper(firstTwoLetters[i]))
  100. {
  101. Console.WriteLine($"ERROR: invalid license plate {carNumber}");
  102. return;
  103. }
  104. }
  105.  
  106. for (int i = 0; i < digitsInNumber.Count; i++)
  107. {
  108. if (!char.IsNumber(digitsInNumber[i]))
  109. {
  110. Console.WriteLine($"ERROR: invalid license plate {carNumber}");
  111. return;
  112. }
  113. }
  114. }
  115. }
  116. }
  117.  
  118.  
  119. //Problem 6. Parking Validation
  120. //SoftUni just got a huge, shiny new parking lot in a super-secret location(under the Code Ground hall). It’s so fancy, it even has online parking validation.Except, the online service doesn’t work. It can only receive users’ data, but doesn’t know what to do with it. Good thing you’re on the dev team and know how to fix it, right?
  121. //Write a program, which validates parking for an online service.Users can register to park and unregister to leave.
  122. //The system supports license plate validation. A valid license plate has the following 3 distinct characteristics:
  123. //It is always exactly 8 characters long.
  124. //Its first 2 and last 2 characters are always uppercase Latin letters
  125. //The 4 characters in the middle are always digits
  126. //If any of the aforementioned conditions fails, the license plate is invalid.
  127. //The program receives 2 commands:
  128. //“register { username} {licensePlateNumber}”:
  129. //The system only supports one car per user at the moment, so if a user tries to register another license plate, using the same username, the system should print:
  130. //“ERROR: already registered with plate number {licensePlateNumber}”
  131. //If the license plate is invalid, the system should print:
  132. //“ERROR: invalid license plate {licensePlateNumber}“
  133. //If the user tries to register someone else’s license plate, the system should print:
  134. //“ERROR: license plate { licensePlateNumber } is busy”
  135. //If the aforementioned checks pass successfully, the plate can be registered, so the system should print:
  136. //“{username} registered {licensePlateNumber} successfully”
  137. //“unregister {username}”:
  138. //If the user is not present in the database, the system should print:
  139. //“ERROR: user {username} not found”
  140. //If the aforementioned check passes successfully, the system should print:
  141. //“user {username} unregistered successfully”
  142. //After you execute all of the commands, print all the currently registered users and their license plates in the format:
  143. //“{username} => {licensePlateNumber}”
  144. //Input
  145. //First line: n – number of commands – integer
  146. //Next n lines: commands in one of two possible formats:
  147. //Register: “register {username} {licensePlateNumber}”
  148. //Unregister: “unregister {username}”
  149. //The input will always be valid and you do not need to check it explicitly.
  150. //Examples
  151. //Input
  152. //Output
  153. //5
  154. //register some0ne CS1234JS
  155. //register vankata JAVA123S
  156. //register vankata AB4142CD
  157. //register housey VR1223EE
  158. //unregister housey
  159. //some0ne registered CS1234JS successfully
  160. //ERROR: invalid license plate JAVA123S
  161. //vankata registered AB4142CD successfully
  162. //housey registered VR1223EE successfully
  163. //user housey unregistered successfully
  164. //some0ne => CS1234JS
  165. //vankata => AB4142CD
  166. //4
  167. //register testUser AA4132BB
  168. //register testuser AA4132BB
  169. //register testuser AA9999BB
  170. //unregister testUser
  171. //testUser registered AA4132BB successfully
  172. //ERROR: license plate AA4132BB is busy
  173. //testuser registered AA9999BB successfully
  174. //user testUser unregistered successfully
  175. //testuser => AA9999BB
  176. //7
  177. //register gosho mm1111XX
  178. //register gosho MM1111xx
  179. //register gosho MMaaaaXX
  180. //unregister gosho
  181. //register gosho MM1111XX
  182. //unregister gosho
  183. //unregister pesho
  184. //ERROR: invalid license plate mm1111XX
  185. //ERROR: invalid license plate MM1111xx
  186. //ERROR: invalid license plate MMaaaaXX
  187. //ERROR: user gosho not found
  188. //gosho registered MM1111XX successfully
  189. //user gosho unregistered successfully
  190. //ERROR: user pesho not found
  191. //Problem 7.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement