Last active
December 2, 2020 21:26
-
-
Save Hellhackee/53d09a20fac21bc5b2c0157ec70f8403 to your computer and use it in GitHub Desktop.
CS Light Lesson 30.1 (database)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
bool isOpen = true; | |
string input; | |
int inputIndex; | |
//Не стал создавать команду в меню для добавления игрока, потому что много описывать придется | |
Player player1 = new Player(1, "Dominator", 18, false); | |
Player player2 = new Player(2, "Hazker", 21, false); | |
Database database = new Database(new List<Player>() { player1, player2 }); | |
database.AddPlayer(3, "Ranger111", 13, true); | |
Console.WriteLine("Добро пожаловать."); | |
while (isOpen) | |
{ | |
Console.WriteLine("Вам доступны следующие команды: "); | |
Console.WriteLine(" 1. Показать всех игроков\n 2. Удалить игрока\n" + | |
" 3. Забанить игрока\n 4. Разбанить игрока\n 5. Выход"); | |
input = Console.ReadLine(); | |
switch (input) | |
{ | |
case "1": | |
database.ShowAllPlayers(); | |
break; | |
case "2": | |
Console.Write("Введите порядковый номер игрока, которого нужно удалить: "); | |
inputIndex = Convert.ToInt32(Console.ReadLine()); | |
if (!database.TryDeletePlayer(inputIndex)) | |
Console.WriteLine("Ошибка удаления. Игрок с таким порядковым номером не найден."); | |
break; | |
case "3": | |
Console.Write("Введите порядковый номер игрока, которого нужно забанить: "); | |
inputIndex = Convert.ToInt32(Console.ReadLine()); | |
if (!database.TryBanPlayer(inputIndex)) | |
Console.WriteLine("Ошибка бана. Игрок с таким порядковым номером не найден."); | |
break; | |
case "4": | |
Console.Write("Введите порядковый номер игрока, которого нужно разбанить: "); | |
inputIndex = Convert.ToInt32(Console.ReadLine()); | |
if (!database.TryUnbanPlayer(inputIndex)) | |
Console.WriteLine("Ошибка разбана. Игрок с таким порядковым номером не найден."); | |
break; | |
case "5": | |
isOpen = false; | |
break; | |
default: | |
Console.WriteLine("Извините, такой команды нет."); | |
break; | |
} | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
} | |
class Database | |
{ | |
private List<Player> _players = new List<Player>(); | |
public Database(List<Player> players) | |
{ | |
_players = players; | |
} | |
public void ShowAllPlayers() | |
{ | |
foreach (var player in _players) | |
{ | |
player.ShowInfo(); | |
} | |
Console.WriteLine(); | |
} | |
public void AddPlayer(int index, string nickName, int level, bool isBanned) | |
{ | |
Player player = new Player(index, nickName, level, isBanned); | |
_players.Add(player); | |
} | |
public bool TryBanPlayer(int index) | |
{ | |
if (CanGetPlayer(index, out Player foundPlayer)) | |
{ | |
foundPlayer.SetBanValue(true); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
public bool TryUnbanPlayer(int index) | |
{ | |
if (CanGetPlayer(index, out Player foundPlayer)) | |
{ | |
foundPlayer.SetBanValue(false); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
public bool TryDeletePlayer(int index) | |
{ | |
if (CanGetPlayer(index, out Player foundPlayer)) | |
{ | |
_players.Remove(foundPlayer); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
private bool CanGetPlayer(int index, out Player foundPlayer) | |
{ | |
foundPlayer = _players.Find(x => x.Index == index); | |
return foundPlayer != null; | |
} | |
} | |
class Player | |
{ | |
public int Index { get; private set; } | |
private string _nickName; | |
private int _level; | |
private bool _isBanned; | |
public Player(int index, string nickName, int level, bool isBanned) | |
{ | |
Index = index; | |
_nickName = nickName; | |
_level = level; | |
_isBanned = isBanned; | |
} | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"Порядковый номер: {Index}, Ник: {_nickName}, Уровень: {_level}, Забанен: {_isBanned}"); | |
} | |
public void SetBanValue(bool value) | |
{ | |
_isBanned = value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment