Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 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 _05.Parking_Validation
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.  
  15.             var n = int.Parse(Console.ReadLine());
  16.             var userLicense = new Dictionary<string, string>();
  17.  
  18.             for (int i = 0; i < n; i++)
  19.             {
  20.                 var input = Console.ReadLine();
  21.                 var command = input
  22.                     .Split()
  23.                     .ToArray()[0];
  24.                 var username = input
  25.                     .Split()
  26.                     .ToArray()[1];
  27.  
  28.                 switch (command)
  29.                 {
  30.                     case "register":
  31.                         var license = input
  32.                             .Split()
  33.                             .ToArray()[2];
  34.                         if (!IsValid(license))
  35.                         {
  36.                             Console.WriteLine($"ERROR: invalid license plate {license}");
  37.                             break;
  38.                         }
  39.  
  40.                         if (userLicense.ContainsKey(username))
  41.                         {
  42.                             Console.WriteLine($"ERROR: already registered " +
  43.                                 $"with plate number {userLicense[username]}");
  44.                             break;
  45.                         }
  46.  
  47.                         if (userLicense.ContainsValue(license))
  48.                         {
  49.                             Console.WriteLine($"ERROR: license plate {license} is busy");
  50.                             break;
  51.                         }
  52.  
  53.                         userLicense[username] = license;
  54.                         Console.WriteLine($"{username} registered {license} successfully");
  55.                         break;
  56.  
  57.                     case "unregister":
  58.                         if (!userLicense.ContainsKey(username))
  59.                         {
  60.                             Console.WriteLine($"ERROR: user {username} not found");
  61.                             break;
  62.                         }
  63.  
  64.                         userLicense.Remove(username);
  65.                         Console.WriteLine($"user {username} unregistered successfully");
  66.                         break;
  67.                 }
  68.             }
  69.  
  70.             foreach (var pair in userLicense)
  71.             {
  72.                 Console.WriteLine($"{pair.Key} => {pair.Value}");
  73.             }
  74.         }
  75.  
  76.         private static bool IsValid(string license)
  77.         {
  78.             string pattern = "([A-Z]{2})([0-9]{4})([A-Z]{2})";
  79.             Regex regex = new Regex(pattern);
  80.             Match match = regex.Match(license);
  81.             return match.Success;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement