mellowdeep

football team generator

Jul 2nd, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 KB | None | 0 0
  1. package pr06FootballTeamGenerator;
  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.  
  9. public class Main {
  10.  
  11.     public static Map<String, Team> teams;
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.  
  15.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  16.                 System.in));
  17.         teams = new HashMap<>();
  18.         String[] input = reader.readLine().split(";");
  19.         while (! input[0].equals("END")) {
  20.             try {
  21.                 switch (input[0]) {
  22.                 case "Team":
  23.                     addTeam(input);
  24.                     break;
  25.                 case "Add":
  26.                     addPlayer(input);
  27.                     break;
  28.                 case "Remove":
  29.                     removePlayer(input);
  30.                     break;
  31.                 case "Rating":
  32.                     getRating(input);
  33.                     break;
  34.                 }
  35.  
  36.             } catch (IllegalArgumentException e) {
  37.                 System.out.println(e.getMessage());
  38.             }
  39.             input = reader.readLine().split(";");
  40.         }
  41.     }
  42.     public static void doesExsist(String teamName) {
  43.         if (!teams.containsKey(teamName)) {
  44.             throw new IllegalArgumentException(String.format(
  45.                     "Team %s does not exist.", teamName));
  46.         }
  47.     }
  48.  
  49.     public static void getRating(String[] input) {
  50.         String teamName = input[1];
  51.         doesExsist(teamName);
  52.         System.out.printf("%s - %d%n",teamName,teams.get(teamName).getRating());
  53.     }
  54.  
  55.    
  56.     public static void removePlayer(String[] input) {
  57.         String teamName = input[1];
  58.         String playerName = input[2];
  59.         doesExsist(teamName);
  60.         teams.get(teamName).removePlayer(playerName);
  61.     }
  62.  
  63.     public static void addPlayer(String[] input) {
  64.         String teamName = input[1];
  65.         String playerName = input[2];
  66.         double endurance = Double.valueOf(input[3]);
  67.         double sprint = Double.valueOf(input[4]);
  68.         double dribble = Double.valueOf(input[5]);
  69.         double passing = Double.valueOf(input[6]);
  70.         double shooting = Double.valueOf(input[7]);
  71.         doesExsist(teamName);
  72.         Player player = new Player(playerName, endurance, sprint, dribble,
  73.                 passing, shooting);
  74.         teams.get(teamName).addPlayer(player);
  75.     }
  76.  
  77.     public static void addTeam(String[] input) {
  78.         String teamName = input[1];
  79.         Team team = new Team(teamName);
  80.         teams.put(teamName, team);
  81.     }
  82.  
  83. }
  84.  
  85. public class Player {
  86.  
  87.     private String name;
  88.     private Stat endurance;
  89.     private Stat sprint;
  90.     private Stat dribble;
  91.     private Stat passing;
  92.     private Stat shooting;
  93.  
  94.     public Player(String name, double endurance, double sprint, double dribble,
  95.             double passing, double shooting) {
  96.         this.setName(name);
  97.         this.setEndurance(endurance);
  98.         this.setSprint(sprint);
  99.         this.setDribble(dribble);
  100.         this.setPassing(passing);
  101.         this.setShooting(shooting);
  102.     }
  103.  
  104.     public String getName() {
  105.         return name;
  106.     }
  107.  
  108.     public double getAverageStats() {
  109.         double result = (this.endurance.getStat()
  110.                 + this.sprint.getStat()
  111.                 + this.dribble.getStat()
  112.                 + this.passing.getStat()
  113.                 + this.shooting.getStat()) / 5.0;
  114.         return result;
  115.     }
  116.     private void setShooting(double shooting) {
  117.         Stat stat = new Stat("Shooting", shooting);
  118.         this.shooting = stat;
  119.     }
  120.  
  121.     private void setPassing(double passing) {
  122.         Stat stat = new Stat("Passing", passing);
  123.         this.passing = stat;
  124.     }
  125.  
  126.     private void setDribble(double dribble) {
  127.         Stat stat = new Stat("Dribble", dribble);
  128.         this.dribble = stat;
  129.     }
  130.  
  131.     private void setSprint(double sprint) {
  132.         Stat stat = new Stat("Sprint", sprint);
  133.         this.sprint = stat;
  134.     }
  135.  
  136.     private void setEndurance(double endurance) {
  137.         Stat stat = new Stat("Endurance", endurance);
  138.         this.endurance = stat;
  139.     }
  140.  
  141.     private void setName(String name) {
  142.         if (name == null || name.trim().length() == 0) {
  143.             System.out.println("A name should not be empty.");
  144.         } else {
  145.             this.name = name;
  146.         }
  147.     }
  148.  
  149. }
  150. public class Stat {
  151.  
  152.     private String name;
  153.     private double number;
  154.  
  155.     public Stat(String name, double number) {
  156.         this.name = name;
  157.         this.setNumber(number);
  158.     }
  159.  
  160.     public double getStat(){
  161.         return this.number;
  162.     }
  163.    
  164.     private void setNumber(double number) {
  165.         if (number < 0 || number > 100) {
  166.             throw new IllegalArgumentException(String.format("%s should be between 0 and 100.",
  167.                     this.name));
  168.         } else {
  169.             this.number = number;
  170.         }
  171.     }
  172.  
  173.     @Override
  174.     public String toString() {
  175.         return this.name + " " + this.number;
  176.     }
  177.    
  178. }
  179.  
  180. public class Team {
  181.  
  182.     private String name;
  183.     private Map<String, Player> players;
  184.  
  185.     public Team(String name) {
  186.         this.setName(name);
  187.         this.players = new HashMap<>();
  188.     }
  189.  
  190.     public void removePlayer(String playerName) {
  191.         if (!players.containsKey(playerName)) {
  192.             throw new IllegalArgumentException(String.format(
  193.                     "Player %s is not in %s team.",playerName, this.name));
  194.         }
  195.         players.remove(playerName);
  196.     }
  197.    
  198.     public void addPlayer(Player player){
  199.         players.put(player.getName(),player);
  200.     }
  201.    
  202.     public int getRating(){
  203.         double averageStat = 0;
  204.         for (Player player : players.values()) {
  205.             averageStat += player.getAverageStats();
  206.         }
  207.         return (int)Math.round(averageStat/players.size());
  208.     }
  209.  
  210.     public String getName() {
  211.         return this.name;
  212.     }
  213.  
  214.     private void setName(String name) {
  215.         if (name == null || name.trim().length() == 0) {
  216.             throw new IllegalArgumentException("A name should not be empty.");
  217.         }
  218.         this.name = name;
  219.     }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment