Advertisement
OldBeliver

PlayerDatabase_ver03

Apr 17th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.42 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 PlayerDatabase_02
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Register register = new Register(new List<Gamer>());
  14.  
  15.             bool isOpen = true;
  16.             string userInput;
  17.  
  18.             while (isOpen)
  19.             {
  20.                 Console.WriteLine($"админка управления игроками");
  21.                 Console.WriteLine($"\n0 - Список игроков\n1 - Добавить игрока\n2 - Заблокировать игрока\n3 - Разблокировать игрока\n4 - Удалить игрока\n5 - Выход");
  22.  
  23.                 Console.Write($"\nВведите номер команды: ");
  24.                 userInput = Console.ReadLine();
  25.  
  26.                 switch (userInput)
  27.                 {
  28.                     case "0":
  29.                         Console.WriteLine($"Список игроков:\n");
  30.                         register.ShowRecords();
  31.                         break;
  32.                     case "1":
  33.                         Console.WriteLine($"Добавление игрока\n");
  34.                         register.AddRecord();
  35.                         break;
  36.                     case "2":
  37.                         Console.WriteLine($"Блокирование игрока");
  38.                         register.BanGamer();
  39.                         break;
  40.                     case "3":
  41.                         Console.WriteLine($"Разблокирование игрока");
  42.                         register.UnbanGamer();
  43.                         break;
  44.                     case "4":
  45.                         Console.WriteLine($"Удаление игрока\n");
  46.                         register.DeleteRecord();
  47.                         break;
  48.                     case "5":
  49.                         Console.WriteLine($"\nВыход из программы");
  50.                         isOpen = false;
  51.                         break;
  52.                 }
  53.  
  54.                 Console.Write($"\nлюбую для продолжения... ");
  55.                 Console.ReadKey();
  56.                 Console.Clear();
  57.             }
  58.         }
  59.     }
  60.  
  61.     class Gamer
  62.     {
  63.         private string _nickname;
  64.         private int _level;
  65.         public bool IsBanned { get; private set; }
  66.  
  67.         public Gamer(string nickname, int level, bool isBanned)
  68.         {
  69.             _nickname = nickname;
  70.             _level = level;
  71.             IsBanned = isBanned;
  72.         }
  73.  
  74.         public void ShowInfo()
  75.         {
  76.             string status = "не числиться";
  77.             if (IsBanned)
  78.             {
  79.                 status = "находится";
  80.             }
  81.             Console.WriteLine($"{_nickname} - {_level} уровень, {status} в черном списке");
  82.         }
  83.  
  84.         public void Ban()
  85.         {
  86.             IsBanned = true;
  87.         }
  88.  
  89.         public void Unan()
  90.         {
  91.             IsBanned = false;
  92.         }
  93.     }
  94.  
  95.     class Register
  96.     {
  97.         private List<Gamer> _gamers;
  98.  
  99.         public Register(List<Gamer> gamers)
  100.         {
  101.             _gamers = gamers;
  102.         }
  103.  
  104.         public void ShowRecords()
  105.         {
  106.             for (int i = 0; i < _gamers.Count; i++)
  107.             {
  108.                 Console.Write($"{i + 1}. ");
  109.                 _gamers[i].ShowInfo();
  110.             }
  111.  
  112.             if (_gamers.Count == 0)
  113.             {
  114.                 Console.WriteLine($"список пуст");
  115.             }
  116.         }        
  117.  
  118.         public void AddRecord()
  119.         {
  120.             Console.Write($"Введите имя игрока: ");
  121.             string nickname = Console.ReadLine();
  122.  
  123.             _gamers.Add(new Gamer(nickname, 1, false));
  124.  
  125.             Console.Write($"\nдобавлен игрок: ");
  126.             _gamers[_gamers.Count - 1].ShowInfo();
  127.         }
  128.  
  129.         public void DeleteRecord()
  130.         {
  131.             int index;
  132.  
  133.             bool isLegalNumber = GetLegalNumber(out index);
  134.  
  135.             if (isLegalNumber)
  136.             {
  137.                 _gamers[index - 1].ShowInfo();
  138.                 Console.WriteLine($"запись удалена");
  139.                 _gamers.RemoveAt(index - 1);
  140.             }
  141.         }
  142.  
  143.         public void BanGamer()
  144.         {
  145.             int index;
  146.  
  147.             GetLegalNumber(out index);
  148.  
  149.             _gamers[index-1].Ban();
  150.         }
  151.  
  152.         public void UnbanGamer()
  153.         {
  154.             int index;
  155.  
  156.             GetLegalNumber(out index);
  157.  
  158.             _gamers[index-1].Unan();
  159.         }
  160.  
  161.         private bool GetLegalNumber(out int index)
  162.         {
  163.             int number;
  164.             index = 0;
  165.             int limit = _gamers.Count;
  166.  
  167.             if (ReadInt(out number))
  168.             {
  169.                 index = number;
  170.             }
  171.  
  172.             bool isLegalRange = (index > 0 && index <= limit);
  173.  
  174.             if (isLegalRange == false)
  175.             {
  176.                 Console.WriteLine($"ошибка ввода числа");
  177.             }
  178.  
  179.             return isLegalRange;
  180.         }
  181.  
  182.         private bool ReadInt(out int number)
  183.         {
  184.             bool result = false;
  185.  
  186.             Console.Write($"Введите порядковый номер игрока: ");
  187.             result = int.TryParse(Console.ReadLine(), out number);
  188.  
  189.             return result;
  190.         }
  191.     }
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement