anizko

Messages Manager

Aug 7th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace problem03
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, List<int>> dict = new Dictionary<string, List<int>>();
  12.             int capacity = int.Parse(Console.ReadLine());
  13.             string comands = Console.ReadLine();
  14.  
  15.             while (comands != "Statistics")
  16.             {
  17.                 string[] comand = comands.Split('=');
  18.                 if (comand[0] == "Add")
  19.                 {
  20.                     string username = comand[1];
  21.                     int sent = int.Parse(comand[2]);
  22.                     int received = int.Parse(comand[3]);
  23.                     int allMessages = sent + received;
  24.                     if (!dict.ContainsKey(username))
  25.                     {
  26.                         dict[username] = new List<int>();
  27.                         dict[username].Add(allMessages);
  28.                         dict[username].Add(received);
  29.                     }
  30.                 }
  31.                 else if (comand[0] == "Message")
  32.                 {
  33.                     string sender = comand[1];
  34.                     string receiver = comand[2];
  35.                     if (dict.ContainsKey(sender) && dict.ContainsKey(receiver))
  36.                     {
  37.                         dict[sender][0]++;
  38.                         dict[receiver][0]++;
  39.                         dict[receiver][1]++;
  40.                         if (dict[sender][0] >= capacity)
  41.                         {
  42.                             Console.WriteLine($"{sender} reached the capacity!");
  43.                             dict.Remove(sender);
  44.                         }
  45.                         if (dict[receiver][0] >= capacity)
  46.                         {
  47.                             Console.WriteLine($"{receiver} reached the capacity!");
  48.                             dict.Remove(receiver);
  49.                         }
  50.                     }
  51.  
  52.                 }
  53.                 else if (comand[0] == "Empty")
  54.                 {
  55.                     if (comand[1] == "All")
  56.                     {
  57.                         dict = new Dictionary<string, List<int>>();
  58.                     }
  59.                     else
  60.                     {
  61.                         string username = comand[1];
  62.                         if (dict.ContainsKey(username))
  63.                         {
  64.                             dict.Remove(username);
  65.                         }
  66.                     }
  67.                 }
  68.                 comands = Console.ReadLine();
  69.             }
  70.  
  71.             var countUserNames = dict.Keys.Count;
  72.             Console.WriteLine($"Users count: {countUserNames}");
  73.  
  74.             var result = dict.OrderByDescending(x => x.Value[1]).ThenBy(x => x.Key);
  75.             foreach (var item in result)
  76.             {
  77.                 Console.WriteLine($"{item.Key} - {item.Value[0]}");
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment