Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ParkingValidation
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- var userBook = new Dictionary<string, string>();
- for (int i = 0; i < n; i++)
- {
- string[] text = Console.ReadLine().Split().ToArray();
- string instruction = text[0];
- string userName = text[1];
- if (instruction == "register")
- {
- string licensePlate = text[2];
- if (userBook.ContainsKey(userName) == false)
- {
- if (LicenseValidation(licensePlate) == false)
- {
- Console.WriteLine($"ERROR: invalid license plate {licensePlate}");
- }
- else if (userBook.ContainsValue(licensePlate))
- {
- Console.WriteLine($"ERROR: license plate {licensePlate} is busy");
- }
- else
- {
- userBook.Add(userName, licensePlate);
- Console.WriteLine($"{userName} registered {licensePlate} successfully");
- }
- }
- else
- {
- Console.WriteLine($"ERROR: already registered with plate number {userBook[userName]}");
- }
- }
- else if (instruction == "unregister")
- {
- if (userBook.ContainsKey(userName) == false)
- {
- Console.WriteLine($"ERROR: user {userName} not found");
- }
- else
- {
- userBook.Remove(userName);
- Console.WriteLine($"user {userName} unregistered successfully");
- }
- }
- }
- foreach (var pair in userBook)
- {
- Console.WriteLine($"{pair.Key} => {pair.Value}");
- }
- }
- static bool LicenseValidation(string licensePlate)
- {
- var firstLast = licensePlate.Substring(0, 2) + licensePlate.Substring(6, 2);
- bool firstLastValidation = false;
- foreach (var symbol in firstLast)
- {
- if (symbol >= 'A' && symbol <= 'Z')
- {
- firstLastValidation = true;
- }
- else
- {
- firstLastValidation = false;
- break;
- }
- }
- var middleNumbers = licensePlate.Substring(2, 4);
- bool middleValidation = false;
- foreach (var number in middleNumbers)
- {
- if (number >= '1' && number <= '9')
- {
- middleValidation = true;
- }
- else
- {
- middleValidation = false;
- break;
- }
- }
- if (middleValidation && firstLastValidation == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement