Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pr06FootballTeamGenerator;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- public class Main {
- public static Map<String, Team> teams;
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- System.in));
- teams = new HashMap<>();
- String[] input = reader.readLine().split(";");
- while (! input[0].equals("END")) {
- try {
- switch (input[0]) {
- case "Team":
- addTeam(input);
- break;
- case "Add":
- addPlayer(input);
- break;
- case "Remove":
- removePlayer(input);
- break;
- case "Rating":
- getRating(input);
- break;
- }
- } catch (IllegalArgumentException e) {
- System.out.println(e.getMessage());
- }
- input = reader.readLine().split(";");
- }
- }
- public static void doesExsist(String teamName) {
- if (!teams.containsKey(teamName)) {
- throw new IllegalArgumentException(String.format(
- "Team %s does not exist.", teamName));
- }
- }
- public static void getRating(String[] input) {
- String teamName = input[1];
- doesExsist(teamName);
- System.out.printf("%s - %d%n",teamName,teams.get(teamName).getRating());
- }
- public static void removePlayer(String[] input) {
- String teamName = input[1];
- String playerName = input[2];
- doesExsist(teamName);
- teams.get(teamName).removePlayer(playerName);
- }
- public static void addPlayer(String[] input) {
- String teamName = input[1];
- String playerName = input[2];
- double endurance = Double.valueOf(input[3]);
- double sprint = Double.valueOf(input[4]);
- double dribble = Double.valueOf(input[5]);
- double passing = Double.valueOf(input[6]);
- double shooting = Double.valueOf(input[7]);
- doesExsist(teamName);
- Player player = new Player(playerName, endurance, sprint, dribble,
- passing, shooting);
- teams.get(teamName).addPlayer(player);
- }
- public static void addTeam(String[] input) {
- String teamName = input[1];
- Team team = new Team(teamName);
- teams.put(teamName, team);
- }
- }
- public class Player {
- private String name;
- private Stat endurance;
- private Stat sprint;
- private Stat dribble;
- private Stat passing;
- private Stat shooting;
- public Player(String name, double endurance, double sprint, double dribble,
- double passing, double shooting) {
- this.setName(name);
- this.setEndurance(endurance);
- this.setSprint(sprint);
- this.setDribble(dribble);
- this.setPassing(passing);
- this.setShooting(shooting);
- }
- public String getName() {
- return name;
- }
- public double getAverageStats() {
- double result = (this.endurance.getStat()
- + this.sprint.getStat()
- + this.dribble.getStat()
- + this.passing.getStat()
- + this.shooting.getStat()) / 5.0;
- return result;
- }
- private void setShooting(double shooting) {
- Stat stat = new Stat("Shooting", shooting);
- this.shooting = stat;
- }
- private void setPassing(double passing) {
- Stat stat = new Stat("Passing", passing);
- this.passing = stat;
- }
- private void setDribble(double dribble) {
- Stat stat = new Stat("Dribble", dribble);
- this.dribble = stat;
- }
- private void setSprint(double sprint) {
- Stat stat = new Stat("Sprint", sprint);
- this.sprint = stat;
- }
- private void setEndurance(double endurance) {
- Stat stat = new Stat("Endurance", endurance);
- this.endurance = stat;
- }
- private void setName(String name) {
- if (name == null || name.trim().length() == 0) {
- System.out.println("A name should not be empty.");
- } else {
- this.name = name;
- }
- }
- }
- public class Stat {
- private String name;
- private double number;
- public Stat(String name, double number) {
- this.name = name;
- this.setNumber(number);
- }
- public double getStat(){
- return this.number;
- }
- private void setNumber(double number) {
- if (number < 0 || number > 100) {
- throw new IllegalArgumentException(String.format("%s should be between 0 and 100.",
- this.name));
- } else {
- this.number = number;
- }
- }
- @Override
- public String toString() {
- return this.name + " " + this.number;
- }
- }
- public class Team {
- private String name;
- private Map<String, Player> players;
- public Team(String name) {
- this.setName(name);
- this.players = new HashMap<>();
- }
- public void removePlayer(String playerName) {
- if (!players.containsKey(playerName)) {
- throw new IllegalArgumentException(String.format(
- "Player %s is not in %s team.",playerName, this.name));
- }
- players.remove(playerName);
- }
- public void addPlayer(Player player){
- players.put(player.getName(),player);
- }
- public int getRating(){
- double averageStat = 0;
- for (Player player : players.values()) {
- averageStat += player.getAverageStats();
- }
- return (int)Math.round(averageStat/players.size());
- }
- public String getName() {
- return this.name;
- }
- private void setName(String name) {
- if (name == null || name.trim().length() == 0) {
- throw new IllegalArgumentException("A name should not be empty.");
- }
- this.name = name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment