Advertisement
GPbl3YH

CSLight #30

Feb 5th, 2021 (edited)
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 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 CSLight
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Player player1 = new Player("GPbl3YH", 10, false);
  14.             Player player2 = new Player("Neo0411", 11, false);
  15.             Player player3 = new Player("Artist", 11, false);
  16.             List<Player> players = new List<Player>() { player1, player2 };
  17.             Database database = new Database(players);
  18.             database.AddPlayer(player3);
  19.             database.ShowPlayers();
  20.             Console.ReadKey();
  21.         }
  22.     }
  23.  
  24.     class Database
  25.     {
  26.         private List<Player> _players = new List<Player>();
  27.         public Database(List<Player> players)
  28.         {
  29.             foreach (var player in players)
  30.             {
  31.                 _players.Add(player);
  32.             }
  33.         }
  34.  
  35.         public void AddPlayer(Player player)
  36.         {
  37.             _players.Add(player);
  38.         }
  39.  
  40.         public void DeletePlayer(int id)
  41.         {
  42.             Player player = GetPlayer(id);
  43.             if (player != null)
  44.             {
  45.                 _players.RemoveAt(player.Id);
  46.             }
  47.             else
  48.             {
  49.                 Console.WriteLine("Пользователь не найден");
  50.             }
  51.         }
  52.  
  53.         private Player GetPlayer(int id)
  54.         {
  55.             if (id >= 0 && id < _players.Count)
  56.             {
  57.                 return _players[id];
  58.             }
  59.             else
  60.             {
  61.                 return null;
  62.             }
  63.         }
  64.  
  65.         public void BanPlayer(int id)
  66.         {
  67.             Player player = GetPlayer(id);
  68.             if (player != null)
  69.             {
  70.                 player.Ban();
  71.             }
  72.             else
  73.             {
  74.                 Console.WriteLine("Пользователь не найден");
  75.             }
  76.         }
  77.  
  78.         public void UnBanPlayer(int id)
  79.         {
  80.             Player player = GetPlayer(id);
  81.             if (player != null)
  82.             {
  83.                 player.UnBan();
  84.             }
  85.             else
  86.             {
  87.                 Console.WriteLine("Пользователь не найден");
  88.             }
  89.         }
  90.  
  91.         public void ShowPlayers()
  92.         {
  93.             foreach (var player in _players)
  94.             {
  95.                 Console.WriteLine($"Ник - {player.Name}, Уровень - {player.Level}, Статус бана - {player._isPlayerBanned}, id - {player.Id}");
  96.             }
  97.         }
  98.     }
  99.  
  100.     class Player
  101.     {
  102.         private static int _id = 0;
  103.         public int Id { get; private set; }
  104.         public string Name { get; private set; }
  105.         public int Level { get; private set; }
  106.         private bool _isPlayerBanned;
  107.  
  108.         public Player(string name, int level, bool flag)
  109.         {
  110.             Id = _id;
  111.             Name = name;
  112.             Level = level;
  113.             _isPlayerBanned = flag;
  114.             _id += 1;
  115.         }
  116.  
  117.         public void Ban()
  118.         {
  119.             _isPlayerBanned = true;
  120.         }
  121.  
  122.         public void UnBan()
  123.         {
  124.             _isPlayerBanned = false;
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement