Vapio

task41

Jun 21st, 2021 (edited)
866
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main(string[] args)
  8.     {
  9.         Server server = new Server();
  10.  
  11.         server.ShowPlayersTopLevel();
  12.         server.ShowPlayersTopPower();
  13.     }
  14. }
  15.  
  16. public class Server
  17. {
  18.     private Random _random;
  19.     private List<Player> _players;
  20.     private int _playersTopAmount;
  21.  
  22.     public Server()
  23.     {
  24.         _players = new List<Player>();
  25.         _random = new Random();
  26.         _playersTopAmount = 3;
  27.         CreatePlayers();
  28.     }
  29.  
  30.     public void ShowPlayersTopLevel()
  31.     {
  32.         List<Player> playersTopLevel = _players.OrderByDescending(player => player.Level)
  33.                                         .Take(_playersTopAmount).ToList();
  34.         Console.WriteLine("\nTop Level : ");
  35.         PrintPlayers(playersTopLevel);
  36.     }
  37.  
  38.     public void ShowPlayersTopPower()
  39.     {
  40.         List<Player> playersTopPower = _players.OrderByDescending(player => player.Power)
  41.                                         .Take(_playersTopAmount).ToList();
  42.         Console.WriteLine("\nTop Power : ");
  43.         PrintPlayers(playersTopPower);
  44.     }
  45.  
  46.     private void CreatePlayers()
  47.     {
  48.         int playersAmountMinimum = 10;
  49.         int playersAmountMaximum = 100;
  50.  
  51.         string[] _names = { "Mike", "Nick", "Valera", "Virgil", "Dante" };
  52.  
  53.         int levelMinimum = 14;
  54.         int levelMaximum = 89;
  55.  
  56.         int powerMinimum = 40;
  57.         int powerMaximum = 250;
  58.  
  59.         int amountPlayers = _random.Next(playersAmountMinimum, playersAmountMaximum);
  60.  
  61.         string name;
  62.         int level;
  63.         int power;
  64.  
  65.         for (int i = 0; i < amountPlayers; ++i)
  66.         {
  67.             name = _names[_random.Next(names.Length)];
  68.             level = _random.Next(levelMinimum, levelMaximum);
  69.             power = _random.Next(powerMinimum, powerMaximum);
  70.             _players.Add(new Player(name, level, power));
  71.         }
  72.     }
  73.  
  74.     private void PrintPlayers(List<Player> players)
  75.     {
  76.         Console.WriteLine();
  77.  
  78.         foreach (Player player in players)
  79.             Console.WriteLine(player);
  80.     }
  81. }
  82.  
  83. public class Player
  84. {
  85.     public string Name { get; private set; }
  86.     public int Level { get; private set; }
  87.     public int Power { get; private set; }
  88.  
  89.     public Player(string name, int level, int power)
  90.     {
  91.         Name = name;
  92.         Level = level;
  93.         Power = power;
  94.     }
  95.  
  96.     public override string ToString()
  97.     {
  98.         return String.Format($"{Name}({Level} lvl.) - {Power}"); ;
  99.     }
  100. }
RAW Paste Data