Advertisement
OldBeliver

PlayerDatabase_ver02

Apr 17th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 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 Status { get; private set; }
  66.  
  67.         public Gamer(string nickname, int level, bool status)
  68.         {
  69.             _nickname = nickname;
  70.             _level = level;
  71.             Status = status;
  72.         }
  73.  
  74.         public void ShowInfo()
  75.         {
  76.             string state = "активный";
  77.             if (Status == false)
  78.             {
  79.                 state = "предан анафеме";
  80.             }
  81.             Console.WriteLine($"{_nickname} - уровень {_level}, статус {state}");
  82.         }
  83.  
  84.         public void ChangeStatus(bool status)
  85.         {            
  86.             Status = status;
  87.         }
  88.     }
  89.  
  90.     class Register
  91.     {
  92.         private List<Gamer> _gamers;
  93.  
  94.         public Register(List<Gamer> gamers)
  95.         {
  96.             _gamers = gamers;
  97.         }
  98.  
  99.         public void ShowRecords()
  100.         {
  101.             for (int i = 0; i < _gamers.Count; i++)
  102.             {
  103.                 Console.Write($"{i + 1}. ");
  104.                 _gamers[i].ShowInfo();
  105.             }
  106.  
  107.             if (_gamers.Count == 0)
  108.             {
  109.                 Console.WriteLine($"список пуст");
  110.             }
  111.         }
  112.  
  113.         public void AddRecord()
  114.         {
  115.             Console.Write($"Введите имя игрока: ");
  116.             string nickname = Console.ReadLine();
  117.  
  118.             _gamers.Add(new Gamer(nickname, 1, true));
  119.  
  120.             Console.Write($"\nдобавлен игрок: ");
  121.             _gamers[_gamers.Count - 1].ShowInfo();
  122.         }
  123.  
  124.         public void DeleteRecord()
  125.         {
  126.             int index;
  127.  
  128.             bool isLegalNumber = GetLegalNumber(out index);
  129.  
  130.             if (isLegalNumber)
  131.             {
  132.                 _gamers[index - 1].ShowInfo();
  133.                 Console.WriteLine($"запись удалена");
  134.                 _gamers.RemoveAt(index - 1);
  135.             }
  136.         }
  137.  
  138.         public void BanGamer()
  139.         {
  140.             int index;
  141.  
  142.             bool isLegalNumber = GetLegalNumber(out index);
  143.  
  144.             if (isLegalNumber)
  145.             {  
  146.                 _gamers[index - 1].ChangeStatus(false);
  147.                 Console.Write($"статус изменен: ");
  148.                 _gamers[index - 1].ShowInfo();
  149.             }
  150.         }
  151.  
  152.         public void UnBanGamer()
  153.         {
  154.             int index;
  155.  
  156.             bool isLegalNumber = GetLegalNumber(out index);
  157.  
  158.             if (isLegalNumber)
  159.             {
  160.                 _gamers[index - 1].ChangeStatus(true);
  161.                 Console.Write($"статус изменен: ");
  162.                 _gamers[index - 1].ShowInfo();
  163.             }
  164.         }
  165.  
  166.         private bool GetLegalNumber(out int index)
  167.         {
  168.             int number;
  169.             index = 0;
  170.             int limit = _gamers.Count;
  171.  
  172.             if (ReadInt(out number))
  173.             {
  174.                 index = number;
  175.             }
  176.  
  177.             bool isLegalRange = (index > 0 && index <= limit);
  178.  
  179.             if (isLegalRange == false)
  180.             {
  181.                 Console.WriteLine($"ошибка ввода числа");
  182.             }
  183.  
  184.             return isLegalRange;
  185.         }
  186.  
  187.         private bool ReadInt(out int number)
  188.         {
  189.             bool result = false;
  190.  
  191.             Console.Write($"Введите порядковый номер игрока: ");
  192.             result = int.TryParse(Console.ReadLine(), out number);
  193.  
  194.             return result;
  195.         }
  196.     }
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement