Advertisement
ranee

Untitled

Mar 16th, 2023
575
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DataBasePlayers
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             bool isWork = true;
  10.             DataBase dataBase = new DataBase();
  11.  
  12.             while (isWork)
  13.             {
  14.                 Console.WriteLine("Выбирете необходимый пункт:\n" +
  15.                     "\n1. Добавить игрока" +
  16.                     "\n2. Посмотреть всех" +
  17.                     "\n3. Удалить игрока по ID" +
  18.                     "\n4. Сменить статус бана" +
  19.                     "\n5. Выйти из программы");
  20.                 string userSelection = Console.ReadLine();
  21.  
  22.                 switch (userSelection)
  23.                 {
  24.                     case "1":
  25.                         dataBase.AddPlayer();
  26.                         break;
  27.                     case "2":
  28.                         dataBase.ShowAllPlayers();
  29.                         break;
  30.                     case "3":
  31.                         dataBase.ShowAllPlayers();
  32.                         dataBase.RemovePlayerById();
  33.                         break;
  34.                     case "4":
  35.                         dataBase.ShowAllPlayers();
  36.                         dataBase.ChangeBanStatusPlayerById();
  37.                         break;
  38.                     case "5":
  39.                         isWork = false;
  40.                         break;
  41.                     default:
  42.                         Console.WriteLine("Ошибка, такого пункта меню нет");
  43.                         break;
  44.                 }
  45.                 Console.ReadKey();
  46.                 Console.Clear();
  47.             }
  48.         }
  49.     }
  50.  
  51.     class Player
  52.     {
  53.         private static int _idCounter = 1;
  54.         private int _level;
  55.         private string _name;
  56.         private bool _isBaned;
  57.  
  58.         public Player(string name, int level, bool isBaned = false)
  59.         {
  60.             _name = name;
  61.             _level = level;
  62.             _isBaned = isBaned;
  63.             UniqueNumber = _idCounter++;
  64.         }
  65.  
  66.         public int UniqueNumber { get; private set; }
  67.  
  68.         public void ShowInfo()
  69.         {
  70.             Console.WriteLine($"ID игрока: {UniqueNumber}," +
  71.                 $"Ник игрока: {_name}, " +
  72.                 $"уровень - {_level} " +
  73.                 $"{(_isBaned ? ", !!!БАН!!!" : ".")}");
  74.         }
  75.  
  76.         public void ChangeBanStatus()
  77.         {
  78.             _isBaned = _isBaned ? false : true;
  79.         }
  80.     }
  81.     class DataBase
  82.     {
  83.         private List<Player> _players;
  84.  
  85.         public DataBase()
  86.         {
  87.             _players = new List<Player>();
  88.         }
  89.  
  90.         public void AddPlayer()
  91.         {
  92.             Console.WriteLine("Введите ник игрока:");
  93.             string namePlayer = Console.ReadLine();
  94.             Console.WriteLine("Введите уровень игрока:");
  95.             int levelPlayer = Convert.ToInt32(Console.ReadLine());
  96.             Player player = new Player(namePlayer, levelPlayer);
  97.             _players.Add(player);
  98.             Console.WriteLine($"Игрок {namePlayer} успешно добавлен");
  99.         }
  100.  
  101.         public void RemovePlayerById()
  102.         {
  103.             Console.Write("Введите ID для удаления: ");
  104.             int id = Convert.ToInt32(Console.ReadLine());
  105.             Player playerToRemove = null;
  106.             foreach (Player player in _players)
  107.             {
  108.                 if (player.UniqueNumber == id)
  109.                 {
  110.                     playerToRemove = player;
  111.                     break;
  112.                 }
  113.             }
  114.             if (playerToRemove != null)
  115.             {
  116.                 _players.Remove(playerToRemove);
  117.                 Console.WriteLine($"Игрок с ID {id} успешно удален");
  118.             }
  119.             else
  120.             {
  121.                 Console.WriteLine($"Игрок с ID {id} не найден");
  122.             }
  123.         }
  124.  
  125.         public void ChangeBanStatusPlayerById()
  126.         {
  127.             Console.Write("Введите ID для изменения статуса БАНА: ");
  128.             int id = Convert.ToInt32(Console.ReadLine());
  129.             Player playerToBan = null;
  130.             foreach (Player player in _players)
  131.             {
  132.                 if (player.UniqueNumber == id)
  133.                 {
  134.                     playerToBan = player;
  135.                     break;
  136.                 }
  137.             }
  138.             if (playerToBan != null)
  139.             {
  140.                 playerToBan.ChangeBanStatus();
  141.                 Console.WriteLine($"Статус бана игрока с ID {id} успешно изменен");
  142.             }
  143.             else
  144.             {
  145.                 Console.WriteLine($"Игрок с ID {id} не найден");
  146.             }
  147.         }
  148.  
  149.         public void ShowAllPlayers()
  150.         {
  151.             if (_players.Count > 0)
  152.             {
  153.  
  154.                 foreach (var player in _players)
  155.                 {
  156.                     player.ShowInfo();
  157.                 }
  158.             }
  159.             else
  160.             {
  161.                 Console.WriteLine("Список пуст");
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement