Simeon1995

Program.cs

Feb 22nd, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. List<Team> teamsList = new List<Team>();
  9. string command;
  10. while ((command = Console.ReadLine()) != "END")
  11. {
  12. string[] commandArgs = command.Split(';');
  13. string mainCommand = commandArgs[0];
  14. switch (mainCommand)
  15. {
  16. case "Team":
  17. ParseTeam(commandArgs, teamsList);
  18. break;
  19. case "Add":
  20. AddPlayerToTeam(commandArgs, teamsList);
  21. break;
  22. case "Remove":
  23. RemovePlayerFromTeam(commandArgs, teamsList);
  24. break;
  25. case "Rating":
  26. string teamName = commandArgs[1];
  27. RateTeam(teamsList, teamName);
  28. break;
  29. }
  30. }
  31. }
  32.  
  33. private static void RateTeam(List<Team> teamsList, string teamName)
  34. {
  35. Team notExistingTeam = teamsList.Find(t => t.Name == teamName);
  36. if (notExistingTeam == null)
  37. {
  38. Console.WriteLine($"Team {teamName} does not exist.");
  39. return;
  40. }
  41. int rate = 0;
  42. Team team = teamsList.Find(t => t.Name == teamName);
  43. if (team.PlayersCount == 0)
  44. {
  45. Console.WriteLine($"{teamName} - 0");
  46. Environment.Exit(0);
  47. }
  48. rate = team.CalculateRate(team);
  49. Console.WriteLine($"{teamName} - {rate}");
  50. }
  51.  
  52.  
  53. private static void RemovePlayerFromTeam(string[] commandArgs, List<Team> teamsList)
  54. {
  55. try
  56. {
  57. string nameOfTeam = commandArgs[1];
  58. string playerName = commandArgs[2];
  59. Team notExistingTeam = teamsList.Find(t => t.Name == nameOfTeam);
  60. if (notExistingTeam == null)
  61. {
  62. Console.WriteLine($"Team {nameOfTeam} does not exist.");
  63. return;
  64. }
  65. Team team = teamsList.Find(t => t.Name == nameOfTeam);
  66. team.RemovePlayer(playerName);
  67. }
  68. catch (ArgumentException ex)
  69. {
  70. Console.WriteLine(ex.Message);
  71. }
  72. }
  73.  
  74. private static void AddPlayerToTeam(string[] commandArgs, List<Team> teamsList)
  75. {
  76. try
  77. {
  78. string nameOfTeam = commandArgs[1];
  79. string playerName = commandArgs[2];
  80. double overallSkill = 0.0;
  81. for (int i = 3; i < 8; i++)
  82. {
  83. overallSkill += CheckValidity(int.Parse(commandArgs[i]), i - 3);
  84. }
  85. overallSkill /= 5;
  86. Player player = new Player(playerName, overallSkill);
  87. Team notExistingTeam = teamsList.Find(t => t.Name == nameOfTeam);
  88. if (notExistingTeam == null)
  89. {
  90. Console.WriteLine($"Team {nameOfTeam} does not exist.");
  91. return;
  92. }
  93. Team team = teamsList.Find(t => t.Name == nameOfTeam);
  94. team.AddPlayer(player);
  95. }
  96. catch (ArgumentException ex)
  97. {
  98. Console.WriteLine(ex.Message);
  99. }
  100. }
  101.  
  102. private static void ParseTeam(string[] commandArgs, List<Team> teamsList)
  103. {
  104. try
  105. {
  106. string teamName = commandArgs[1];
  107. Team team = new Team(teamName);
  108. teamsList.Add(team);
  109. }
  110. catch (ArgumentException ex)
  111. {
  112. Console.WriteLine(ex.Message);
  113. }
  114. }
  115.  
  116. public static int CheckValidity(int skillLevel, int skill)
  117. {
  118. if (skillLevel < 0 || skillLevel > 100)
  119. {
  120. switch (skill)
  121. {
  122. case 0:
  123. throw new ArgumentException("Endurance should be between 0 and 100.");
  124. case 1:
  125. throw new ArgumentException("Sprint should be between 0 and 100.");
  126. case 2:
  127. throw new ArgumentException("Dribble should be between 0 and 100.");
  128. case 3:
  129. throw new ArgumentException("Passing should be between 0 and 100.");
  130. case 4:
  131. throw new ArgumentException("Shooting should be between 0 and 100.");
  132. }
  133. }
  134. return skillLevel;
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment