NadezhdaGeorgieva

05 Football Team Generator

Mar 3rd, 2021
914
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×