Advertisement
FrauCules

02. Friendlist Maintenance

Feb 28th, 2020
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Friendlist_Maintenance
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> list = Console.ReadLine().Split(", ").ToList();
  12.  
  13.             List<string> list1 = new List<string>();
  14.             List<string> command = new List<string>();
  15.             command.Add("");
  16.             int numBL = 0;
  17.             int numLost = 0;
  18.             bool flag = true;
  19.  
  20.             while (command[0] != "Report")
  21.             {
  22.                 command = Console.ReadLine()
  23.                     .Split()
  24.                     .ToList();
  25.                 if(command[0] == "Report")
  26.                 {
  27.                     break;
  28.                 }
  29.  
  30.                 switch (command[0])
  31.                 {
  32.                     case "Blacklist":
  33.                         list1 = list;
  34.                         list = Blacklist(list, command[1]);
  35.                         flag = list1.SequenceEqual(list); // Има ли промяна в листа
  36.                         if (!flag)
  37.                         {
  38.                             numBL++; // ако има промяна, увеличавам брояча
  39.                         }
  40.                         break;
  41.                     case "Error":
  42.                         list1 = list;
  43.                         list = Error(list, command[1]);
  44.                         flag = list1.SequenceEqual(list); // Има ли промяна в листа
  45.                         if (!flag)
  46.                         {
  47.                             numLost++;  // ако има промяна, увеличавам брояча
  48.                         }
  49.                         break;
  50.                     case "Change":
  51.                         list = Change(list, command[1], command[2]);
  52.                         break;
  53.                 }
  54.             }
  55.             Console.WriteLine($"Blacklisted names: {numBL}");
  56.             Console.WriteLine($"Lost names: {numLost}");
  57.             Console.WriteLine(string.Join(" ",list));
  58.         }
  59.  
  60.         private static List<string> Change(List<string> arr, string v1, string name)
  61.         {
  62.             int index = int.Parse(v1);
  63.            
  64.             if (index >= 0 && index < arr.Count)
  65.             {
  66.                 Console.WriteLine($"{arr[index]} changed his username to {name}.");
  67.                 arr[index] = name;
  68.             }
  69.             return arr;
  70.         }
  71.  
  72.         private static List<string> Error(List<string> arr, string v)
  73.         {
  74.             int index = int.Parse(v);
  75.             List<string> arrNew = new List<string>();
  76.  
  77.             for (int i = 0; i < arr.Count; i++)
  78.                 {
  79.                     arrNew.Add(arr[i]);
  80.                 }
  81.  
  82.             if (index >= 0 && index < arr.Count)
  83.             {
  84.                 if (arr[index] != "Blacklisted" && arr[index] != "Lost")
  85.                 {
  86.                     Console.WriteLine($"{arr[index]} was lost due to an error.");
  87.                     arrNew[index] = "Lost";
  88.                 }
  89.             }
  90.             return arrNew;
  91.         }
  92.  
  93.         private static List<string> Blacklist(List<string> arr, string name)
  94.         {
  95.             List<string> arrNew = new List<string>();
  96.  
  97.             for (int i = 0; i < arr.Count; i++)
  98.             {
  99.                 arrNew.Add(arr[i]);
  100.             }
  101.  
  102.             if (arr.Contains(name))
  103.             {
  104.                 int index = arr.IndexOf(name);
  105.                 Console.WriteLine($"{name} was blacklisted.");
  106.                 arrNew[index] = "Blacklisted";
  107.             }
  108.             else
  109.             {
  110.                 Console.WriteLine($"{name} was not found.");
  111.             }
  112.             return arrNew;
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement