LardaX

User_Logs

Oct 8th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace User_Logs
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             SortedDictionary<string, Dictionary<string, int>> userLogs = new SortedDictionary<string, Dictionary<string, int>>();
  14.  
  15.             char[] splitters = { ' ', '=' };
  16.  
  17.             string[] commandLine = Console.ReadLine().Split(splitters, StringSplitOptions.RemoveEmptyEntries).Where(entry => !entry.Equals("IP") && !entry.Equals("message") && !entry.Equals("user")).ToArray();
  18.  
  19.             while (!commandLine[0].Equals("end"))
  20.             {
  21.                 string ip = commandLine[0];
  22.                 string userName = commandLine[2];
  23.  
  24.                 InsertUser(userLogs, userName);
  25.                 InsertIps(userLogs, userName, ip);
  26.  
  27.                 commandLine = Console.ReadLine().Split(splitters, StringSplitOptions.RemoveEmptyEntries).Where(entry => !entry.Equals("IP") && !entry.Equals("message") && !entry.Equals("user")).ToArray();
  28.             }
  29.  
  30.             PrintUserLogs(userLogs);
  31.  
  32.         }
  33.  
  34.         private static void PrintUserLogs(SortedDictionary<string, Dictionary<string, int>> userLogs)
  35.         {
  36.             foreach (KeyValuePair<string, Dictionary<string, int>> userEntry in userLogs)
  37.             {
  38.                 Console.WriteLine(userEntry.Key + ": ");
  39.                 string ipString = "";
  40.                 foreach (KeyValuePair<string, int> ipInformation in userEntry.Value)
  41.                 {
  42.                     ipString += $"{ipInformation.Key} => {ipInformation.Value}, ";
  43.                 }
  44.                 ipString = ipString.Substring(0, ipString.Length - 2);
  45.                 ipString += ".";
  46.                 Console.WriteLine(ipString);
  47.             }
  48.         }
  49.  
  50.         private static void InsertIps(SortedDictionary<string, Dictionary<string, int>> userLogs, string userName, string ip)
  51.         {
  52.             if (!userLogs[userName].ContainsKey(ip))
  53.             {
  54.                 userLogs[userName].Add(ip, 0);
  55.             }
  56.  
  57.             userLogs[userName][ip]++;
  58.         }
  59.  
  60.         private static void InsertUser(SortedDictionary<string, Dictionary<string, int>> userLogs, string userName)
  61.         {
  62.             if (!userLogs.ContainsKey(userName))
  63.             {
  64.                 userLogs.Add(userName, new Dictionary<string, int>());
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment