Advertisement
Guest User

refactored Dobri - Player Ranking

a guest
Jan 19th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Wintellect.PowerCollections;
  5.  
  6. namespace _06V5Kiro
  7. {
  8. public class Program
  9. {
  10. public static BigList<Player> playerRanking = new BigList<Player>();
  11. public static Dictionary<string, SortedSet<Player>> playerType = new Dictionary<string, SortedSet<Player>>();
  12. public static StringBuilder ForPrint = new StringBuilder();
  13.  
  14. static void Main(string[] args)
  15. {
  16. string commParams;
  17. while ((commParams = Console.ReadLine()) != "end")
  18. {
  19. switch (commParams[0])
  20. {
  21. case 'a':
  22. AddPlayer(commParams);
  23. break;
  24. case 'f':
  25. Find(commParams);
  26. break;
  27. case 'r':
  28. Ranklist(commParams);
  29. break;
  30. }
  31. }
  32. Console.WriteLine(ForPrint.ToString());
  33. }
  34.  
  35. public static void AddPlayer(string comPars)
  36. {
  37. var commands = comPars.Split();
  38.  
  39. string name = commands[1];
  40. string type = commands[2];
  41. int age = int.Parse(commands[3]);
  42. int pos = int.Parse(commands[4]);
  43.  
  44. Player playerToBeAdded = new Player(name, type, age);
  45. playerRanking.Insert(pos - 1, playerToBeAdded);
  46.  
  47. if (playerType.ContainsKey(type))
  48. {
  49. if (playerType[type].Count == 5)
  50. {
  51. Player lastPlayer = playerType[type].Max;
  52. if (lastPlayer.CompareTo(playerToBeAdded) > 0)
  53. {
  54. playerType[type].Remove(lastPlayer);
  55. playerType[type].Add(playerToBeAdded);
  56. }
  57. }
  58. else
  59. {
  60. playerType[type].Add(playerToBeAdded);
  61. }
  62. }
  63. else
  64. {
  65. playerType[type] = new SortedSet<Player>();
  66. playerType[type].Add(playerToBeAdded);
  67. }
  68.  
  69. ForPrint.AppendLine($"Added player {playerToBeAdded.Name} to position {pos}");
  70. }
  71.  
  72. public static void Find(string comPars)
  73. {
  74. var commands = comPars.Split();
  75.  
  76. string type = commands[1];
  77.  
  78. if (playerType.ContainsKey(type))
  79. {
  80. ForPrint.Append($"Type {type}: ");
  81. foreach (Player player in playerType[type])
  82. {
  83. ForPrint.Append($" {player};");
  84. }
  85.  
  86. ForPrint.Remove(ForPrint.Length - 1, 1);
  87. ForPrint.AppendLine();
  88. }
  89. else
  90. {
  91. ForPrint.Append($"Type {type}: ");
  92. ForPrint.AppendLine();
  93. }
  94. }
  95.  
  96. public static void Ranklist(string comPars)
  97. {
  98. var commands = comPars.Split();
  99.  
  100. int startInd = int.Parse(commands[1]);
  101. int endInd = int.Parse(commands[2]);
  102. var askedRange = playerRanking.Range(startInd - 1, endInd - startInd + 1);
  103. int playerPosition = startInd;
  104.  
  105. foreach (Player player in askedRange)
  106. {
  107. ForPrint.AppendFormat("{0}. {1}; ", playerPosition++, player);
  108. }
  109.  
  110. ForPrint = ForPrint.Remove(ForPrint.Length - 2, 2);
  111. ForPrint = ForPrint.AppendLine();
  112. }
  113. }
  114.  
  115. public class Player : IComparable<Player>
  116. {
  117. private string name;
  118. private string type;
  119. private int age;
  120. public Player(string name, string type, int age)
  121. {
  122. this.Name = name;
  123. this.Type = type;
  124. this.Age = age;
  125. }
  126. public string Name
  127. {
  128. get { return this.name; }
  129. private set
  130. {
  131. this.name = value;
  132. }
  133. }
  134. public string Type
  135. {
  136. get { return this.type; }
  137. private set
  138. {
  139. this.type = value;
  140. }
  141. }
  142. public int Age
  143. {
  144. get { return this.age; }
  145. private set
  146. {
  147. this.age = value;
  148. }
  149. }
  150. public int CompareTo(Player other)
  151. {
  152. if (this.Name.CompareTo(other.Name) == 0)
  153. {
  154. return this.Age.CompareTo(other.Age) * -1;
  155. }
  156.  
  157. return this.Name.CompareTo(other.Name);
  158. }
  159. public override string ToString()
  160. {
  161. return string.Format($"{this.Name}({this.Age})");
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement