BSO9009

Untitled

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