Last active
June 11, 2025 19:44
-
-
Save Strelok78/e43a9ae21f99775a0c5036d13a049b53 to your computer and use it in GitHub Desktop.
player data base (data base as class)
This file contains hidden or 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
using System; | |
using System.IO; | |
using iJuniorPractice; | |
namespace iJuniorPractice | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Database playerDatabase = new Database(); | |
DatabaseController databaseController = new DatabaseController(); | |
databaseController.OpenDatabaseController(playerDatabase); | |
} | |
} | |
static class Utils | |
{ | |
public static int ReadInt(string message = "") | |
{ | |
int command; | |
string input; | |
Console.WriteLine(message); | |
input = Console.ReadLine(); | |
while (int.TryParse(input, out command) == false) | |
{ | |
Console.WriteLine("Incorrect input"); | |
input = Console.ReadLine(); | |
} | |
return command; | |
} | |
} | |
class DatabaseController | |
{ | |
private const int CommandAddPlayer = 1; | |
private const int CommandRemovePlayer = 2; | |
private const int CommandBanPlayer = 3; | |
private const int CommandUnbanPlayer = 4; | |
private const int CommandShowPlayers = 5; | |
private const int CommandExit = 6; | |
public void ShowDatabaseControllerCommands() | |
{ | |
Console.WriteLine("MAIN MENU"); | |
Console.WriteLine | |
( | |
"Commands:\n" + | |
$"{CommandAddPlayer}. Add player\n" + | |
$"{CommandRemovePlayer}. Remove player\n" + | |
$"{CommandBanPlayer}. Ban player\n" + | |
$"{CommandUnbanPlayer}. Unban player\n" + | |
$"{CommandShowPlayers}. Show players\n" + | |
$"{CommandExit}.Exit\n" | |
); | |
} | |
public void OpenDatabaseController(Database playerDatabase) | |
{ | |
bool isWork = true; | |
while (isWork) | |
{ | |
int input; | |
ShowDatabaseControllerCommands(); | |
input = Utils.ReadInt(); | |
switch (input) | |
{ | |
case CommandAddPlayer: | |
playerDatabase.AddPlayer(); | |
break; | |
case CommandRemovePlayer: | |
playerDatabase.RemovePlayer(); | |
break; | |
case CommandBanPlayer: | |
playerDatabase.BanPlayer(); | |
break; | |
case CommandUnbanPlayer: | |
playerDatabase.UnbanPlayer(); | |
break; | |
case CommandShowPlayers: | |
playerDatabase.ShowPlayers(); | |
break; | |
case CommandExit: | |
isWork = false; | |
Console.WriteLine("Goodbye"); | |
break; | |
default: | |
Console.WriteLine("Incorrect input"); | |
break; | |
} | |
Console.WriteLine("Press any key to continue..."); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
} | |
class Player | |
{ | |
public Player(int id) | |
{ | |
Id = id; | |
Console.WriteLine("Enter name: "); | |
Name = Console.ReadLine(); | |
Level = Utils.ReadInt("Enter level: "); | |
IsBan = false; | |
} | |
public int Id { get; private set; } | |
public string Name{ get; } | |
public int Level{ get; } | |
public bool IsBan { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"Name: {Name}, Level: {Level}, is Banned: {IsBan}"); | |
} | |
public void Ban() | |
{ | |
IsBan = true; | |
} | |
public void Unban() | |
{ | |
IsBan = false; | |
} | |
} | |
class Database | |
{ | |
private List<Player> _players; | |
private int _lastPlayerId; | |
public Database() | |
{ | |
_lastPlayerId = 0; | |
_players = new List<Player>(); | |
} | |
public void AddPlayer() | |
{ | |
_players.Add(new Player(_lastPlayerId++)); | |
} | |
public void RemovePlayer() | |
{ | |
if (TryGetPlayer(out Player player)) | |
{ | |
_players.Remove(player); | |
} | |
} | |
public void BanPlayer() | |
{ | |
if (TryGetPlayer(out Player player)) | |
{ | |
player.Ban(); | |
Console.WriteLine($"{player.Name} banned"); | |
} | |
} | |
public void UnbanPlayer() | |
{ | |
if (TryGetPlayer(out Player player)) | |
{ | |
player.Unban(); | |
Console.WriteLine($"{player.Name} unbanned"); | |
} | |
} | |
public void ShowPlayers() | |
{ | |
if(_players.Count > 0) | |
{ | |
foreach (var player in _players) | |
{ | |
Console.Write($"Player Id: {player.Id}, "); | |
player.ShowInfo(); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("No players yet."); | |
} | |
} | |
private bool TryGetPlayer(out Player player) | |
{ | |
if (_players.Count > 0) | |
{ | |
int playerId = Utils.ReadInt("Enter player Id"); | |
foreach (var playerObject in _players) | |
{ | |
if (playerObject.Id == playerId) | |
{ | |
player = playerObject; | |
return true; | |
} | |
} | |
Console.WriteLine("Player not found"); | |
player = null; | |
return false; | |
} | |
else | |
{ | |
Console.WriteLine("Database is empty"); | |
player = null; | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment