Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main()
- {
- const ConsoleKey AddPlayerCommand = ConsoleKey.F1;
- const ConsoleKey DeletePlayerCommand = ConsoleKey.F2;
- const ConsoleKey BanPlayerCommand = ConsoleKey.F3;
- const ConsoleKey UnbanPlayerCommand = ConsoleKey.F4;
- const ConsoleKey ExitCommand = ConsoleKey.F5;
- PlayersBase playersBase = new PlayersBase();
- bool isWorking = true;
- while (isWorking)
- {
- Console.Clear();
- Console.WriteLine($"{AddPlayerCommand} - Добавление игрока.");
- Console.WriteLine($"{DeletePlayerCommand} - Удаление игрока.");
- Console.WriteLine($"{BanPlayerCommand} - Бан игрока.");
- Console.WriteLine($"{UnbanPlayerCommand} - Разбан игрока.");
- Console.WriteLine($"{ExitCommand} - Выход.");
- ConsoleKey inputKey = Console.ReadKey().Key;
- Console.Clear();
- switch (inputKey)
- {
- case AddPlayerCommand:
- playersBase.AddPlayer();
- break;
- case DeletePlayerCommand:
- playersBase.DeletePlayer();
- break;
- case BanPlayerCommand:
- playersBase.SetBan(true);
- break;
- case UnbanPlayerCommand:
- playersBase.SetBan(false);
- break;
- case ExitCommand:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Не известная команда. Попробуйте ещё раз.");
- break;
- }
- }
- }
- }
- class PlayersBase
- {
- private int _maxLevel = 100;
- private DataBase _dataBase = new DataBase();
- public PlayersBase()
- {
- Player player = new Player("Иван", 11, 23);
- _dataBase.AddPlayer(player);
- player = new Player("Сергей", 10, 23);
- _dataBase.AddPlayer(player);
- player = new Player("Влад", 9, 25);
- _dataBase.AddPlayer(player);
- }
- public void AddPlayer()
- {
- Console.Write("\nВведите фамилию игрока: ");
- string surname = Console.ReadLine();
- int id = GetUniqueID("\nВведите номер игрока");
- int level = GetClampNumber($"\nВведите уровень игрока до {_maxLevel}", 0, _maxLevel);
- _dataBase.AddPlayer(new Player(surname, id, level));
- }
- public void DeletePlayer()
- {
- string userInput = string.Empty;
- bool isCorrectNumber = false;
- if (IsAnyInBase() == false)
- {
- Console.Write("Пока нет ни одного игрока в базе.");
- Console.ReadKey();
- return;
- }
- ShowAllPlayers();
- Console.Write("\nВведите номер пункта удаляемого игрока: ");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int number))
- {
- if (TryGetPlayerIndex(number, out int index))
- {
- _dataBase.DeletePlayer(index);
- Console.WriteLine("Игрок успешно удалён.");
- }
- else
- {
- Console.WriteLine("Номер пункта за пределами списка игроков.");
- }
- }
- else
- {
- Console.WriteLine("Номер пункта не корректен.");
- }
- Console.ReadKey();
- }
- public void SetBan(bool isBanned)
- {
- if (IsAnyInBase() == false)
- {
- Console.Write("Пока нет ни одного игрока в базе.");
- Console.ReadKey();
- return;
- }
- ShowAllPlayers();
- Console.Write($"\nВведите уникальный номер игрока для смены статуса бана на {isBanned}: ");
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int id))
- {
- if (TryGetPlayerIndexById(id, out int index))
- {
- if (_dataBase.Players[index].IsBanned != isBanned)
- {
- _dataBase.CahngeBan(index);
- Console.WriteLine("Смена статуса прошла успешно.");
- }
- else
- {
- Console.WriteLine($"Игрок уже имеет статус {isBanned}");
- }
- }
- else
- {
- Console.WriteLine($"Нет игрока с номером {id}.");
- }
- }
- else
- {
- Console.WriteLine("Введите число.");
- }
- Console.ReadKey();
- }
- private int GetUniqueID(string message)
- {
- int id = int.MinValue;
- bool isUniqueID = false;
- bool isDuplicate = false;
- while (isUniqueID == false)
- {
- id = GetClampNumber(message, 0, int.MaxValue);
- foreach (Player player in _dataBase.Players)
- {
- if (player.ID == id)
- {
- isDuplicate = true;
- break;
- }
- }
- if (isDuplicate)
- {
- Console.WriteLine("Номер игрока не универсальный. Попробуйте ещё раз.");
- isDuplicate = false;
- }
- else
- {
- isUniqueID = true;
- }
- }
- return id;
- }
- private int GetClampNumber(string message, int minValue, int maxValue)
- {
- int value = int.MinValue;
- string userInput = string.Empty;
- while (int.TryParse(userInput, out value) == false)
- {
- Console.Write($"{message}: ");
- userInput = Console.ReadLine();
- }
- value = Math.Clamp(value, minValue, maxValue);
- return value;
- }
- private bool TryGetPlayerIndex(int number, out int index)
- {
- index = int.MinValue;
- if (number > 0 && number <= _dataBase.Players.Count)
- {
- index = --number;
- return true;
- }
- else
- {
- Console.WriteLine("Номер пункта за пределами списка игроков.");
- }
- return false;
- }
- private bool TryGetPlayerIndexById(int id, out int index)
- {
- index = int.MinValue;
- for (int i = 0; i < _dataBase.Players.Count; i++)
- {
- if (_dataBase.Players[i].ID == id)
- {
- index = i;
- return true;
- }
- }
- return false;
- }
- private void ShowAllPlayers()
- {
- _dataBase.ShowAllPlayers();
- }
- private bool IsAnyInBase()
- {
- return _dataBase.Players.Any();
- }
- private bool IsCorrectIndex(int index)
- {
- return index >= 0 && index < _dataBase.Players.Count;
- }
- }
- class DataBase
- {
- private List<Player> _players = new List<Player>();
- public List<Player> Players => _players;
- public void AddPlayer(Player player)
- {
- _players.Add(player);
- }
- public void DeletePlayer(int index)
- {
- _players.Remove(_players[index]);
- }
- public void ShowAllPlayers()
- {
- for (int i = 0; i < _players.Count; i++)
- {
- Console.Write($"{i + 1}. ");
- _players[i].ShowInfo();
- }
- }
- public void CahngeBan(int index)
- {
- _players[index].CahngeBan();
- }
- }
- class Player
- {
- public string Surname { get; private set; }
- public int ID { get; private set; }
- public int Level { get; private set; }
- public bool IsBanned { get; private set; }
- public Player(string surname, int id, int level)
- {
- Surname = surname;
- ID = id;
- Level = level;
- IsBanned = false;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Фамилия: {Surname};\tУникальный номер: {ID};\tУровень: {Level};\tБан: {IsBanned};");
- }
- public void CahngeBan()
- {
- IsBanned = !IsBanned;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement