Advertisement
Rodunskiy

Untitled

May 13th, 2025
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 KB | None | 0 0
  1. namespace CSLight
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             const string AddPlayer = "1";
  8.             const string BanPlayer = "2";
  9.             const string UnbanPlayer = "3";
  10.             const string DeletePlayer = "4";
  11.             const string PrintAllPlayers = "5";
  12.             const string ExitProgram = "6";
  13.  
  14.             Database database = new Database();
  15.  
  16.             bool isWorking = true;
  17.  
  18.             while (isWorking)
  19.             {
  20.                 Console.WriteLine($"{AddPlayer})Добавить игрока.\n{BanPlayer})Забанить игрока.\n{UnbanPlayer})Разбанить игрока.\n{DeletePlayer})Удалить игрока." +
  21.                                   $"\n{PrintAllPlayers})Вывести список всех игроков.\n{ExitProgram})Выход из программы.");
  22.                 string userInput = Console.ReadLine();
  23.  
  24.                 switch (userInput)
  25.                 {
  26.                     case AddPlayer:
  27.                         database.AddPlayer();
  28.                         break;
  29.  
  30.                     case BanPlayer:
  31.                         database.BanPlayer();
  32.                         break;
  33.  
  34.                     case UnbanPlayer:
  35.                         database.UnbanPlayer();
  36.                         break;
  37.  
  38.                     case DeletePlayer:
  39.                         database.DeletePlayer();
  40.                         break;
  41.  
  42.                     case PrintAllPlayers:
  43.                         database.PrintAllPlayers();
  44.                         break;
  45.  
  46.                     case ExitProgram:
  47.                         isWorking = false;
  48.                         break;
  49.  
  50.                     default:
  51.                         Console.WriteLine("Неверный выбор. Попробуйте еще раз.");
  52.                         break;
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     class Database
  59.     {
  60.         private Dictionary<int, Player> _players = new Dictionary<int, Player>();
  61.         private int _nextId = 1;
  62.  
  63.         public void AddPlayer()
  64.         {
  65.             Console.WriteLine("Введите ник игрока.");
  66.             string nickname = Console.ReadLine();
  67.  
  68.             Console.WriteLine("Введите уровень");
  69.             int level = Convert.ToInt32(Console.ReadLine());
  70.  
  71.             Player player = new Player(_nextId, nickname, level);
  72.  
  73.             _players.Add(_nextId, player);
  74.             _nextId++;
  75.  
  76.             Console.WriteLine($"Игрок {nickname} добавлен с ID {player.Id}");
  77.         }
  78.  
  79.         public void BanPlayer()
  80.         {
  81.             Console.WriteLine("Введите уникальный номер игрока которого хотите забанить.");
  82.             int userInput = Convert.ToInt32(Console.ReadLine());
  83.  
  84.             if (_players.TryGetValue(userInput, out var player))
  85.             {
  86.                 player.IsBanned = true;
  87.  
  88.                 Console.WriteLine($"Игрок {player.Nickname} (ID: {userInput}) забанен.");
  89.             }
  90.             else
  91.             {
  92.                 Console.WriteLine($"Игрок с ID {userInput} не найден.");
  93.             }
  94.         }
  95.  
  96.         public void UnbanPlayer()
  97.         {
  98.             Console.WriteLine("Введите уникальный номер игрока которого хотите разбанить.");
  99.             int userInput = Convert.ToInt32(Console.ReadLine());
  100.  
  101.             if (_players.TryGetValue(userInput, out var player))
  102.             {
  103.                 player.IsBanned = false;
  104.  
  105.                 Console.WriteLine($"Игрок {player.Nickname} (ID: {userInput}) разбанен.");
  106.             }
  107.             else
  108.             {
  109.                 Console.WriteLine($"Игрок с ID {userInput} не найден.");
  110.             }
  111.         }
  112.  
  113.         public void DeletePlayer()
  114.         {
  115.             Console.WriteLine("Введите уникальный номер игрока которого хотите удалить.");
  116.             int userInput = Convert.ToInt32(Console.ReadLine());
  117.  
  118.             if (_players.Remove(userInput, out var player))
  119.             {
  120.                 Console.WriteLine($"Игрок {player.Nickname} (ID: {userInput}) удален.");
  121.             }
  122.             else
  123.             {
  124.                 Console.WriteLine($"Игрок с ID {userInput} не найден.");
  125.             }
  126.         }
  127.  
  128.         public void PrintAllPlayers()
  129.         {
  130.             if (_players.Count == 0)
  131.             {
  132.                 Console.WriteLine("В базе нет игроков.");
  133.             }
  134.             else
  135.             {
  136.                 Console.WriteLine("Список игроков:");
  137.  
  138.                 foreach (var player in _players.Values)
  139.                 {
  140.                     player.ShowPlayer();
  141.                 }
  142.             }
  143.         }
  144.     }
  145.  
  146.     class Player
  147.     {
  148.         public int Id { get; private set; }
  149.         public string Nickname { get; private set; }
  150.         public int Level { get; private set; }
  151.         public bool IsBanned { get;  set; }
  152.  
  153.         public Player(int uniqueNumber, string nickname, int lvl, bool isBanned = false)
  154.         {
  155.             Id = uniqueNumber;
  156.             Nickname = nickname;
  157.             Level = lvl;
  158.             IsBanned = isBanned;
  159.         }
  160.  
  161.         public void ShowPlayer()
  162.         {
  163.             if (IsBanned == false)
  164.             {
  165.                 Console.WriteLine($"ID: {Id} | Ник: {Nickname} | Уровень: {Level} | Статус: Активен");
  166.             }
  167.             else
  168.             {
  169.                 Console.WriteLine($"ID: {Id} | Ник: {Nickname} | Уровень: {Level} | Статус: Забанен");
  170.             }
  171.  
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement