Advertisement
Kostadin_

Football Team Generator

Jun 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. package com.company.FootballTeamGenerator;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11.  
  12. public class Main {
  13.     public static void main(String[] args) throws IOException {
  14.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  15.  
  16.         Map<String, Team> teamLeague = new HashMap<>();
  17.  
  18.         String[] command = reader.readLine().split(";");
  19.         while (!"END".equals(command[0])) {
  20.             switch (command[0]) {
  21.                 case "Team" :
  22.                     Team team = null;
  23.                     try {
  24.                         team = tryAddTeam(command);
  25.                     } catch (IllegalArgumentException ex) {
  26.                         System.out.println(ex.getMessage());
  27.                     } catch (IndexOutOfBoundsException o) {
  28.                         System.out.println("A name should not be empty.");
  29.                     }
  30.                     if (team != null) {
  31.                         String teamName = command[1];
  32.                         teamLeague.put(teamName, team);
  33.                     }
  34.                     break;
  35.                 case "Add" :
  36.                     Player player = null;
  37.                     try {
  38.                         player = tryAddPlayer(command);
  39.                     } catch (IllegalArgumentException ex) {
  40.                         System.out.println(ex.getMessage());
  41.                     }
  42.                     String inTeam = command[1];
  43.                     try {
  44.                         if (!teamLeague.containsKey(inTeam)) {
  45.                             throw new IllegalArgumentException("Team " + inTeam + " does not exist.");
  46.                         } else {
  47.                             if (player != null) {
  48.                                 teamLeague.get(inTeam).addPlayer(player);
  49.                             }
  50.                         }
  51.                     } catch (IllegalArgumentException ex) {
  52.                         System.out.println(ex.getMessage());
  53.                     }
  54.                     break;
  55.                 case "Remove" :
  56.                     String inATeam = command[1];
  57.                     String kickPlayer = command[2];
  58.                     boolean isInTeam = false;
  59.                     Player index = null;
  60.                     try {
  61.                         for (Team team1 : teamLeague.values()) {
  62.                             for (Player player1 : team1.getPlayers()) {
  63.                                 if (player1.getName().equals(kickPlayer)) {
  64.                                     isInTeam = true;
  65.                                     index = player1;
  66.                                     break;
  67.                                 }
  68.                             }
  69.                             if (isInTeam) {
  70.                                 team1.getPlayers().remove(index);
  71.                                 break;
  72.                             }
  73.                         }
  74.  
  75.                         if (!isInTeam) {
  76.                             throw new IllegalArgumentException("Player " + kickPlayer + " is not in " + inATeam + " team.");
  77.                         }
  78.                     } catch (IllegalArgumentException ex) {
  79.                         System.out.println(ex.getMessage());
  80.                     }
  81.                     break;
  82.                 case "Rating" :
  83.                     String teamRate = command[1];
  84.                     try {
  85.                         if (!teamLeague.containsKey(teamRate)) {
  86.                             throw new IllegalArgumentException("Team " + teamRate + " does not exist.");
  87.                         } else {
  88.                             double rating = teamLeague.get(teamRate).getTeamRating();
  89.                             System.out.printf("%s - %.0f" + System.lineSeparator(), teamRate, rating);
  90.                         }
  91.                     } catch (IllegalArgumentException ex) {
  92.                         System.out.println(ex.getMessage());
  93.                     }
  94.                     break;
  95.             }
  96.  
  97.             command = reader.readLine().split(";");
  98.         }
  99.     }
  100.  
  101.     private static Team tryAddTeam(String[] command) {
  102.         if (command[1] == null || command[1].trim().isEmpty()) {
  103.             throw new IllegalArgumentException("A name should not be empty.");
  104.         } else {
  105.             Team team = new Team(command[1]);
  106.             return team;
  107.         }
  108.     }
  109.  
  110.     private static Player tryAddPlayer(String[] command) {
  111.         if (command[1] == null || command[1].trim().isEmpty()) {
  112.             throw new IllegalArgumentException("A name should not be empty.");
  113.         } else {
  114.             String name = command[2];
  115.             int endurance   = Integer.parseInt(command[3]);
  116.             if (endurance < 0 || endurance > 100) {
  117.                 throw new IllegalArgumentException("Endurance should be between 0 and 100.");
  118.             }
  119.             int spirit      = Integer.parseInt(command[4]);
  120.             if (spirit < 0 || spirit > 100) {
  121.                 throw new IllegalArgumentException("Spirit should be between 0 and 100.");
  122.             }
  123.             int dribble     = Integer.parseInt(command[5]);
  124.             if (dribble < 0 || dribble > 100) {
  125.                 throw new IllegalArgumentException("Dribble should be between 0 and 100.");
  126.             }
  127.             int passing     = Integer.parseInt(command[6]);
  128.             if (passing < 0 || passing > 100) {
  129.                 throw new IllegalArgumentException("Passing should be between 0 and 100.");
  130.             }
  131.             int shooting    = Integer.parseInt(command[7]);
  132.             if (shooting < 0 || shooting > 100) {
  133.                 throw new IllegalArgumentException("Shooting should be between 0 and 100.");
  134.             }
  135.             Player player = new Player(name, endurance, spirit, dribble, passing, shooting);
  136.             return player;
  137.         }
  138.     }
  139. }
  140.  
  141. public class Player {
  142.     private String name;
  143.     private double skill;
  144.     private int endurance;
  145.     private int spirit;
  146.     private int dribble;
  147.     private int passing;
  148.     private int shooting;
  149.  
  150.     public Player(String name, int endurance, int spirit, int dribble, int passing, int shooting) {
  151.         this.name = name;
  152.         this.endurance = endurance;
  153.         this.spirit = spirit;
  154.         this.dribble = dribble;
  155.         this.passing = passing;
  156.         this.shooting = shooting;
  157.     }
  158.  
  159.     public double getPlayerRating() {
  160.         this.skill = (this.endurance + this.spirit + this.dribble + this.passing + this.shooting) / 5.0;
  161.         return this.skill;
  162.     }
  163.  
  164.     public String getName() {
  165.         return name;
  166.     }
  167. }
  168.  
  169. public class Team {
  170.     private String name;
  171.     private double rating;
  172.     private List<Player> players;
  173.  
  174.     public Team(String name) {
  175.         this.name = name;
  176.         this.rating = 0;
  177.         this.players = new ArrayList<>();
  178.     }
  179.  
  180.     public void addPlayer(Player player) {
  181.         this.players.add(player);
  182.     }
  183.  
  184.     public double getTeamRating() {
  185.         for (Player player : players) {
  186.             this.rating += player.getPlayerRating();
  187.         }
  188.         this.rating = Math.round(this.rating / players.size());
  189.         if (players.size() == 0) {
  190.             this.rating = 0;
  191.         }
  192.         return this.rating;
  193.     }
  194.  
  195.     public List<Player> getPlayers() {
  196.         return players;
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement