Advertisement
nikitaTheSlayer

Lesson: OOP_3

Feb 5th, 2022 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace OOP
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             bool isExit = true;
  11.  
  12.             List<Player> player = new List<Player>();
  13.             DataBase database = new DataBase(player);
  14.  
  15.             while (isExit == true)
  16.             {
  17.                 Console.WriteLine("1) Добавить игрока");
  18.                 Console.WriteLine("2) Забанить игрока");
  19.                 Console.WriteLine("3) Разбанить игрока");
  20.                 Console.WriteLine("4) Удалить игрока");
  21.                 Console.WriteLine("5) Показать всех игроков");
  22.                 Console.WriteLine("6) Выйти");
  23.                 Console.Write("Выберите действие: ");
  24.  
  25.                 switch (Console.ReadLine())
  26.                 {
  27.                     case "1":
  28.                         database.AddPlayer();                        
  29.                         break;
  30.                     case "2":
  31.                         database.BanPlayer();                        
  32.                         break;
  33.                     case "3":
  34.                         database.UnbanPlayer();                        
  35.                         break;
  36.                     case "4":
  37.                         database.RemovePlayer();                        
  38.                         break;
  39.                     case "5":
  40.                         database.ShowPlayersInfo();
  41.                         break;
  42.                     case "6":
  43.                         isExit = false;
  44.                         break;
  45.                     default:
  46.                         Console.WriteLine("Ошибка! Такой команды нет!");
  47.                         Console.ReadKey();
  48.                         Console.Clear();
  49.                         break;
  50.                 }
  51.             }
  52.         }
  53.     }
  54.  
  55.     class Player
  56.     {
  57.         private string _name;
  58.         private int _level;
  59.         private bool _isBan;
  60.  
  61.         public Player(string name, int level, bool isBan)
  62.         {
  63.             _name = name;
  64.             _level = level;
  65.             _isBan = isBan;
  66.         }
  67.  
  68.         public void ShowInfo(int index)
  69.         {
  70.             Console.Write(index + 1 + ") ");
  71.             Console.WriteLine($"Имя игрока - {_name}, уровень игрока - {_level}, статус блокировки игрока - {_isBan}");
  72.         }
  73.  
  74.         public void Ban()
  75.         {
  76.             _isBan = true;
  77.         }
  78.  
  79.         public void Unban()
  80.         {
  81.             _isBan = false;
  82.         }
  83.     }
  84.  
  85.     class DataBase
  86.     {
  87.         private Player _player;
  88.         private List<Player> _players = new List<Player>();
  89.         private string _indexPlayer;
  90.         private string _name;
  91.         private int _numberPlayer;
  92.  
  93.         public DataBase(List<Player> players)
  94.         {
  95.             _players = players;
  96.         }
  97.  
  98.         public void AddPlayer()
  99.         {
  100.             Console.Write("Введите имя игрока: ");
  101.             _name = Console.ReadLine();
  102.             _players.Add(new Player(_name, 1, false));
  103.             Console.WriteLine("Игрок добавлен!");
  104.             Console.ReadKey();
  105.             Console.Clear();
  106.         }
  107.  
  108.         public void RemovePlayer()
  109.         {
  110.             if (TryFindPlayer(out _player, "Игрок удален"))
  111.             {
  112.                 _players.RemoveAt(_numberPlayer);
  113.             }
  114.             Console.ReadKey();
  115.             Console.Clear();
  116.         }
  117.  
  118.         public void BanPlayer()
  119.         {
  120.             Console.Write("Введите номер игрока, которого хотите забанить: ");
  121.             if (TryFindPlayer(out _player, "Игрок забанен"))
  122.             {
  123.                 _player.Ban();
  124.             }
  125.             Console.ReadKey();
  126.             Console.Clear();
  127.         }
  128.  
  129.         public void UnbanPlayer()
  130.         {
  131.             Console.Write("Введите номер игрока, которого хотите разабанить: ");
  132.             if (TryFindPlayer(out _player, "Игрок разбанен"))
  133.             {
  134.                 _player.Unban();
  135.             }
  136.             Console.ReadKey();
  137.             Console.Clear();
  138.         }
  139.  
  140.         public void ShowPlayersInfo()
  141.         {
  142.             for (int i = 0; i < _players.Count; i++)
  143.             {
  144.                 _players[i].ShowInfo(i);
  145.             }
  146.             Console.ReadKey();
  147.             Console.Clear();
  148.         }
  149.  
  150.         private bool TryFindPlayer(out Player player, string message)
  151.         {
  152.             player = null;
  153.             _indexPlayer = Console.ReadLine();
  154.             if (int.TryParse(_indexPlayer, out _numberPlayer))
  155.             {
  156.                 _numberPlayer--;
  157.                 if (_players.Count > _numberPlayer && _numberPlayer >= 0)
  158.                 {
  159.                     Console.WriteLine(message);
  160.                     player = _players[_numberPlayer];
  161.                     return true;
  162.                 }
  163.                 else
  164.                 {
  165.                     Console.WriteLine("Игрок не найден!");
  166.                     return false;
  167.                 }
  168.             }
  169.             else
  170.             {
  171.                 Console.WriteLine("Игрок не найден!");
  172.                 return false;
  173.             }
  174.         }
  175.     }    
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement