Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.FootballTeamGenerator;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Map<String, Team> teamLeague = new HashMap<>();
- String[] command = reader.readLine().split(";");
- while (!"END".equals(command[0])) {
- switch (command[0]) {
- case "Team" :
- Team team = null;
- try {
- team = tryAddTeam(command);
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- } catch (IndexOutOfBoundsException o) {
- System.out.println("A name should not be empty.");
- }
- if (team != null) {
- String teamName = command[1];
- teamLeague.put(teamName, team);
- }
- break;
- case "Add" :
- Player player = null;
- try {
- player = tryAddPlayer(command);
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- String inTeam = command[1];
- try {
- if (!teamLeague.containsKey(inTeam)) {
- throw new IllegalArgumentException("Team " + inTeam + " does not exist.");
- } else {
- if (player != null) {
- teamLeague.get(inTeam).addPlayer(player);
- }
- }
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- break;
- case "Remove" :
- String inATeam = command[1];
- String kickPlayer = command[2];
- boolean isInTeam = false;
- Player index = null;
- try {
- for (Team team1 : teamLeague.values()) {
- for (Player player1 : team1.getPlayers()) {
- if (player1.getName().equals(kickPlayer)) {
- isInTeam = true;
- index = player1;
- break;
- }
- }
- if (isInTeam) {
- team1.getPlayers().remove(index);
- break;
- }
- }
- if (!isInTeam) {
- throw new IllegalArgumentException("Player " + kickPlayer + " is not in " + inATeam + " team.");
- }
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- break;
- case "Rating" :
- String teamRate = command[1];
- try {
- if (!teamLeague.containsKey(teamRate)) {
- throw new IllegalArgumentException("Team " + teamRate + " does not exist.");
- } else {
- double rating = teamLeague.get(teamRate).getTeamRating();
- System.out.printf("%s - %.0f" + System.lineSeparator(), teamRate, rating);
- }
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- break;
- }
- command = reader.readLine().split(";");
- }
- }
- private static Team tryAddTeam(String[] command) {
- if (command[1] == null || command[1].trim().isEmpty()) {
- throw new IllegalArgumentException("A name should not be empty.");
- } else {
- Team team = new Team(command[1]);
- return team;
- }
- }
- private static Player tryAddPlayer(String[] command) {
- if (command[1] == null || command[1].trim().isEmpty()) {
- throw new IllegalArgumentException("A name should not be empty.");
- } else {
- String name = command[2];
- int endurance = Integer.parseInt(command[3]);
- if (endurance < 0 || endurance > 100) {
- throw new IllegalArgumentException("Endurance should be between 0 and 100.");
- }
- int spirit = Integer.parseInt(command[4]);
- if (spirit < 0 || spirit > 100) {
- throw new IllegalArgumentException("Spirit should be between 0 and 100.");
- }
- int dribble = Integer.parseInt(command[5]);
- if (dribble < 0 || dribble > 100) {
- throw new IllegalArgumentException("Dribble should be between 0 and 100.");
- }
- int passing = Integer.parseInt(command[6]);
- if (passing < 0 || passing > 100) {
- throw new IllegalArgumentException("Passing should be between 0 and 100.");
- }
- int shooting = Integer.parseInt(command[7]);
- if (shooting < 0 || shooting > 100) {
- throw new IllegalArgumentException("Shooting should be between 0 and 100.");
- }
- Player player = new Player(name, endurance, spirit, dribble, passing, shooting);
- return player;
- }
- }
- }
- public class Player {
- private String name;
- private double skill;
- private int endurance;
- private int spirit;
- private int dribble;
- private int passing;
- private int shooting;
- public Player(String name, int endurance, int spirit, int dribble, int passing, int shooting) {
- this.name = name;
- this.endurance = endurance;
- this.spirit = spirit;
- this.dribble = dribble;
- this.passing = passing;
- this.shooting = shooting;
- }
- public double getPlayerRating() {
- this.skill = (this.endurance + this.spirit + this.dribble + this.passing + this.shooting) / 5.0;
- return this.skill;
- }
- public String getName() {
- return name;
- }
- }
- public class Team {
- private String name;
- private double rating;
- private List<Player> players;
- public Team(String name) {
- this.name = name;
- this.rating = 0;
- this.players = new ArrayList<>();
- }
- public void addPlayer(Player player) {
- this.players.add(player);
- }
- public double getTeamRating() {
- for (Player player : players) {
- this.rating += player.getPlayerRating();
- }
- this.rating = Math.round(this.rating / players.size());
- if (players.size() == 0) {
- this.rating = 0;
- }
- return this.rating;
- }
- public List<Player> getPlayers() {
- return players;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement