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 const string ExitCommand = "Exit";
- 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 = ProcessMenu();
- if (typeSort.ToLower() == ExitCommand.ToLower())
- {
- isRuning = false;
- }
- else if (Validate(typeSort))
- {
- List<Top> sortedPlayers = _playerBase.SelectTopPlayers(_players, typeSort);
- _playerBase.ShowTopPlayers(sortedPlayers);
- }
- }
- }
- private string ProcessMenu()
- {
- Console.WriteLine($"\nSelect command: " +
- $"\nshow top level - {PlayerBase.LevelSortCommand}" +
- $"\nshow top strength - {PlayerBase.StrengthSortCommand}" +
- $"\n\"{ExitCommand}\" for exit");
- return Console.ReadLine();
- }
- private bool Validate(string typeSort)
- {
- if (typeSort == PlayerBase.LevelSortCommand)
- {
- return true;
- }
- else if (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 List<Player> Fill()
- {
- PlayerFactory factory = new PlayerFactory();
- List<Player> players = factory.Create();
- return players;
- }
- 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 void ShowTopPlayers(List<Top> players)
- {
- int currentPlace = 0;
- foreach (Top player in players)
- {
- if (currentPlace != player.Place)
- {
- Console.WriteLine($"\nPlace {player.Place} ");
- currentPlace = player.Place;
- }
- string tabulation = FixTabulation(player.Player.Name);
- Console.WriteLine($"{player.Player.Name}{tabulation}level: {player.Player.Level}\t strength: {player.Player.Strength}");
- }
- }
- public List<Top> SelectTopPlayers(List<Player> players, string typeSort)
- {
- int countRecords = 3;
- List<Top> selectedPlayers = new List<Top>();
- List<int> topCharacteristic = null;
- if (typeSort == LevelSortCommand)
- {
- topCharacteristic = players.Select(player => player.Level).Distinct().OrderByDescending(level => level).Take(countRecords).ToList();
- }
- else if (typeSort == StrengthSortCommand)
- {
- topCharacteristic = players.Select(player => player.Strength).Distinct().OrderByDescending(strength => strength).Take(countRecords).ToList();
- }
- foreach (Player player in players)
- {
- for (int i = 0; i < countRecords; i++)
- {
- if (typeSort == LevelSortCommand)
- {
- if (player.Level == topCharacteristic[i])
- {
- selectedPlayers.Add(new Top(i + 1, player));
- }
- }
- else if (typeSort == StrengthSortCommand)
- {
- if (player.Strength == topCharacteristic[i])
- {
- selectedPlayers.Add(new Top(i + 1, player));
- }
- }
- }
- }
- selectedPlayers = selectedPlayers.OrderBy(player => player.Place).ToList();
- return selectedPlayers;
- }
- private string FixTabulation(string line)
- {
- string tabulation = "\t";
- int maxLength = 7;
- if (line.Length <= maxLength)
- {
- tabulation += tabulation;
- }
- return tabulation;
- }
- }
- public class Top
- {
- public Top(int place, Player player)
- {
- Place = place;
- Player = player;
- }
- public int Place { get; private set; }
- public Player Player { get; private set; }
- }
- 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