Last active
November 14, 2023 07:51
-
-
Save talatari/5694664e0baf11633a4c16a86dc270f9 to your computer and use it in GitHub Desktop.
40. Task: Players 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
public class Program | |
{ | |
static void Main() | |
{ | |
Database database = new Database(); | |
database.Work(); | |
} | |
} | |
public class Player | |
{ | |
public Player(int uniqueIdentifier, string nickName, int level, bool isBanned) | |
{ | |
UniqueIdentifier = uniqueIdentifier; | |
NickName = nickName; | |
Level = level; | |
IsBanned = isBanned; | |
} | |
public int UniqueIdentifier { get; private set; } | |
public string NickName { get; private set; } | |
public int Level { get; private set; } | |
public bool IsBanned { get; private set; } | |
public void Ban() | |
{ | |
IsBanned = true; | |
} | |
public void Unban() | |
{ | |
IsBanned = false; | |
} | |
} | |
public class Database | |
{ | |
private List<Player> _players = new List<Player>(); | |
private int _lastPlayerId = 0; | |
public void Work() | |
{ | |
const string AddPlayerCommand = "1"; | |
const string BanPlayerCommand = "2"; | |
const string UnbanPlayerCommand = "3"; | |
const string DeletePlayerCommand = "4"; | |
const string ExitCommand = "E"; | |
bool isWork = true; | |
while (isWork == true) | |
{ | |
Console.WriteLine("\nMenu: \n" + | |
$" {AddPlayerCommand} - Add Player\n" + | |
$" {BanPlayerCommand} - BAN Player\n" + | |
$" {UnbanPlayerCommand} - UnBAN Player\n" + | |
$" {DeletePlayerCommand} - Delete Player\n" + | |
$" {ExitCommand} - Exit\n"); | |
Console.Write("Enter command number: "); | |
string userInput = Console.ReadLine(); | |
switch (userInput.ToUpper()) | |
{ | |
case AddPlayerCommand: | |
AddPlayer(); | |
break; | |
case BanPlayerCommand: | |
BanPlayer(); | |
break; | |
case UnbanPlayerCommand: | |
UnbanPlayer(); | |
break; | |
case DeletePlayerCommand: | |
DeletePlayer(); | |
break; | |
case ExitCommand: | |
isWork = false; | |
break; | |
default: | |
Console.Clear(); | |
continue; | |
} | |
ShowPlayers(); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
private void AddPlayer() | |
{ | |
_lastPlayerId++; | |
Console.Write("\nEnter nickname player: "); | |
string userNickname = Console.ReadLine(); | |
Console.Write("Enter level player: "); | |
Int32.TryParse(Console.ReadLine(), out int userLevel); | |
bool isUserBanned = false; | |
Player player = new Player(_lastPlayerId, userNickname, userLevel, isUserBanned); | |
_players.Add(player); | |
if (_players.Contains(player) == true) | |
Console.Write("\nPlayer has been successfully added to DB.\n"); | |
} | |
private void ShowPlayers() | |
{ | |
for (int i = 0; i < _players.Count; i++) | |
{ | |
Console.Write($"\nPlayer ID: {_players[i].UniqueIdentifier}\n" + | |
$"Player Nickname: {_players[i].NickName}\n" + | |
$"Player Level: {_players[i].Level}\n" + | |
"Player Status: "); | |
if (_players[i].IsBanned == true) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine("BANned"); | |
} | |
else | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine("UnBANned"); | |
} | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
} | |
private void BanPlayer() | |
{ | |
if (TryGetPlayer(out Player player) == true) | |
{ | |
player.Ban(); | |
Console.WriteLine("\nPlayer has been successfully BANned."); | |
} | |
else | |
{ | |
Console.WriteLine("\nPlayer not found."); | |
} | |
} | |
private void UnbanPlayer() | |
{ | |
if (TryGetPlayer(out Player player) == true) | |
{ | |
player.Unban(); | |
Console.WriteLine("\nPlayer has been successfully disBANded."); | |
} | |
else | |
{ | |
Console.WriteLine("\nPlayer not found."); | |
} | |
} | |
private void DeletePlayer() | |
{ | |
if (TryGetPlayer(out Player player) == true) | |
{ | |
_players.Remove(player); | |
Console.WriteLine("\nPlayer has been successfully deleted."); | |
} | |
else | |
{ | |
Console.WriteLine("\nPlayer not found."); | |
} | |
} | |
private bool TryGetPlayer(out Player player) | |
{ | |
player = null; | |
Console.WriteLine("Enter ID player: "); | |
if (Int32.TryParse(Console.ReadLine(), out int playerId)) | |
{ | |
for (int i = 0; i < _players.Count; i++) | |
{ | |
if (_players[i].UniqueIdentifier == playerId) | |
{ | |
player = _players[i]; | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment