Advertisement
Guest User

Parking

a guest
Jun 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 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 Test
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var registredUsersAndPlates = new Dictionary<string, string>();
  14.  
  15.             var entrtiesCount = int.Parse(Console.ReadLine());
  16.  
  17.             for (int i = 0; i < entrtiesCount; i++)
  18.             {
  19.                 var commnadList = Console.ReadLine().Split(' ').ToList();
  20.                 var instruction = commnadList[0];
  21.                 if (instruction == "register")
  22.                 {
  23.                     var user = commnadList[1];
  24.                     var plate = commnadList[2];
  25.  
  26.                     if (registredUsersAndPlates.ContainsKey(user))
  27.                     {
  28.                         Console.WriteLine($"ERROR: already registered with plate number {registredUsersAndPlates[user]}"); // ok
  29.                         continue;
  30.                     }
  31.                     if (IsPlateInvalid(plate))
  32.                     {
  33.                         Console.WriteLine($"ERROR: invalid license plate {plate}"); // ok
  34.                         continue;
  35.                     }
  36.                     if (registredUsersAndPlates.ContainsValue(plate))
  37.                     {
  38.                         Console.WriteLine($"ERROR: license plate {plate} is busy"); // ok
  39.                         continue;
  40.                     }
  41.                     if (!registredUsersAndPlates.ContainsKey(user))
  42.                     {
  43.                         registredUsersAndPlates[user] = plate;
  44.                         Console.WriteLine($"{user} registered {plate} successfully"); // ok
  45.                     }
  46.                 }
  47.  
  48.                 if (instruction == "unregister")
  49.                 {
  50.                     var user = commnadList[1];
  51.                     if (!registredUsersAndPlates.ContainsKey(user))
  52.                     {
  53.                         Console.WriteLine($"ERROR: user {user} not found");
  54.                     }
  55.                     else
  56.                     {
  57.                         registredUsersAndPlates.Remove(user);
  58.                         Console.WriteLine($"user {user} unregistered successfully");
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             foreach (var user in registredUsersAndPlates)
  64.             {
  65.                 Console.WriteLine($"{user.Key} => {user.Value}");
  66.             }
  67.         }
  68.  
  69.         static bool IsPlateInvalid(string plate)
  70.         {
  71.             if (plate.Length != 8)
  72.             {
  73.                 return true;
  74.             }
  75.             var firstLastTwoSymbols = plate.Substring(0, 2) + plate.Substring(6);
  76.             foreach (var symbol in firstLastTwoSymbols)
  77.             {
  78.                 if (symbol < 'A' || 'Z' < symbol)
  79.                 {
  80.                     return true;
  81.                 }
  82.             }
  83.             var midleSymbols = plate.Substring(2, 4);
  84.             foreach (var symbol in midleSymbols)
  85.             {
  86.                 if (symbol < '0' || '9' < symbol)
  87.                 {
  88.                     return true;
  89.                 }
  90.             }
  91.  
  92.             return false;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement