Vapio

task29

Apr 27th, 2021 (edited)
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.     public static void Main()
  7.     {
  8.         PlayersController controller = new PlayersController();
  9.         controller.Add("Aboba", 54, false);
  10.         controller.Add("Adoba", 24, false);
  11.         controller.Add("boba", 14, false);
  12.         controller.Add("Abofa", 14, false);
  13.         Console.WriteLine(controller);
  14.  
  15.         controller.Ban(2);
  16.         Console.WriteLine(controller);
  17.  
  18.         controller.Remove(1);
  19.         controller.Add("Abofda", 131, false);
  20.         controller.Add("Aboda", 11, false);
  21.         controller.Ban(2);
  22.         Console.WriteLine(controller);
  23.     }
  24. }
  25.  
  26. public class PlayersController
  27. {
  28.     private List<Player> _players;
  29.     private int _id;
  30.  
  31.     public PlayersController()
  32.     {
  33.         _players = new List<Player>();
  34.         _id = 0;
  35.     }
  36.  
  37.     private bool SearchElement(int id, out int index)
  38.     {
  39.         index = 0;
  40.         for (int i = 0; i < _players.Count; ++i)
  41.         {
  42.             if (_players[i].Id == id)
  43.             {
  44.                 index = i;
  45.                 return true;
  46.             }
  47.         }
  48.         return false;
  49.     }
  50.  
  51.     public void Add(string name, int level, bool isBanned)
  52.     {
  53.         Player player = new Player(_id, name, level, isBanned);
  54.         Add(player);
  55.     }
  56.  
  57.     public void Add(Player player)
  58.     {
  59.         _players.Add(player);
  60.         ++_id;
  61.     }
  62.  
  63.     public void Remove(int id)
  64.     {
  65.         int index;
  66.         if(SearchElement(id, out index))
  67.             _players.RemoveAt(index);
  68.     }
  69.  
  70.     public void Ban(int id)
  71.     {
  72.         int index;
  73.         if (SearchElement(id, out index))
  74.             _players[index].Ban();
  75.     }
  76.  
  77.     public void Unban(int id)
  78.     {
  79.         int index;
  80.         if (SearchElement(id, out index))
  81.             _players[index].Unban();
  82.     }
  83.  
  84.     public override string ToString()
  85.     {
  86.         string output = "";
  87.  
  88.         foreach (Player player in _players)
  89.             output += String.Format("{0}. {1} - {2} {3}\n",
  90.                                     player.Id, player.Name, player.Level, player.IsBanned);
  91.         return output;
  92.     }
  93. }
  94.  
  95. public class Player
  96. {
  97.     public int Id { get; private set; }
  98.     public string Name { get; private set; }
  99.     public int Level { get; private set; }
  100.     public bool IsBanned { get; private set; }
  101.  
  102.     public Player(int id, string name, int level, bool isBanned)
  103.     {
  104.         Id = id;
  105.         Name = name;
  106.         Level = level;
  107.         IsBanned = isBanned;
  108.     }
  109.  
  110.     public void Ban()
  111.     {
  112.         IsBanned = true;
  113.     }
  114.  
  115.     public void Unban()
  116.     {
  117.         IsBanned = false;
  118.     }
  119. }
  120.  
Add Comment
Please, Sign In to add comment