Advertisement
NadezhdaGeorgieva

05 Football Team Generator

Mar 3rd, 2021
1,416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package FootballTeamGenerator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Team {
  7.     private String name;
  8.     private List<Player> players;
  9.  
  10.     public Team(String name) {
  11.         this.setName(name);
  12.         this.players = new ArrayList<>();
  13.     }
  14.  
  15.     private void setName(String name) {
  16.         if (name == null || name.trim().isEmpty()){
  17.             throw new IllegalArgumentException("A name should not be empty.");
  18.         }
  19.         this.name = name.trim();
  20.     }
  21.  
  22.     public String getName() {
  23.         return name;
  24.     }
  25.  
  26.     public void addPlayer(Player player){
  27.         if (!this.players.contains(player)) {
  28.             this.players.add(player);
  29.         }
  30.     }
  31.  
  32.     public void removePlayer(String playerName){
  33.         //this.players.removeIf(player -> player.getName().equals(playerName));
  34.         int index = -1;
  35.         for (int i = 0; i < this.players.size(); i++) {
  36.             if (this.players.get(i).getName().equals(playerName)){
  37.                 index = i;
  38.                 break;
  39.             }
  40.         }
  41.         if (index != -1){
  42.             this.players.remove(index);
  43.         } else {
  44.             throw new IllegalArgumentException(String.format("Player %s is not in %s team.", playerName, this.name));
  45.         }
  46.     }
  47.  
  48.     public double getRating(){
  49.         return this.players.stream()
  50.                 .mapToDouble(Player::overallSkillLevel)
  51.                 .average()
  52.                 .orElse(0.00);
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement