Advertisement
Torgach

temptempBDofPlayers

Mar 26th, 2021 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.08 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 BDPlayers
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Database database = new Database();
  14.             database.Work();
  15.         }
  16.     }
  17.  
  18.     class Player
  19.     {
  20.         public string Nickname { get; private set; }
  21.         public int Index { get; private set; }
  22.         public int Level { get; private set; }
  23.         public bool IsBanned { get; private set; }
  24.  
  25.         public Player()
  26.         {
  27.             Nickname = "Без имени";
  28.             Index = 0;
  29.             Level = 0;
  30.             IsBanned = false;
  31.         }
  32.  
  33.         public Player(string nickname, int index, int lvl, bool isBannded)
  34.         {
  35.             Nickname = nickname;
  36.             Index = index;
  37.             Level = lvl;
  38.             IsBanned = isBannded;
  39.         }
  40.  
  41.         public void Ban()
  42.         {
  43.             IsBanned = true;
  44.         }
  45.  
  46.         public void Unban()
  47.         {
  48.             IsBanned = false;
  49.         }
  50.     }
  51.  
  52.     class Database
  53.     {
  54.         private List<Player> _players = new List<Player>()
  55.         {
  56.             new Player("Io", 1, 15, true),
  57.             new Player("V", 2, 100, false)
  58.         };
  59.  
  60.         public void Work()
  61.         {
  62.             bool isRun = true;
  63.  
  64.             while (isRun)
  65.             {
  66.                 Console.WriteLine("\n");
  67.  
  68.                 Console.WriteLine("[1] - создать игрока\n" +
  69.                 "[2] - показать всех игроков\n" +
  70.                 "[3] - забанить игрока\n" +
  71.                 "[4] - разбанить игрока\n" +
  72.                 "[5] - удалить игрока\n" +
  73.                 "[6] - выход");
  74.                 Console.Write("Ввод: ");
  75.  
  76.                 switch (Console.ReadLine())
  77.                 {
  78.                     case "1":
  79.                         CreatePlayer();
  80.                         break;
  81.                     case "2":
  82.                         ShowPlayers();
  83.                         break;
  84.                     case "3":
  85.                         BanPlayer();
  86.                         break;
  87.                     case "4":
  88.                         UnBanPlayer();
  89.                         break;
  90.                     case "5":
  91.                         DeletePlayer();
  92.                         break;
  93.                     case "6":
  94.                         isRun = false;
  95.                         break;
  96.                     default:
  97.                         Console.WriteLine("Ошибка!");
  98.                         break;
  99.                 }
  100.             }
  101.         }
  102.  
  103.         private void CreatePlayer()
  104.         {
  105.             string name;
  106.             bool isBanned;
  107.  
  108.             Console.Write("Введите имя: ");
  109.             name = Console.ReadLine();
  110.  
  111.             if (IsInputVerified("Введите уровень: ", 0, 100, out int lvl) == false)
  112.             {
  113.                 Console.Write("Ввод неправильный!");
  114.                 return;
  115.             }
  116.             ++lvl;
  117.  
  118.             Console.Write("Игрок в бане? Да - [Y] или Нет - [N]. Ввод: ");
  119.             string userInput = Console.ReadLine();
  120.             if (userInput == "Y")
  121.                 isBanned = true;
  122.             else
  123.                 isBanned = false;
  124.  
  125.             Player player = new Player(name, _players.Count + 1, lvl, isBanned);
  126.             _players.Add(player);
  127.         }
  128.  
  129.         private void ShowPlayers()
  130.         {
  131.             if (IsDataBaseEmpty())
  132.             {
  133.                 return;
  134.             }
  135.             foreach (var player in _players)
  136.             {
  137.                 Console.Write($"{player.Index}) '{player.Nickname}' {player.Level}lvl. ");
  138.                 if (player.IsBanned)
  139.                     Console.WriteLine("Игрок в бане");
  140.                 else
  141.                     Console.WriteLine("Игрок не в бане");
  142.             }
  143.         }
  144.  
  145.         private void BanPlayer()
  146.         {
  147.             if (IsDataBaseEmpty())
  148.                 return;
  149.  
  150.             if (IsInputVerified("Введите номер: ", 1, _players.Count, out int userIndex))
  151.             {
  152.                 if (_players.ElementAt(userIndex).IsBanned == true)
  153.                 {
  154.                     Console.WriteLine("Игрока забанить нельзя!");
  155.                     return;
  156.  
  157.                 }
  158.                 _players.ElementAt(userIndex).Ban();
  159.             }
  160.         }
  161.  
  162.         private void UnBanPlayer()
  163.         {
  164.             if (IsDataBaseEmpty())
  165.                 return;
  166.  
  167.             if (IsInputVerified("Введите номер: ", 1, _players.Count, out int userIndex))
  168.             {
  169.                 if (_players.ElementAt(userIndex).IsBanned == false)
  170.                 {
  171.                     Console.WriteLine("Игрока забанить нельзя!");
  172.                     return;
  173.  
  174.                 }
  175.                 _players.ElementAt(userIndex).Unban();
  176.             }
  177.         }
  178.  
  179.         private void DeletePlayer()
  180.         {
  181.             if (IsDataBaseEmpty())
  182.                 return;
  183.  
  184.             if (IsInputVerified("Введите номер: ", 1, _players.Count, out int userIndex))
  185.                 _players.RemoveAt(userIndex);    
  186.         }
  187.  
  188.         private bool IsInputVerified(string message, int min, int max, out int userInput)
  189.         {
  190.             Console.WriteLine(message + $"Диапазон: от {min} до {max}");
  191.  
  192.             if (int.TryParse(Console.ReadLine(), out userInput) == false || (userInput > max || userInput < min))
  193.             {
  194.                 Console.WriteLine("Некорректный ввод!");
  195.                 return false;
  196.             }
  197.             --userInput;
  198.             return true;
  199.         }
  200.  
  201.         private bool IsDataBaseEmpty()
  202.         {
  203.             if (_players.Count == 0)
  204.             {
  205.                 Console.WriteLine("База данных пуста!");
  206.                 return true;
  207.             }
  208.             return false;
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement