Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.HashSet;
  7. import java.util.Set;
  8.  
  9. public class Main {
  10. public static void main(String[] args) throws IOException {
  11. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12. Set<Team> teams = new HashSet<>();
  13. String input = reader.readLine();
  14.  
  15. while (!"END".equals(input)) {
  16. String[] tokens = input.split(";");
  17. boolean isThereSuchTeam;
  18. String teamName;
  19. String playerName;
  20. switch (tokens[0]) {
  21. case "Team":
  22. isThereSuchTeam = false;
  23. teamName = tokens[1];
  24. for (Team team : teams) {
  25. if (team.getName().equals(teamName)) {
  26. isThereSuchTeam = true;
  27. team = new Team(teamName);
  28. }
  29. }
  30. if (!isThereSuchTeam) {
  31. try {
  32. Team team = new Team(teamName);
  33. teams.add(team);
  34. } catch (IllegalArgumentException iae) {
  35. System.out.println(iae.getMessage());
  36. }
  37. }
  38. break;
  39. case "Add":
  40. teamName = tokens[1];
  41. playerName = tokens[2];
  42. Integer endurance = Integer.parseInt(tokens[3]);
  43. Integer sprint = Integer.parseInt(tokens[4]);
  44. Integer dribble = Integer.parseInt(tokens[5]);
  45. Integer passing = Integer.parseInt(tokens[6]);
  46. Integer shooting = Integer.parseInt(tokens[7]);
  47. isThereSuchTeam = false;
  48. for (Team team : teams) {
  49. if (team.getName().equals(teamName)) {
  50. isThereSuchTeam = true;
  51. try {
  52. team.addPlayer(playerName, endurance, sprint, dribble, passing, shooting);
  53. } catch (IllegalArgumentException iae) {
  54. System.out.println(iae.getMessage());
  55. }
  56. break;
  57. }
  58. }
  59. if (!isThereSuchTeam) {
  60. System.out.printf("Team %s does not exist.%n", teamName);
  61. }
  62. break;
  63. case "Remove":
  64. teamName = tokens[1];
  65. playerName = tokens[2];
  66. isThereSuchTeam = false;
  67. for (Team team : teams) {
  68. if (team.getName().equals(teamName)) {
  69. isThereSuchTeam = true;
  70. try {
  71. team.removePlayer(playerName);
  72. } catch (IllegalArgumentException iae) {
  73. System.out.println(iae.getMessage());
  74. }
  75. break;
  76. }
  77. }
  78. if (!isThereSuchTeam) {
  79. System.out.printf("Team %s does not exist.%n", teamName);
  80. }
  81. break;
  82. case "Rating":
  83. teamName = tokens[1];
  84. isThereSuchTeam = false;
  85. for (Team team : teams) {
  86. if (team.getName().equals(teamName)) {
  87. isThereSuchTeam = true;
  88. System.out.printf("%s - %d%n", teamName, team.getRating());
  89. break;
  90. }
  91. }
  92. if (!isThereSuchTeam) {
  93. System.out.printf("Team %s does not exist.%n", teamName);
  94. }
  95. break;
  96. }
  97. input = reader.readLine();
  98. }
  99. }
  100. }
  101.  
  102. package test;
  103.  
  104. import java.util.HashMap;
  105. import java.util.Map;
  106.  
  107. public class Team {
  108. private String name;
  109. private Map<String, Double> players;
  110.  
  111. public Team(String name) {
  112. this.setName(name);
  113. this.players = new HashMap<>();
  114. }
  115.  
  116. public String getName() {
  117. return this.name;
  118. }
  119.  
  120. private void setName(String name) {
  121. if (name == null || name.trim().isEmpty()){
  122. throw new IllegalArgumentException("A name should not be empty. ");
  123. }
  124. this.name = name;
  125. }
  126.  
  127.  
  128. public void addPlayer(String name, int endurance, int sprint, int dribble, int passing, int shooting){
  129. if (name == null || name.trim().isEmpty()){
  130. throw new IllegalArgumentException("A name should not be empty. ");
  131. }
  132. if (endurance < 0 || endurance > 100) {
  133. throw new IllegalArgumentException("Endurance should be between 0 and 100.");
  134. }
  135. if (sprint < 0 || sprint > 100) {
  136. throw new IllegalArgumentException("Sprint should be between 0 and 100.");
  137. }
  138. if (dribble < 0 || dribble > 100) {
  139. throw new IllegalArgumentException("Dribble should be between 0 and 100.");
  140. }
  141. if (passing < 0 || passing > 100) {
  142. throw new IllegalArgumentException("Passing should be between 0 and 100.");
  143. }
  144. if (shooting < 0 || shooting > 100) {
  145. throw new IllegalArgumentException("Shooting should be between 0 and 100.");
  146. }
  147.  
  148. double skill = (endurance + sprint + dribble + passing + shooting) / 5.0;
  149. this.players.put(name, skill);
  150. }
  151.  
  152. public void removePlayer(String name) {
  153. if (!this.players.containsKey(name)) {
  154. throw new IllegalArgumentException(String.format("Player %s is not in %s team.", name, this.getName()));
  155. }
  156. this.players.remove(name);
  157. }
  158.  
  159. public int getRating() {
  160. int rating = 0;
  161. double sum = 0.0;
  162. if (!this.players.isEmpty()) {
  163. for (Map.Entry<String, Double> player : this.players.entrySet()) {
  164. sum += player.getValue();
  165. }
  166. rating = (int)Math.round(sum/this.players.size());
  167. }
  168. return rating;
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement