Advertisement
Pazzobg

ParkingValidation_DictsMoreExercises_Dim

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