Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _05.Parking_Validation
- {
- class ParkingValidation
- {
- static void Main(string[] args)
- {
- //условирето е на дъното на кода
- int numberOfRegistrations = int.Parse(Console.ReadLine());
- var registrationDatabase = new Dictionary<string, string>();
- for (int i = 0; i < numberOfRegistrations; i++)
- {
- var registrationInput = Console.ReadLine().Split().ToList();
- if(registrationInput[0] == "register")
- {
- CheckPlate(registrationInput);
- bool canContinue = true;
- string name = registrationInput[1];
- string carNumber = registrationInput[2];
- var firstTwoLetters = registrationInput[2].Take(2).ToList();
- var lastTwoLetters = registrationInput[2].Skip(6).Take(2).ToList();
- if (!registrationDatabase.ContainsKey(name))
- {
- registrationDatabase.Add(name, carNumber);
- }
- else if (registrationDatabase.ContainsKey(name))
- {
- Console.WriteLine($"ERROR: already registered with plate number {carNumber}");
- canContinue = false;
- }
- else if (registrationDatabase.ContainsValue(carNumber))
- {
- canContinue = false;
- }
- if(canContinue)
- {
- Console.WriteLine($"{name} registred {carNumber} successfully");
- }
- }
- if(registrationInput[0] == "unregister")
- {
- string name = registrationInput[1];
- if (!registrationDatabase.ContainsKey(name))
- {
- Console.WriteLine($"ERROR: user {name} not found");
- }
- else
- {
- registrationDatabase.Remove(name);
- Console.WriteLine($"user {name} unregistered successfully");
- }
- }
- }
- foreach(var parkingUser in registrationDatabase)
- {
- var name = parkingUser.Key;
- var carNumber = parkingUser.Value;
- Console.WriteLine($"{name} => {carNumber}");
- }
- }
- private static void CheckPlate(List<string> registrationInput)
- {
- string carNumber = registrationInput[2];
- var firstTwoLetters = registrationInput[2].Take(2).ToList();
- var lastTwoLetters = registrationInput[2].Skip(6).Take(2).ToList();
- var digitsInNumber = carNumber.Skip(2).Take(4).ToList();
- firstTwoLetters.AddRange(lastTwoLetters);
- if (carNumber.Length < 8 || carNumber.Length > 8)
- {
- Console.WriteLine($"ERROR: invalid license plate {carNumber}");
- return;
- }
- for (int i = 0; i < firstTwoLetters.Count; i++)
- {
- if (!char.IsLetter(firstTwoLetters[i])){
- Console.WriteLine($"ERROR: invalid license plate {carNumber}");
- return;
- }
- if (!char.IsUpper(firstTwoLetters[i]))
- {
- Console.WriteLine($"ERROR: invalid license plate {carNumber}");
- return;
- }
- }
- for (int i = 0; i < digitsInNumber.Count; i++)
- {
- if (!char.IsNumber(digitsInNumber[i]))
- {
- Console.WriteLine($"ERROR: invalid license plate {carNumber}");
- return;
- }
- }
- }
- }
- }
- //Problem 6. Parking Validation
- //SoftUni just got a huge, shiny new parking lot in a super-secret location(under the Code Ground hall). It’s so fancy, it even has online parking validation.Except, the online service doesn’t work. It can only receive users’ data, but doesn’t know what to do with it. Good thing you’re on the dev team and know how to fix it, right?
- //Write a program, which validates parking for an online service.Users can register to park and unregister to leave.
- //The system supports license plate validation. A valid license plate has the following 3 distinct characteristics:
- //It is always exactly 8 characters long.
- //Its first 2 and last 2 characters are always uppercase Latin letters
- //The 4 characters in the middle are always digits
- //If any of the aforementioned conditions fails, the license plate is invalid.
- //The program receives 2 commands:
- //“register { username} {licensePlateNumber}”:
- //The system only supports one car per user at the moment, so if a user tries to register another license plate, using the same username, the system should print:
- //“ERROR: already registered with plate number {licensePlateNumber}”
- //If the license plate is invalid, the system should print:
- //“ERROR: invalid license plate {licensePlateNumber}“
- //If the user tries to register someone else’s license plate, the system should print:
- //“ERROR: license plate { licensePlateNumber } is busy”
- //If the aforementioned checks pass successfully, the plate can be registered, so the system should print:
- //“{username} registered {licensePlateNumber} successfully”
- //“unregister {username}”:
- //If the user is not present in the database, the system should print:
- //“ERROR: user {username} not found”
- //If the aforementioned check passes successfully, the system should print:
- //“user {username} unregistered successfully”
- //After you execute all of the commands, print all the currently registered users and their license plates in the format:
- //“{username} => {licensePlateNumber}”
- //Input
- //First line: n – number of commands – integer
- //Next n lines: commands in one of two possible formats:
- //Register: “register {username} {licensePlateNumber}”
- //Unregister: “unregister {username}”
- //The input will always be valid and you do not need to check it explicitly.
- //Examples
- //Input
- //Output
- //5
- //register some0ne CS1234JS
- //register vankata JAVA123S
- //register vankata AB4142CD
- //register housey VR1223EE
- //unregister housey
- //some0ne registered CS1234JS successfully
- //ERROR: invalid license plate JAVA123S
- //vankata registered AB4142CD successfully
- //housey registered VR1223EE successfully
- //user housey unregistered successfully
- //some0ne => CS1234JS
- //vankata => AB4142CD
- //4
- //register testUser AA4132BB
- //register testuser AA4132BB
- //register testuser AA9999BB
- //unregister testUser
- //testUser registered AA4132BB successfully
- //ERROR: license plate AA4132BB is busy
- //testuser registered AA9999BB successfully
- //user testUser unregistered successfully
- //testuser => AA9999BB
- //7
- //register gosho mm1111XX
- //register gosho MM1111xx
- //register gosho MMaaaaXX
- //unregister gosho
- //register gosho MM1111XX
- //unregister gosho
- //unregister pesho
- //ERROR: invalid license plate mm1111XX
- //ERROR: invalid license plate MM1111xx
- //ERROR: invalid license plate MMaaaaXX
- //ERROR: user gosho not found
- //gosho registered MM1111XX successfully
- //user gosho unregistered successfully
- //ERROR: user pesho not found
- //Problem 7.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement