Advertisement
osman1997

03. problem Final exam dec 13 2020

Dec 13th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. class Program
  8. {
  9.  
  10. static void Main()
  11. {
  12.  
  13. int macCapacity = int.Parse(Console.ReadLine());
  14.  
  15. Dictionary<string, Dictionary<string, int>> users = new Dictionary<string, Dictionary<string, int>>();
  16.  
  17. string command = Console.ReadLine();
  18.  
  19. while (command != "Statistics")
  20. {
  21. string[] line = command.Split("=");
  22. string cmd = line[0];
  23.  
  24. if (cmd == "Add")
  25. {
  26. string userName = line[1];
  27. int sendMessages = int.Parse(line[2]);
  28. int receivedMessages = int.Parse(line[3]);
  29. if (!users.ContainsKey(userName))
  30. {
  31. users.Add(userName, new Dictionary<string, int>()
  32. {
  33. {"send", sendMessages },
  34. {"received", receivedMessages }
  35. });
  36. }
  37. }
  38.  
  39. else if (cmd == "Message")
  40. {
  41. string sender = line[1];
  42. string receiver = line[2];
  43.  
  44. if (users.ContainsKey(sender) && users.ContainsKey(receiver))
  45. {
  46. users[sender]["send"] += 1;
  47. users[receiver]["received"] += 1;
  48.  
  49.  
  50. if (users[sender]["send"] + users[sender]["received"] >= macCapacity)
  51. {
  52. Console.WriteLine($"{sender} reached the capacity!");
  53. users.Remove(sender);
  54.  
  55. }
  56. if (users[receiver]["received"] + users[receiver]["send"] >= macCapacity)
  57. {
  58. Console.WriteLine($"{receiver} reached the capacity!");
  59. users.Remove(receiver);
  60. }
  61. }
  62. }
  63.  
  64. else if (cmd == "Empty")
  65. {
  66. if (line[1] == "All")
  67. {
  68. foreach (var item in users)
  69. {
  70. users.Remove(item.Key);
  71. }
  72. }
  73. else
  74. {
  75. string user = line[1];
  76.  
  77. users.Remove(user);
  78. }
  79. }
  80.  
  81.  
  82.  
  83.  
  84. command = Console.ReadLine();
  85. }
  86.  
  87. foreach (var item in users)
  88. {
  89. if (item.Value["send"] == 0 && item.Value["received"] == 0)
  90. {
  91. users.Remove(item.Key);
  92. }
  93. }
  94.  
  95. users = users
  96. .OrderByDescending(s => s.Value["received"])
  97. .ThenBy(s => s.Key)
  98. .ToDictionary(s => s.Key, x => x.Value);
  99.  
  100. Console.WriteLine($"Users count: {users.Count}");
  101.  
  102. foreach (var item in users)
  103. {
  104. int totalPoints = item.Value["send"] + item.Value["received"];
  105. string userName = item.Key;
  106.  
  107. Console.WriteLine($"{userName} - {totalPoints}");
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement