Created
August 17, 2023 12:45
-
-
Save talatari/91c4d55c834486d599bd2c6f223c0d3f to your computer and use it in GitHub Desktop.
Repeat practice
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(); | |
} | |
} | |
class Player | |
{ | |
public Player(int uniqueIdentifier, string name, int level, bool isBanned) | |
{ | |
UniqueIdentifier = uniqueIdentifier; | |
Name = name; | |
Level = level; | |
IsBanned = isBanned; | |
} | |
public int UniqueIdentifier { get; private set; } | |
public string Name { 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; | |
} | |
} | |
class Database | |
{ | |
private List<Player> _players = new List<Player>(); | |
private int _playerId = 0; | |
public void Work() | |
{ | |
const string AddPlayerCommand = "1"; | |
const string BanPlayerCommand = "2"; | |
const string UnbanPlayerCommand = "3"; | |
const string DeletePlayerCommand = "4"; | |
const string ExitCommand = "5"; | |
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: "); | |
string userInput = Console.ReadLine(); | |
switch (userInput) | |
{ | |
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; | |
} | |
ShowAllPlayers(); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
private void AddPlayer() | |
{ | |
string playerName = ""; | |
int playerLevel = 1; | |
bool isBanned = false; | |
Console.Write("\nEnter player name: "); | |
playerName = Console.ReadLine(); | |
Console.Write("Enter player level: "); | |
Int32.TryParse(Console.ReadLine(), out playerLevel); | |
_playerId++; | |
Player player = new Player(_playerId, playerName, playerLevel, isBanned); | |
_players.Add(player); | |
} | |
private void BanPlayer() | |
{ | |
if (TryGetPlayer(out Player player) == true) | |
{ | |
player.Ban(); | |
Console.WriteLine("\nPlayer successed banned."); | |
} | |
} | |
private void UnbanPlayer() | |
{ | |
if (TryGetPlayer(out Player player) == true) | |
{ | |
player.Unban(); | |
Console.WriteLine("\nPlayer successed disbanned."); | |
} | |
} | |
private void DeletePlayer() | |
{ | |
if (TryGetPlayer(out Player player) == true) | |
{ | |
_players.Remove(player); | |
Console.WriteLine("\nPlayer successed deleted."); | |
} | |
} | |
private void ShowAllPlayers() | |
{ | |
for (int i = 0; i < _players.Count; i++) | |
{ | |
Console.Write($"\n[Player Info]\n" + | |
$" + ID: {_players[i].UniqueIdentifier}\n" + | |
$" + Name: {_players[i].Name}\n" + | |
$" + Level: {_players[i].Level}\n" + | |
$" + Status: "); | |
if (_players[i].IsBanned == true) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.Write("Banned\n"); | |
} | |
else | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.Write("Unbanned\n"); | |
} | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
} | |
private bool TryGetPlayer(out Player player) | |
{ | |
player = null; | |
Console.Write("Enter ID Player: "); | |
if (Int32.TryParse(Console.ReadLine(), out int playerID) == true) | |
{ | |
for (int i = 0; i < _players.Count; i++) | |
{ | |
if (_players[i].UniqueIdentifier == playerID) | |
{ | |
player = _players[i]; | |
return true; | |
} | |
} | |
} | |
Console.WriteLine("\nPlayer not found."); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment