Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace player_database
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Database database = new Database();
- database.Work();
- }
- }
- class Player
- {
- public Player(int id, string name, int level, bool isBanned)
- {
- Id = id;
- Name = name;
- Level = level;
- IsBanned = isBanned;
- }
- public int Id { get; private set; }
- public string Name { get; private set; }
- public int Level { get; private set; }
- public bool IsBanned { get; private set; }
- public void Block()
- {
- IsBanned = true;
- }
- public void Unblock()
- {
- IsBanned = false;
- }
- }
- class Database
- {
- private List<Player> _players = new List<Player>();
- private int _lastId = 1;
- public void Work()
- {
- const string AddPlayersCommand = "1";
- const string BlockCommand = "2";
- const string UnblockCommand = "3";
- const string DeletePlayerCommand = "4";
- const string EndProgramCommadn = "5";
- bool isWorking = true;
- while (isWorking)
- {
- DisplayPlayersInformation();
- Console.Write($"База данных игроков.\n\n\n" +
- $"Команды для работы с базой данных:\n\n" +
- $"{AddPlayersCommand} - если вы хотите добавть игроков.\n" +
- $"{BlockCommand} - если вы хотите заблокировать игрока.\n" +
- $"{UnblockCommand} - если вы хотите разблокировать игрока.\n" +
- $"{DeletePlayerCommand} - если вы хотите удалить игрока.\n" +
- $"{EndProgramCommadn} - если вы хотите выйти из программы.\n\n\n" +
- $"Ввод: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case AddPlayersCommand:
- AddPlayer();
- break;
- case BlockCommand:
- BlockPlayer();
- break;
- case UnblockCommand:
- UnblockPlayer();
- break;
- case DeletePlayerCommand:
- DeletePlayer();
- break;
- case EndProgramCommadn:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Некорректная команда.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void AddPlayer()
- {
- int id = _lastId++;
- int levelStarting = 1;
- bool isBanned = false;
- Console.WriteLine("Введите имя персонажа: ");
- string name = Console.ReadLine();
- Player user = new Player(id, name, levelStarting, isBanned);
- _players.Add(user);
- }
- private void DeletePlayer()
- {
- if (TryGetPlayer(out Player player))
- {
- _players.Remove(player);
- }
- }
- private void BlockPlayer()
- {
- if (TryGetPlayer(out Player player))
- {
- player.Block();
- }
- }
- private void UnblockPlayer()
- {
- if (TryGetPlayer(out Player player))
- {
- player.Unblock();
- }
- }
- private bool TryGetPlayer(out Player player)
- {
- int idRequired;
- player = null;
- Console.WriteLine("Введите id игрока: ");
- idRequired = ReadInt();
- foreach (Player user in _players)
- {
- if (user.Id == idRequired)
- {
- player = user;
- return true;
- }
- }
- Console.WriteLine("Игрока с таким ID не обнаружено.");
- Console.ReadKey();
- return false;
- }
- private int ReadInt()
- {
- int numberUserInput = 0;
- bool isWorking = true;
- while (isWorking)
- {
- string userInput = Console.ReadLine();
- bool isNumber = int.TryParse(userInput, out numberUserInput);
- if (isNumber == false)
- {
- Console.Write("Введите число:");
- }
- else
- {
- isWorking = false;
- }
- }
- return numberUserInput;
- }
- private void DisplayPlayersInformation()
- {
- for (int i = 0; i < _players.Count; i++)
- {
- int numberOfPlayer = i + 1;
- Console.BackgroundColor = ConsoleColor.DarkCyan;
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"Игрок - {numberOfPlayer}\n" +
- $"Имя игрока - {_players[i].Name}\n" +
- $"Уровнень игрока - {_players[i].Level}\n" +
- $"Статус блокировки игрока - {_players[i].IsBanned}\n" +
- $"Индивидуальный номер игрока - {_players[i].Id}\n\n");
- }
- Console.ResetColor();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement