Deserboy

Messages Manager

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