JulianJulianov

13.AsociativeArrays-Company Users

Apr 12th, 2020
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. 13. Company Users
  2. Write a program that keeps information about companies and their employees.
  3. You will be receiving a company name and an employee's id, until you receive the command "End" command. Add each employee to the given company. Keep in mind that a company cannot have two employees with the same id.
  4. When you finish reading the data, order the companies by the name in ascending order.
  5. Print the company name and each employee's id in the following format:
  6. {companyName}
  7. -- {id1}
  8. -- {id2}
  9. -- {idN}
  10. Input / Constraints
  11. • Until you receive the "End" command, you will be receiving input in the format: "{companyName} -> {employeeId}".
  12. • The input always will be valid.
  13. Examples
  14. Input                     Output
  15. SoftUni -> AA12345        HP
  16. SoftUni -> BB12345        -- BB12345
  17. Microsoft -> CC12345      Microsoft
  18. HP -> BB12345             -- CC12345
  19. End                       SoftUni
  20.                           -- AA12345
  21.                           -- BB12345
  22.  
  23. SoftUni -> AA12345        Lenovo
  24. SoftUni -> CC12344        -- XX23456
  25. Lenovo -> XX23456         Movement
  26. SoftUni -> AA12345        -- DD11111
  27. Movement -> DD11111       SoftUni
  28. End                       -- AA12345
  29.                           -- CC12344
  30.  
  31.  
  32. namespace _13CompanyUsers
  33. {
  34.     using System;
  35.     using System.Collections.Generic;
  36.     using System.Linq;
  37.  
  38.     class Program
  39.     {
  40.         static void Main()
  41.         {
  42.             var companies = new Dictionary<string, List<string>>();
  43.  
  44.             var inputLine = Console.ReadLine();
  45.  
  46.             while (inputLine != "End")
  47.             {
  48.                 var inputArray = inputLine.Split(" -> ");
  49.                 var companyName = inputArray[0];
  50.                 var employeeID = inputArray[1];
  51.  
  52.                 if (!companies.ContainsKey(companyName))
  53.                 {
  54.                     companies.Add(companyName, new List<string>());
  55.                 }
  56.  
  57.                 if (!companies[companyName].Contains(employeeID))
  58.                 {
  59.                     companies[companyName].Add(employeeID);
  60.                 }
  61.  
  62.                 inputLine = Console.ReadLine();
  63.             }
  64.  
  65.  
  66.             foreach (var item in companies.OrderBy(x => x.Key))
  67.             {
  68.                 Console.WriteLine($"{item.Key}");
  69.  
  70.                 foreach (var employeeID in item.Value)
  71.                 {
  72.                     Console.WriteLine($"-- {employeeID}");
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment