JulianJulianov

Final Exam - 07 December 2019 Group 1 - Inbox Manager

May 9th, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 KB | None | 0 0
  1. 03.Inbox Manager
  2. Create a program that manages users and Emails sent by users. You need to keep information about their username and their sent Emails. The Emails are represented as strings. You will be receiving lines with commands separated by "->" until you receive the "Statistics" command.  There are three possible commands:
  3. "Add->{username}":
  4. o   Check if the username exists and if it does print:
  5. "{username} is already registered"
  6.  If it doesn’t exist, then add the user to the collection of users.
  7. "Send->{username}->{Email}"
  8. o   Add the {Email} to the {username}'s collection of sent Emails.
  9. • "Delete->{username}":
  10. o   Delete the given user, if he exists. If the user doesn’t exist, print:
  11. "{username} not found!"
  12. In the end, you have to print the count of users, each user with his/her Emails. Users need to be sorted in descending order by the count of sent Emails and then by their username in ascending order in the following format:  
  13. Users count: {count}
  14. {username}
  15. - {Email1}
  16. - {Email2}
  17. - {Emailn}
  18. Input
  19. • You will be receiving lines until you receive the "Statistics" command.
  20. • The commands will be in the format described above.
  21. Output
  22. • Print the collection in the format described above after the "Statistics" command.
  23.  
  24. Examples
  25. Input                                                       Output
  26. Add->Mike                                                   Users count: 2
  27. Add->George                                                 George
  28. Send->George->Hello World                                    - Hello World
  29. Send->George->Some random test mail                          - Some random test mail
  30. Send->Mike->Hello, do you want to meet up tomorrow?          - It would be a pleasure
  31. Send->George->It would be a pleasure                        Mike
  32. Send->Mike->Another random test mail                         - Hello, do you want to meet up tomorrow?
  33. Statistics                                                   - Another random test mail
  34.    
  35. Comments
  36. First we receive our users. Since they are not already in our collection, we add them. Then they start sending emails and in the end we print the output in the described format.
  37.  
  38. Add->Mike
  39. Add->George
  40. Send->George->Hello World
  41. Send->George->Your loan is overdue
  42. Add->Mike                                                    Mike is already registered
  43. Send->Mike->Hello, do you want to meet up tomorrow?
  44. Delete->Peter                                                Peter not found!
  45. Send->George->I'm busy                                       Users count: 1
  46. Send->Mike->Another random test mail                         Mike
  47. Delete->George                                                - Hello, do you want to meet up tomorrow?
  48. Statistics                                                    - Another random test mail
  49.  
  50. using System;
  51. using System.Collections.Generic;
  52. using System.Linq;
  53.  
  54. public class Program
  55. {
  56.     public static void Main()
  57.     {
  58.           var listEmails = new Dictionary<string, List<string>>();
  59.             var input = "";
  60.             while ((input = Console.ReadLine()) != "Statistics")
  61.             {
  62.                 var command = input.Split("->");
  63.                 var userName = command[1];
  64.  
  65.                 switch (command[0])
  66.                 {
  67.                     case "Add":
  68.                         if (!listEmails.ContainsKey(userName))
  69.                         {
  70.                             listEmails.Add(userName, new List<string>());
  71.                         }
  72.                         else
  73.                         {
  74.                             Console.WriteLine($"{userName} is already registered");
  75.                         }
  76.                         break;
  77.                     case "Send":
  78.                       //var email = $" - {command[2]}";
  79.                         var email = command[2];
  80.                             listEmails[userName].Add(email);
  81.                         break;
  82.                     case "Delete":
  83.                         if (listEmails.ContainsKey(userName))
  84.                         {
  85.                             listEmails.Remove(userName);
  86.                         }
  87.                         else
  88.                         {
  89.                             Console.WriteLine($"{userName} not found!");
  90.                         }
  91.                         break;
  92.                 }
  93.             }
  94.             Console.WriteLine($"Users count: {listEmails.Count}");
  95.             if (listEmails.Count == 0)
  96.             {
  97.                 return;
  98.             }
  99.             listEmails = listEmails.OrderByDescending(x => x.Value.Count).ThenBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);
  100.             foreach (var infoData in listEmails/*.OrderByDescending(x => x.Value.Count).ThenBy(x => x.Key)*/)
  101.             {
  102.                 Console.WriteLine(infoData.Key);
  103.                 foreach(var email in infoData.Value)
  104.                 {
  105.                     Console.WriteLine($" - {email}");
  106.                 }
  107.               //Console.WriteLine(string.Join("\n", infoData.Value));
  108.             }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment