petyaminkova91

Untitled

Dec 7th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03_Followers
  6. {
  7. class UserReport
  8. {
  9. public UserReport(int likes = 0, int comments = 0)
  10. {
  11. Likes = likes;
  12. Comments = comments;
  13. }
  14. public int Likes { get; set; }
  15.  
  16. public int Comments { get; set; }
  17.  
  18. public int Sum => this.Likes + this.Comments;
  19. }
  20. class Program
  21. {
  22. static void Main(string[] args)
  23. {
  24. Dictionary<string, UserReport> users = new Dictionary<string, UserReport>();
  25. string input = Console.ReadLine();
  26.  
  27. while (input != "Log out")
  28. {
  29. string[] commandArgs = input.Split(": ");
  30.  
  31. string command = commandArgs[0];
  32. string username = commandArgs[1];
  33.  
  34. if (command == "New follower")
  35. {
  36. AddNewUserIfIsNotInTheList(users, username);
  37. }
  38. else if (command == "Like")
  39. {
  40. int likesCount = int.Parse(commandArgs[2]);
  41.  
  42. AddNewUserIfIsNotInTheList(users, username);
  43. users[username].Likes += likesCount;
  44. }
  45. else if (command == "Comment")
  46. {
  47. AddNewUserIfIsNotInTheList(users, username);
  48. users[username].Comments++;
  49. }
  50. else if (command == "Blocked")
  51. {
  52. BlockUserIfExist(users, username);
  53. }
  54.  
  55. input = Console.ReadLine();
  56. }
  57.  
  58. PrintAllFollowers(users);
  59. }
  60.  
  61. private static void BlockUserIfExist(Dictionary<string, UserReport> users, string username)
  62. {
  63. if (users.ContainsKey(username))
  64. {
  65. users.Remove(username);
  66. }
  67. else
  68. {
  69. Console.WriteLine($"{username} doesn't exist.");
  70. }
  71. }
  72.  
  73. private static void PrintAllFollowers(Dictionary<string, UserReport> users)
  74. {
  75. Console.WriteLine($"{users.Keys.Count} followers");
  76.  
  77. foreach (var user in users.OrderByDescending(u => u.Value.Likes).ThenBy(u => u.Key))
  78. {
  79. Console.WriteLine($"{user.Key}: {user.Value.Sum}");
  80. }
  81. }
  82.  
  83. private static void AddNewUserIfIsNotInTheList(Dictionary<string, UserReport> users, string username)
  84. {
  85. if (!users.ContainsKey(username))
  86. {
  87. users.Add(username, new UserReport());
  88. }
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment