JulianJulianov

10.AssociativeArray-SoftUni Parking

Mar 15th, 2020
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 KB | None | 0 0
  1. 10.SoftUni Parking
  2. SoftUni just got a new parking lot. It’s so fancy, it even has online parking validation. Except the online service doesn’t work. It can only receive users’ data, but it doesn’t know what to do with it. Good thing you’re on the dev team and know how to fix it, right?
  3. Write a program, which validates a parking place for an online service. Users can register to park and unregister to leave.
  4. The program receives 2 commands:   
  5. "register {username} {licensePlateNumber}":
  6. o   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:
  7. "ERROR: already registered with plate number {licensePlateNumber}"
  8. o   If the aforementioned checks passes successfully, the plate can be registered, so the system should print:
  9.  "{username} registered {licensePlateNumber} successfully"
  10. "unregister {username}":
  11. o   If the user is not present in the database, the system should print:
  12. "ERROR: user {username} not found"
  13. o   If the aforementioned check passes successfully, the system should print:
  14. "{username} unregistered successfully"
  15. After you execute all of the commands, print all the currently registered users and their license plates in the format:
  16. "{username} => {licensePlateNumber}"
  17. Input
  18. • First line: n – number of commands – integer
  19. • Next n lines: commands in one of the two possible formats:
  20. o   Register: "register {username} {licensePlateNumber}"
  21. o   Unregister: "unregister {username}"
  22. The input will always be valid and you do not need to check it explicitly.
  23. Examples
  24. Input                                                              Output
  25. 5
  26. register John CS1234JS                                             John registered CS1234JS successfully
  27. register George JAVA123S                                           George registered JAVA123S successfully
  28. register Andy AB4142CD                                             Andy registered AB4142CD successfully
  29. register Jesica VR1223EE                                           Jesica registered VR1223EE successfully
  30. unregister Andy                                                    Andy unregistered successfully
  31.                                                                    John => CS1234JS
  32.                                                                    George => JAVA123S
  33.                                                                    Jesica => VR1223EE
  34.  
  35.  
  36. 4
  37. register Jony AA4132BB                                             Jony registered AA4132BB successfully
  38. register Jony AA4132BB                                             ERROR: already registered with plate number AA4132BB
  39. register Linda AA9999BB                                            Linda registered AA9999BB successfully
  40. unregister Jony                                                    Jony unregistered successfully
  41.                                                                    Linda => AA9999BB
  42.  
  43.  
  44.  
  45. 6
  46. register Jacob MM1111XX                                            Jacob registered MM1111XX successfully
  47. register Anthony AB1111XX                                          Anthony registered AB1111XX successfully
  48. unregister Jacob                                                   Jacob unregistered successfully
  49. register Joshua DD1111XX                                           Joshua registered DD1111XX successfully
  50. unregister Lily                                                    ERROR: user Lily not found
  51. register Samantha AA9999BB                                         Samantha registered AA9999BB successfully
  52.                                                                    Joshua => DD1111XX
  53.                                                                    Anthony => AB1111XX
  54.                                                                    Samantha => AA9999BB
  55.  
  56. using System;
  57. using System.Collections.Generic;
  58. using System.Linq;
  59.  
  60. namespace _04Orders
  61. {
  62.     class Program
  63.     {
  64.         static void Main(string[] args)
  65.         {
  66.             var numberCommands = int.Parse(Console.ReadLine());
  67.             var username = new Dictionary<string, string>();
  68.  
  69.             for (int i = 1; i <= numberCommands; i++)
  70.             {
  71.                 var command = Console.ReadLine().Split();
  72.                 if (command[0] == "register")
  73.                 {
  74.                     if (username.ContainsKey(command[1]) == false)
  75.                     {
  76.                         username.Add(command[1], command[2]);
  77.                         Console.WriteLine($"{command[1]} registered {username[command[1]]} successfully");
  78.                     }                                                 //Или command[2]
  79.                     else
  80.                     {
  81.                         Console.WriteLine($"ERROR: already registered with plate number {command[2]}");
  82.                     }                                                                  //Или username[command[1]]
  83.                 }
  84.                 else
  85.                 {
  86.                     if (username.ContainsKey(command[1]) == false)
  87.                     {
  88.                         Console.WriteLine($"ERROR: user {command[1]} not found");
  89.                     }
  90.                     else
  91.                     {
  92.                         username.Remove(command[1]);
  93.                         Console.WriteLine($"{command[1]} unregistered successfully");
  94.                     }
  95.                 }
  96.             }
  97.             foreach (var item in username)
  98.             {
  99.                 Console.WriteLine($"{item.Key} => {item.Value}");
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment