Advertisement
Guest User

RankList - Task - Exam - 2017 - January

a guest
Aug 31st, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace PlayerRankList
  8. {
  9.     class Program
  10.     {
  11.         public static StringBuilder resultMessages = new StringBuilder();
  12.  
  13.         public static List<Player> rankList = new List<Player>();
  14.  
  15.         public static Dictionary<string, SortedSet<Player>> playersByType = new Dictionary<string, SortedSet<Player>>();
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             var line = Console.ReadLine();
  20.  
  21.             while (line != "end")
  22.             {
  23.                 var command = line.Split(' ').ToArray();
  24.  
  25.                 switch (command[0])
  26.                 {
  27.                     case "add":
  28.                         AddPlayer(command);
  29.                         break;
  30.                     case "find":
  31.                         Find(command[1]);
  32.                         break;
  33.                     case "ranklist":
  34.                         RankPlayers(int.Parse(command[1]), int.Parse(command[2]));
  35.                         break;
  36.                 }
  37.  
  38.                 line = Console.ReadLine();
  39.             }
  40.  
  41.             Console.WriteLine(resultMessages.ToString().Trim());
  42.         }
  43.  
  44.         public static void AddPlayer(string[] command)
  45.         {
  46.             var player = new Player(command[1], command[2], int.Parse(command[3]), int.Parse(command[4]));
  47.  
  48.             if (!playersByType.ContainsKey(player.Type))
  49.             {
  50.                 playersByType[player.Type] = new SortedSet<Player>();
  51.             }
  52.  
  53.             playersByType[player.Type].Add(player);
  54.             rankList.Insert(player.Position - 1, player);
  55.  
  56.             var msg = string.Format("Added player {0} to position {1}", player.Name, player.Position);
  57.  
  58.             resultMessages.AppendLine(msg);
  59.         }
  60.  
  61.         public static void Find(string type)
  62.         {
  63.             var playersWithTheCurrentType = playersByType[type].Take(5);
  64.  
  65.             var msg = string.Format("Type {0}: {1}", type, string.Join("; ", playersWithTheCurrentType));
  66.  
  67.             resultMessages.AppendLine(msg);
  68.         }
  69.  
  70.         public static void RankPlayers(int start, int end)
  71.         {
  72.             var rankedPlayers = new Player[end];
  73.  
  74.             for (int i = start - 1; i <= end - 1; i++)
  75.             {
  76.                 rankedPlayers[i] = rankList[i];
  77.             }
  78.  
  79.             var org = rankedPlayers.Select(r => new { Position = ++start - 1, Player = r }).ToArray();
  80.            
  81.             resultMessages.AppendLine(string.Join("; ", org.Select(r => string.Format("{0}. {1}", r.Position, r.Player ))));
  82.         }
  83.     }
  84.  
  85.     public class Player : IComparable<Player>
  86.     {
  87.         public Player(string name, string type, int age, int position)
  88.         {
  89.             this.Name = name;
  90.             this.Age = age;
  91.             this.Type = type;
  92.             this.Position = position;
  93.         }
  94.  
  95.         public string Name { get; set; }
  96.         public int Age { get; set; }
  97.         public string Type { get; set; }
  98.         public int Position { get; set; }
  99.  
  100.  
  101.         public int CompareTo(Player other)
  102.         {
  103.             var result = this.Name.CompareTo(other.Name);
  104.  
  105.             if (result == 0)
  106.             {
  107.                 result = this.Age.CompareTo(other.Age) * -1;
  108.             }
  109.  
  110.             return result;
  111.         }
  112.  
  113.         public override string ToString()
  114.         {
  115.             return string.Format("{0}({1})", this.Name, this.Age);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement