Advertisement
ralichka

Untitled

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