Torgach

bd

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