Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace CSLight
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string AddPlayer = "1";
- const string BanPlayer = "2";
- const string UnbanPlayer = "3";
- const string DeletePlayer = "4";
- const string PrintAllPlayers = "5";
- const string ExitProgram = "6";
- Database database = new Database();
- bool isWorking = true;
- while (isWorking)
- {
- Console.WriteLine($"{AddPlayer})Добавить игрока.\n{BanPlayer})Забанить игрока.\n{UnbanPlayer})Разбанить игрока.\n{DeletePlayer})Удалить игрока." +
- $"\n{PrintAllPlayers})Вывести список всех игроков.\n{ExitProgram})Выход из программы.");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case AddPlayer:
- database.AddPlayer();
- break;
- case BanPlayer:
- database.BanPlayer();
- break;
- case UnbanPlayer:
- database.UnbanPlayer();
- break;
- case DeletePlayer:
- database.DeletePlayer();
- break;
- case PrintAllPlayers:
- database.PrintAllPlayers();
- break;
- case ExitProgram:
- isWorking = false;
- break;
- default:
- Console.WriteLine("Неверный выбор. Попробуйте еще раз.");
- break;
- }
- }
- }
- }
- class Database
- {
- private Dictionary<int, Player> _players = new Dictionary<int, Player>();
- private int _nextIdPlayer = 1;
- public void AddPlayer()
- {
- Console.WriteLine("Введите ник игрока.");
- string nickname = Console.ReadLine();
- Console.WriteLine("Введите уровень");
- int level = ReadInt("Уровень должен быть числом. Попробуйте еще раз:");
- Player player = new Player(_nextIdPlayer, nickname, level);
- _players.Add(_nextIdPlayer, player);
- _nextIdPlayer++;
- Console.WriteLine($"Игрок {nickname} добавлен с ID {player.Id}");
- }
- public void BanPlayer()
- {
- int id = ReadInt("ID игрока должен быть числом. Попробуйте еще раз:");
- if (TryGetPlayer(id, out Player player))
- {
- player.Ban();
- Console.WriteLine($"Игрок {player.Nickname} (ID: {id}) забанен.");
- }
- }
- public void UnbanPlayer()
- {
- int id = ReadInt("ID игрока должен быть числом. Попробуйте еще раз:");
- if (TryGetPlayer(id, out Player player))
- {
- player.Unban();
- Console.WriteLine($"Игрок {player.Nickname} (ID: {id}) разбанен.");
- }
- }
- public void DeletePlayer()
- {
- int id = ReadInt("ID игрока должен быть числом. Попробуйте еще раз:");
- if (_players.Remove(id, out Player player))
- {
- Console.WriteLine($"Игрок {player.Nickname} (ID: {id}) удален.");
- }
- else
- {
- Console.WriteLine($"Игрок с ID {id} не найден.");
- }
- }
- public void PrintAllPlayers()
- {
- if (_players.Count == 0)
- {
- Console.WriteLine("В базе нет игроков.");
- }
- else
- {
- Console.WriteLine("Список игроков:");
- foreach (var player in _players.Values)
- {
- player.ShowInfo();
- }
- }
- }
- private bool TryGetPlayer(int id, out Player player)
- {
- if (_players.TryGetValue(id, out player))
- {
- return true;
- }
- Console.WriteLine($"Игрок с ID {id} не найден.");
- return false;
- }
- private int ReadInt(string errorMessage)
- {
- int result;
- while (!int.TryParse(Console.ReadLine(), out result))
- {
- Console.WriteLine(errorMessage);
- }
- return result;
- }
- }
- class Player
- {
- public int Id { get; private set; }
- public string Nickname { get; private set; }
- public int Level { get; private set; }
- public bool IsBanned { get; private set; }
- public Player(int uniqueNumber, string nickname, int lvl, bool isBanned = false)
- {
- Id = uniqueNumber;
- Nickname = nickname;
- Level = lvl;
- IsBanned = isBanned;
- }
- public void Ban()
- {
- IsBanned = true;
- }
- public void Unban()
- {
- IsBanned = false;
- }
- public void ShowInfo()
- {
- string status = IsBanned ? "Забанен" : "Активен";
- Console.WriteLine($"ID: {Id} | Ник: {Nickname} | Уровень: {Level} | Статус: {status}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement