Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace TopServerPlayers
- {
- internal class Program
- {
- static void Main()
- {
- Game game = new Game();
- game.ShowTopPlayers();
- }
- }
- public class Game
- {
- private readonly List<Player> _players;
- private readonly PlayerBase _playerBase;
- public Game()
- {
- _playerBase = new PlayerBase();
- _players = _playerBase.Fill();
- }
- public void ShowTopPlayers()
- {
- _playerBase.ShowPlayers(_players);
- bool isRuning = true;
- while (isRuning)
- {
- string typeSort = _playerBase.ProcessMenu();
- if (typeSort.ToLower() == PlayerBase.ExitCommand.ToLower())
- {
- isRuning = false;
- }
- else if (Validate(typeSort))
- {
- List<Player> sortedPlayers = _playerBase.SelectTopPlayers(_players, typeSort);
- _playerBase.ShowPlayers(sortedPlayers);
- }
- }
- }
- private bool Validate(string typeSort)
- {
- if (typeSort == PlayerBase.LevelSortCommand || typeSort == PlayerBase.StrengthSortCommand)
- {
- return true;
- }
- Console.WriteLine("\nUnknown command. Press something..");
- Console.ReadKey();
- return false;
- }
- }
- public class PlayerBase
- {
- public const string LevelSortCommand = "1";
- public const string StrengthSortCommand = "2";
- public const string ExitCommand = "Exit";
- public List<Player> Fill()
- {
- PlayerFactory factory = new PlayerFactory();
- List<Player> players = factory.Create();
- return players;
- }
- public string ProcessMenu()
- {
- Console.WriteLine($"\nSelect command: " +
- $"\nshow top level - {PlayerBase.LevelSortCommand}" +
- $"\nshow top strength - {PlayerBase.StrengthSortCommand}" +
- $"\n\"{ExitCommand}\" for exit");
- return Console.ReadLine();
- }
- public void ShowPlayers(List<Player> players)
- {
- Console.WriteLine($"Name\t\tLevel\tStrength");
- foreach (Player player in players)
- {
- string tabulation = FixTabulation(player.Name);
- Console.WriteLine($"{player.Name}{tabulation}{player.Level}\t{player.Strength}");
- }
- }
- public List<Player> SelectTopPlayers(List<Player> players, string typeSort)
- {
- int countRecords = 3;
- List<Player> selectedPlayers = new List<Player>();
- if (typeSort == LevelSortCommand)
- {
- selectedPlayers = players.OrderByDescending(player =>player.Level).ThenByDescending(player => player.Strength).Take(countRecords).ToList();
- }
- else if (typeSort == StrengthSortCommand)
- {
- selectedPlayers = players.OrderByDescending(player => player.Strength).ThenByDescending(player => player.Level).Take(countRecords).ToList();
- }
- return selectedPlayers;
- }
- private string FixTabulation(string line)
- {
- string tabulation = "\t";
- int maxLength = 7;
- if (line.Length <= maxLength)
- {
- tabulation += tabulation;
- }
- return tabulation;
- }
- }
- public class PlayerFactory
- {
- private readonly List<string> _names;
- private readonly Random _random;
- public PlayerFactory()
- {
- _random = new Random();
- _names = new List<string>{"Aleksandr", "Nikolaj", "Ivan", "Sergej", "Vladimir", "Mikhail", "Vasilij", "Aleksej", "Andrej",
- "Dmitrij", "Viktor", "Yurij", "Petr", "Anatolij", "Pavel", "Igor", "Evgenij", "Oleg", "Valerij", "Vitalij", "Grigorij",
- "Fedor", "Boris", "Gennadij", "Leonid", "Konstantin", "Vyacheslav", "Georgij", "Denis", "Valentin", "Maksim", "Vadim",
- "Roman", "Ilya", "Ruslan", "Stepan", "Semen", "Anton", "Eduard", "Stanislav"};
- }
- public List<Player> Create()
- {
- int playersCount = 20;
- List<Player> players = new List<Player>();
- for (int i = 0; i < playersCount; i++)
- {
- players.Add(new Player(GetName(), GetIntValue(), GetIntValue()));
- }
- return players;
- }
- public string GetName()
- {
- int maxValue = 99;
- return _names[_random.Next(_names.Count)] + _random.Next(maxValue + 1);
- }
- public int GetIntValue()
- {
- int minValue = 1;
- int maxValue = 10;
- return _random.Next(minValue, maxValue + 1);
- }
- }
- public class Player
- {
- public Player(string name, int level, int strength)
- {
- Name = name;
- Level = level;
- Strength = strength;
- }
- public string Name { get; private set; }
- public int Level { get; private set; }
- public int Strength { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement