Advertisement
TwinFrame

Clight_40_PlayerDataBase

Aug 8th, 2023 (edited)
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         const ConsoleKey AddPlayerCommand = ConsoleKey.F1;
  6.         const ConsoleKey DeletePlayerCommand = ConsoleKey.F2;
  7.         const ConsoleKey BanPlayerCommand = ConsoleKey.F3;
  8.         const ConsoleKey UnbanPlayerCommand = ConsoleKey.F4;
  9.         const ConsoleKey ExitCommand = ConsoleKey.F5;
  10.  
  11.         PlayersBase playersBase = new PlayersBase();
  12.  
  13.         bool isWorking = true;
  14.  
  15.         while (isWorking)
  16.         {
  17.             Console.Clear();
  18.             Console.WriteLine($"{AddPlayerCommand} - Добавление игрока.");
  19.             Console.WriteLine($"{DeletePlayerCommand} - Удаление игрока.");
  20.             Console.WriteLine($"{BanPlayerCommand} - Бан игрока.");
  21.             Console.WriteLine($"{UnbanPlayerCommand} - Разбан игрока.");
  22.             Console.WriteLine($"{ExitCommand} - Выход.");
  23.  
  24.             ConsoleKey inputKey = Console.ReadKey().Key;
  25.             Console.Clear();
  26.  
  27.             switch (inputKey)
  28.             {
  29.                 case AddPlayerCommand:
  30.                     playersBase.AddPlayer();
  31.                     break;
  32.  
  33.                 case DeletePlayerCommand:
  34.                     playersBase.DeletePlayer();
  35.                     break;
  36.  
  37.                 case BanPlayerCommand:
  38.                     playersBase.SetBan(true);
  39.                     break;
  40.  
  41.                 case UnbanPlayerCommand:
  42.                     playersBase.SetBan(false);
  43.                     break;
  44.  
  45.                 case ExitCommand:
  46.                     isWorking = false;
  47.                     break;
  48.  
  49.                 default:
  50.                     Console.WriteLine("Не известная команда. Попробуйте ещё раз.");
  51.                     break;
  52.             }
  53.         }
  54.     }
  55. }
  56.  
  57. class PlayersBase
  58. {
  59.     private int _maxLevel = 100;
  60.  
  61.     private DataBase _dataBase = new DataBase();
  62.  
  63.     public PlayersBase()
  64.     {
  65.         Player player = new Player("Иван", 11, 23);
  66.         _dataBase.AddPlayer(player);
  67.  
  68.         player = new Player("Сергей", 10, 23);
  69.         _dataBase.AddPlayer(player);
  70.  
  71.         player = new Player("Влад", 9, 25);
  72.         _dataBase.AddPlayer(player);
  73.     }
  74.  
  75.     public void AddPlayer()
  76.     {
  77.         Console.Write("\nВведите фамилию игрока: ");
  78.         string surname = Console.ReadLine();
  79.  
  80.         int id = GetUniqueID("\nВведите номер игрока");
  81.  
  82.         int level = GetClampNumber($"\nВведите уровень игрока до {_maxLevel}", 0, _maxLevel);
  83.  
  84.         _dataBase.AddPlayer(new Player(surname, id, level));
  85.     }
  86.  
  87.     public void DeletePlayer()
  88.     {
  89.         string userInput = string.Empty;
  90.  
  91.         bool isCorrectNumber = false;
  92.  
  93.         if (IsAnyInBase() == false)
  94.         {
  95.             Console.Write("Пока нет ни одного игрока в базе.");
  96.             Console.ReadKey();
  97.             return;
  98.         }
  99.  
  100.         ShowAllPlayers();
  101.  
  102.         Console.Write("\nВведите номер пункта удаляемого игрока: ");
  103.         userInput = Console.ReadLine();
  104.  
  105.         if (int.TryParse(userInput, out int number))
  106.         {
  107.             if (TryGetPlayerIndex(number, out int index))
  108.             {
  109.                 _dataBase.DeletePlayer(index);
  110.                 Console.WriteLine("Игрок успешно удалён.");
  111.             }
  112.             else
  113.             {
  114.                 Console.WriteLine("Номер пункта за пределами списка игроков.");
  115.             }
  116.         }
  117.         else
  118.         {
  119.             Console.WriteLine("Номер пункта не корректен.");
  120.         }
  121.  
  122.         Console.ReadKey();
  123.     }
  124.  
  125.     public void SetBan(bool isBanned)
  126.     {
  127.         if (IsAnyInBase() == false)
  128.         {
  129.             Console.Write("Пока нет ни одного игрока в базе.");
  130.             Console.ReadKey();
  131.             return;
  132.         }
  133.  
  134.         ShowAllPlayers();
  135.  
  136.         Console.Write($"\nВведите уникальный номер игрока для смены статуса бана на {isBanned}: ");
  137.  
  138.         string userInput = Console.ReadLine();
  139.  
  140.         if (int.TryParse(userInput, out int id))
  141.         {
  142.             if (TryGetPlayerIndexById(id, out int index))
  143.             {
  144.                 if (_dataBase.Players[index].IsBanned != isBanned)
  145.                 {
  146.                     _dataBase.CahngeBan(index);
  147.  
  148.                     Console.WriteLine("Смена статуса прошла успешно.");
  149.                 }
  150.                 else
  151.                 {
  152.                     Console.WriteLine($"Игрок уже имеет статус {isBanned}");
  153.                 }
  154.             }
  155.             else
  156.             {
  157.                 Console.WriteLine($"Нет игрока с номером {id}.");
  158.             }
  159.         }
  160.         else
  161.         {
  162.             Console.WriteLine("Введите число.");
  163.         }
  164.  
  165.         Console.ReadKey();
  166.     }
  167.  
  168.     private int GetUniqueID(string message)
  169.     {
  170.         int id = int.MinValue;
  171.  
  172.         bool isUniqueID = false;
  173.         bool isDuplicate = false;
  174.  
  175.         while (isUniqueID == false)
  176.         {
  177.             id = GetClampNumber(message, 0, int.MaxValue);
  178.  
  179.             foreach (Player player in _dataBase.Players)
  180.             {
  181.                 if (player.ID == id)
  182.                 {
  183.                     isDuplicate = true;
  184.                     break;
  185.                 }
  186.             }
  187.  
  188.             if (isDuplicate)
  189.             {
  190.                 Console.WriteLine("Номер игрока не универсальный. Попробуйте ещё раз.");
  191.                 isDuplicate = false;
  192.             }
  193.             else
  194.             {
  195.                 isUniqueID = true;
  196.             }
  197.         }
  198.  
  199.         return id;
  200.     }
  201.  
  202.     private int GetClampNumber(string message, int minValue, int maxValue)
  203.     {
  204.         int value = int.MinValue;
  205.         string userInput = string.Empty;
  206.  
  207.         while (int.TryParse(userInput, out value) == false)
  208.         {
  209.             Console.Write($"{message}: ");
  210.             userInput = Console.ReadLine();
  211.         }
  212.  
  213.         value = Math.Clamp(value, minValue, maxValue);
  214.  
  215.         return value;
  216.     }
  217.  
  218.     private bool TryGetPlayerIndex(int number, out int index)
  219.     {
  220.         index = int.MinValue;
  221.  
  222.         if (number > 0 && number <= _dataBase.Players.Count)
  223.         {
  224.             index = --number;
  225.             return true;
  226.         }
  227.         else
  228.         {
  229.             Console.WriteLine("Номер пункта за пределами списка игроков.");
  230.         }
  231.  
  232.         return false;
  233.     }
  234.  
  235.     private bool TryGetPlayerIndexById(int id, out int index)
  236.     {
  237.         index = int.MinValue;
  238.  
  239.         for (int i = 0; i < _dataBase.Players.Count; i++)
  240.         {
  241.             if (_dataBase.Players[i].ID == id)
  242.             {
  243.                 index = i;
  244.                 return true;
  245.             }
  246.         }
  247.  
  248.         return false;
  249.     }
  250.  
  251.     private void ShowAllPlayers()
  252.     {
  253.         _dataBase.ShowAllPlayers();
  254.     }
  255.  
  256.     private bool IsAnyInBase()
  257.     {
  258.         return _dataBase.Players.Any();
  259.     }
  260.  
  261.     private bool IsCorrectIndex(int index)
  262.     {
  263.         return index >= 0 && index < _dataBase.Players.Count;
  264.     }
  265. }
  266.  
  267. class DataBase
  268. {
  269.     private List<Player> _players = new List<Player>();
  270.  
  271.     public List<Player> Players => _players;
  272.  
  273.     public void AddPlayer(Player player)
  274.     {
  275.         _players.Add(player);
  276.     }
  277.  
  278.     public void DeletePlayer(int index)
  279.     {
  280.         _players.Remove(_players[index]);
  281.     }
  282.  
  283.     public void ShowAllPlayers()
  284.     {
  285.         for (int i = 0; i < _players.Count; i++)
  286.         {
  287.             Console.Write($"{i + 1}. ");
  288.             _players[i].ShowInfo();
  289.         }
  290.     }
  291.  
  292.     public void CahngeBan(int index)
  293.     {
  294.         _players[index].CahngeBan();
  295.     }
  296. }
  297.  
  298. class Player
  299. {
  300.  
  301.     public string Surname { get; private set; }
  302.     public int ID { get; private set; }
  303.     public int Level { get; private set; }
  304.     public bool IsBanned { get; private set; }
  305.  
  306.     public Player(string surname, int id, int level)
  307.     {
  308.         Surname = surname;
  309.         ID = id;
  310.         Level = level;
  311.         IsBanned = false;
  312.     }
  313.  
  314.     public void ShowInfo()
  315.     {
  316.         Console.WriteLine($"Фамилия: {Surname};\tУникальный номер: {ID};\tУровень: {Level};\tБан: {IsBanned};");
  317.     }
  318.  
  319.     public void CahngeBan()
  320.     {
  321.         IsBanned = !IsBanned;
  322.     }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement