Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _03.MessageManager
- {
- class Program
- {
- static void Main(string[] args)
- {
- int capacity = int.Parse(Console.ReadLine());
- List<string> input = Console.ReadLine().Split("=").ToList();
- string command = input[0];
- Dictionary<string, int> sent = new Dictionary<string, int>();
- Dictionary<string, int> received = new Dictionary<string, int>();
- while (command != "Statistics")
- {
- if (command == "Add")
- {
- string username = input[1];
- int currentSent = int.Parse(input[2]);
- int currentReceived = int.Parse(input[3]);
- if(!sent.ContainsKey(username))
- {
- sent.Add(username, currentSent);
- received.Add(username, currentReceived);
- }
- }
- else if (command == "Message")
- {
- string sender = input[1];
- string receiver = input[2];
- if(sent.ContainsKey(sender) && received.ContainsKey(receiver))
- {
- sent[sender]++;
- received[receiver]++;
- }
- if ((sent[sender] + received[sender]) >= capacity)
- {
- sent.Remove(sender);
- received.Remove(sender);
- Console.WriteLine($"{sender} reached the capacity!");
- }
- if((received[receiver] + sent[receiver]) >= capacity)
- {
- received.Remove(receiver);
- sent.Remove(receiver);
- Console.WriteLine($"{receiver} reached the capacity!");
- }
- }
- else if (command == "Empty")
- {
- string username = input[1];
- if (sent.ContainsKey(username))
- {
- sent.Remove(username);
- received.Remove(username);
- }
- if (username == "All")
- {
- sent.Clear();
- received.Clear();
- }
- }
- input = Console.ReadLine().Split("=").ToList();
- command = input[0];
- }
- Console.WriteLine($"Users count: {sent.Count}");
- received = received.OrderByDescending(x => x.Value).ThenBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);
- foreach (var item in received)
- {
- Console.WriteLine($"{item.Key} - {item.Value+sent[item.Key]}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement