Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashSet;
- import java.util.Set;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Set<Team> teams = new HashSet<>();
- String input = reader.readLine();
- while (!"END".equals(input)) {
- String[] tokens = input.split(";");
- boolean isThereSuchTeam;
- String teamName;
- String playerName;
- switch (tokens[0]) {
- case "Team":
- isThereSuchTeam = false;
- teamName = tokens[1];
- for (Team team : teams) {
- if (team.getName().equals(teamName)) {
- isThereSuchTeam = true;
- team = new Team(teamName);
- }
- }
- if (!isThereSuchTeam) {
- try {
- Team team = new Team(teamName);
- teams.add(team);
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- }
- }
- break;
- case "Add":
- teamName = tokens[1];
- playerName = tokens[2];
- Integer endurance = Integer.parseInt(tokens[3]);
- Integer sprint = Integer.parseInt(tokens[4]);
- Integer dribble = Integer.parseInt(tokens[5]);
- Integer passing = Integer.parseInt(tokens[6]);
- Integer shooting = Integer.parseInt(tokens[7]);
- isThereSuchTeam = false;
- for (Team team : teams) {
- if (team.getName().equals(teamName)) {
- isThereSuchTeam = true;
- try {
- team.addPlayer(playerName, endurance, sprint, dribble, passing, shooting);
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- }
- break;
- }
- }
- if (!isThereSuchTeam) {
- System.out.printf("Team %s does not exist.%n", teamName);
- }
- break;
- case "Remove":
- teamName = tokens[1];
- playerName = tokens[2];
- isThereSuchTeam = false;
- for (Team team : teams) {
- if (team.getName().equals(teamName)) {
- isThereSuchTeam = true;
- try {
- team.removePlayer(playerName);
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- }
- break;
- }
- }
- if (!isThereSuchTeam) {
- System.out.printf("Team %s does not exist.%n", teamName);
- }
- break;
- case "Rating":
- teamName = tokens[1];
- isThereSuchTeam = false;
- for (Team team : teams) {
- if (team.getName().equals(teamName)) {
- isThereSuchTeam = true;
- System.out.printf("%s - %d%n", teamName, team.getRating());
- break;
- }
- }
- if (!isThereSuchTeam) {
- System.out.printf("Team %s does not exist.%n", teamName);
- }
- break;
- }
- input = reader.readLine();
- }
- }
- }
- package test;
- import java.util.HashMap;
- import java.util.Map;
- public class Team {
- private String name;
- private Map<String, Double> players;
- public Team(String name) {
- this.setName(name);
- this.players = new HashMap<>();
- }
- public String getName() {
- return this.name;
- }
- private void setName(String name) {
- if (name == null || name.trim().isEmpty()){
- throw new IllegalArgumentException("A name should not be empty. ");
- }
- this.name = name;
- }
- public void addPlayer(String name, int endurance, int sprint, int dribble, int passing, int shooting){
- if (name == null || name.trim().isEmpty()){
- throw new IllegalArgumentException("A name should not be empty. ");
- }
- if (endurance < 0 || endurance > 100) {
- throw new IllegalArgumentException("Endurance should be between 0 and 100.");
- }
- if (sprint < 0 || sprint > 100) {
- throw new IllegalArgumentException("Sprint should be between 0 and 100.");
- }
- if (dribble < 0 || dribble > 100) {
- throw new IllegalArgumentException("Dribble should be between 0 and 100.");
- }
- if (passing < 0 || passing > 100) {
- throw new IllegalArgumentException("Passing should be between 0 and 100.");
- }
- if (shooting < 0 || shooting > 100) {
- throw new IllegalArgumentException("Shooting should be between 0 and 100.");
- }
- double skill = (endurance + sprint + dribble + passing + shooting) / 5.0;
- this.players.put(name, skill);
- }
- public void removePlayer(String name) {
- if (!this.players.containsKey(name)) {
- throw new IllegalArgumentException(String.format("Player %s is not in %s team.", name, this.getName()));
- }
- this.players.remove(name);
- }
- public int getRating() {
- int rating = 0;
- double sum = 0.0;
- if (!this.players.isEmpty()) {
- for (Map.Entry<String, Double> player : this.players.entrySet()) {
- sum += player.getValue();
- }
- rating = (int)Math.round(sum/this.players.size());
- }
- return rating;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement