YORDAN2347

07. Vloggers - SoftUni

Feb 13th, 2021 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _07._The_V_Logger
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             HashSet<Vlogger> vloggers = new HashSet<Vlogger>();
  12.  
  13.             string input = Console.ReadLine();
  14.             while (input != "Statistics")
  15.             {
  16.                 string[] commandParts = input.Split(" ",
  17.                     StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.                 string name = commandParts[0];
  20.                 string command = commandParts[1];
  21.  
  22.                 switch (command)
  23.                 {
  24.                     case "joined":
  25.                         bool isExists = IsVloggerExists(vloggers, name);                      
  26.                         if (isExists)
  27.                         {
  28.                             break;
  29.                         }
  30.                         else
  31.                         {
  32.                             vloggers.Add(new Vlogger(name));
  33.                         }
  34.                         break;
  35.  
  36.                     case "followed":
  37.                         string followed = commandParts[2];
  38.                         bool isVloggerExists = IsVloggerExists(vloggers, name);
  39.                         bool isFollowedExists = IsVloggerExists(vloggers, followed);
  40.                      
  41.                         bool isFollowing = IsFollowing(vloggers, name, followed);
  42.  
  43.                         if (isVloggerExists
  44.                             && isFollowedExists
  45.                             && name != followed
  46.                             && isFollowing == false)
  47.                         {
  48.                             foreach (var vlogger in vloggers)
  49.                             {
  50.                                 if (vlogger.Name == name)
  51.                                 {
  52.                                     vlogger.Following.Add(followed);
  53.                                    
  54.                                 }
  55.                                 if (vlogger.Name == followed)
  56.                                 {
  57.                                     vlogger.Followers.Add(name);
  58.                                 }
  59.                             }
  60.                         }
  61.                         break;
  62.                 }
  63.  
  64.                 input = Console.ReadLine();
  65.             }
  66.  
  67.             Console.WriteLine($"The V-Logger has a total of {vloggers.Count} vloggers in its logs.");
  68.             int counter = 1;
  69.             foreach (var vlogger in vloggers
  70.                 .OrderByDescending(vlogger => vlogger.Followers.Count)
  71.                 .ThenBy(vlogger => vlogger.Following.Count))
  72.             {
  73.                 Console.WriteLine($"{counter}. {vlogger.Name} : " +
  74.                     $"{vlogger.Followers.Count} followers, " +
  75.                     $"{vlogger.Following.Count} following");
  76.                 if (counter == 1)
  77.                 {
  78.                     foreach (var follower in vlogger.Followers.OrderBy(x => x))
  79.                     {
  80.                         Console.WriteLine($"*  {follower}");
  81.                     }
  82.                 }
  83.                 counter++;
  84.             }
  85.         }
  86.  
  87.         private static bool IsFollowing(HashSet<Vlogger> vloggers, string name, string followed)
  88.         {
  89.             foreach (var vlogger in vloggers)
  90.             {
  91.                 if (vlogger.Name == name)
  92.                 {
  93.                     if (vlogger.Following.Contains(followed))
  94.                     {
  95.                         return true;
  96.                     }
  97.                 }
  98.             }
  99.             return false;
  100.         }
  101.  
  102.         private static bool IsVloggerExists(HashSet<Vlogger> vloggers, string name)
  103.         {
  104.             bool isExists = false;
  105.             foreach (var vlogger in vloggers)
  106.             {
  107.                 if (vlogger.Name == name)
  108.                 {
  109.                     isExists = true;
  110.                     break;
  111.                 }
  112.             }
  113.             return isExists;
  114.         }
  115.     }
  116.  
  117.     public class Vlogger
  118.     {
  119.         public Vlogger(string name)
  120.         {
  121.             Name = name;
  122.             Followers = new HashSet<string>();
  123.             Following = new HashSet<string>();
  124.         }
  125.         public string Name { get; set; }
  126.         public HashSet<string> Followers { get; set; }
  127.         public HashSet<string> Following { get; set; }
  128.     }
  129. }
  130.  
Add Comment
Please, Sign In to add comment