Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Messages_Manager
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int capacity = int.Parse(Console.ReadLine());
  12.  
  13.             Dictionary<string, List<int>> users = new Dictionary<string, List<int>>();
  14.  
  15.             string command = Console.ReadLine();
  16.  
  17.             while (command != "Statistics")
  18.             {
  19.                 string[] cmd = command.Split("=", StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 if (cmd[0] == "Add")
  22.                 {
  23.                     if (!users.ContainsKey(cmd[1]))
  24.                     {
  25.                         users.Add(cmd[1], new List<int>());
  26.                         users[cmd[1]].Add(int.Parse(cmd[2]));
  27.                         users[cmd[1]].Add(int.Parse(cmd[3]));
  28.                     }
  29.                 }
  30.                 if (cmd[0] == "Message")
  31.                 {
  32.                     string firstUser = cmd[1];
  33.                     string secndUser = cmd[2];
  34.  
  35.                     if (users.ContainsKey(firstUser) && users.ContainsKey(secndUser))
  36.                     {
  37.                         users[firstUser][0]++;
  38.                         users[secndUser][1]++;
  39.                     }
  40.                     if (users[firstUser].Sum()>=capacity)
  41.                     {
  42.                         users.Remove(firstUser);
  43.                         Console.WriteLine($"{firstUser} reached the capacity!");
  44.                     }
  45.                     if (users[secndUser].Sum()>=capacity)
  46.                     {
  47.                         users.Remove(secndUser);
  48.                         Console.WriteLine($"{secndUser} reached the capacity!");
  49.                     }
  50.                 }
  51.                 if (cmd[0] == "Empty")
  52.                 {
  53.                     if (cmd[1]=="All")
  54.                     {
  55.                         users.Clear();
  56.                     }
  57.                     else
  58.                     {
  59.                         if (users.ContainsKey(cmd[1]))
  60.                         {
  61.                             users.Remove(cmd[1]);
  62.                         }
  63.                     }
  64.                 }
  65.  
  66.                 command = Console.ReadLine();
  67.             }
  68.  
  69.             Console.WriteLine($"Users count: {users.Count}");
  70.             foreach (var user in users.OrderByDescending(u=>u.Value[1]).ThenBy(x=>x.Key))
  71.             {
  72.                 int sum = user.Value.Sum();
  73.                 Console.WriteLine($"{user.Key} - {sum}");
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement