Advertisement
Guest User

Untitled

a guest
Jun 26th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. namespace _05.Parking_Validation
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public static class ParkingValidation
  8.     {
  9.         public static void Main()
  10.         {
  11.             var registered = new Dictionary<string, string>();
  12.             int n = int.Parse(Console.ReadLine());
  13.             for (int i = 0; i < n; i++)
  14.             {
  15.                 var split = Console.ReadLine().Split();
  16.                 string name = split[1];
  17.                 switch (split[0])
  18.                 {
  19.                     case "register":
  20.                         string plate = split[2];
  21.                         if (registered.ContainsKey(name))
  22.                         {
  23.                             Console.WriteLine($"ERROR: already registered with plate number {registered[name]}");
  24.                             continue;
  25.                         }
  26.                         if (!IsPlateValid(plate))
  27.                         {
  28.                             Console.WriteLine($"ERROR: invalid license plate {plate}");
  29.                             continue;
  30.                         }
  31.                         if (registered.Any(kvp => kvp.Value == plate))
  32.                         {
  33.                             Console.WriteLine($"ERROR: license plate {plate} is busy");
  34.                             continue;
  35.                         }
  36.                         Console.WriteLine($"{name} registered {plate} successfully");
  37.                         registered[name] = plate;
  38.                         break;
  39.  
  40.                     case "unregister":
  41.                         if (!registered.ContainsKey(name))
  42.                         {
  43.                             Console.WriteLine($"ERROR: user {name} not found");
  44.                             continue;
  45.                         }
  46.                         Console.WriteLine($"user {name} unregistered successfully");
  47.                         registered.Remove(name);
  48.                         break;
  49.                 }
  50.             }
  51.             foreach (var plate in registered)
  52.             {
  53.                 Console.WriteLine($"{plate.Key} => {plate.Value}");
  54.             }
  55.         }
  56.  
  57.         private static bool IsPlateValid(string plate)
  58.         {
  59.             return
  60.                 plate.Length == 8
  61.                 && plate
  62.                 .Take(2)
  63.                 .All(c => c >= 'A' && c <= 'Z')
  64.                 && plate
  65.                 .Skip(2)
  66.                 .Take(4)
  67.                 .All(c => c >= '0' && c <= '9')
  68.                 && plate
  69.                 .Skip(6)
  70.                 .Take(2)
  71.                 .All(c => c >= 'A' && c <= 'Z');
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement