Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05.ParkingValidation
  6. {
  7.     class StartUp
  8.     {
  9.         static void Main()
  10.         {
  11.             int numberOfCommands = int.Parse(Console.ReadLine());
  12.             Dictionary<string, string> parkingRegister = new Dictionary<string, string>();
  13.  
  14.             for (int i = 0; i < numberOfCommands; i++)
  15.             {
  16.                 string[] args = Console.ReadLine().Split(' ');
  17.                 string command = args[0];
  18.                 string username = args[1];
  19.  
  20.                 if (command == "register")
  21.                 {
  22.                     string licensePlate = args[2];
  23.                     string licensePlateNumbers = licensePlate.Substring(2, 4);
  24.                     string licensePlateLetters = licensePlate.Substring(0, 2) + licensePlate.Substring(6);
  25.                     bool isValid = false;
  26.                     if (licensePlate.Length == 8 && int.TryParse(licensePlateNumbers, out int ignor))
  27.                     {
  28.                         foreach (var letter in licensePlateLetters)
  29.                         {
  30.                             isValid = Char.IsLetter(letter) && Char.IsUpper(letter);
  31.                             if (!isValid)
  32.                             {
  33.                                 break;
  34.                             }
  35.                         }
  36.                     }
  37.                     if (isValid == true)
  38.                     {
  39.                         if (!parkingRegister.ContainsKey(username))
  40.                         {
  41.                             if (!parkingRegister.ContainsValue(licensePlate))
  42.                             {
  43.                                 parkingRegister.Add(username, licensePlate);
  44.                                 Console.WriteLine($"{username} registered {licensePlate} successfully");
  45.                             }
  46.                             else
  47.                             {
  48.                                 Console.WriteLine($"ERROR: license plate {licensePlate} is busy");
  49.                             }
  50.                         }
  51.                         else
  52.                         {
  53.                             Console.WriteLine($"ERROR: already registered with plate number {parkingRegister[username]}");
  54.                         }
  55.                     }
  56.                     else
  57.                     {
  58.                         Console.WriteLine($"ERROR: invalid license plate {licensePlate}");
  59.                     }
  60.                 }
  61.                 else if (command == "unregister")
  62.                 {
  63.                     if (parkingRegister.ContainsKey(username))
  64.                     {
  65.                         parkingRegister.Remove(username);
  66.                         Console.WriteLine($"user {username} unregistered successfully");
  67.                     }
  68.                     else
  69.                     {
  70.                         Console.WriteLine($"ERROR: user {username} not found");
  71.                     }
  72.                 }
  73.             }
  74.             if (parkingRegister.Count > 0)
  75.             {
  76.                 Console.WriteLine(string.Join(Environment.NewLine, parkingRegister.Select(u => $"{u.Key} => {u.Value}")));
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement