Advertisement
wpinda

"Class Team only needs object variables for "..." "

Jul 23rd, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. // Class Team.java starts here
  2. // "Class Team only needs object variables for name, players and maximum size, remove the rest"
  3.  
  4.  
  5. import java.util.ArrayList;
  6. public class Team {
  7.    
  8.     private String teamName;
  9.     private ArrayList<Player> playerList;
  10.     private int size; // amount of players in a team
  11.     private int maxSize; // max team size
  12.     private int totalGoals; // I can't have this :(
  13.    
  14.     public Team(String teamName) {
  15.         this.teamName = teamName;
  16.         this.playerList = new ArrayList<Player>();
  17.         this.size = 0;
  18.         this.maxSize = 16;
  19.        
  20.     }
  21.    
  22.     public int goals() { // how do I make this return the total amount of goals without the variable totalGoals
  23.         this.totalGoals = 0;
  24.         for (Player a : this.playerList) {
  25.             this.totalGoals = this.totalGoals + a.goals();
  26.         }
  27.         return this.totalGoals;
  28.     }
  29.    
  30.     // you can ignore the rest of this class
  31.    
  32.     public String getName() {
  33.         return this.teamName;
  34.     }
  35.    
  36.     public void addPlayer(Player player) {
  37.         if (this.size < this.maxSize) {    
  38.             this.playerList.add(player);
  39.             this.size++;
  40.         }
  41.     }
  42.    
  43.     public void printPlayers() {
  44.         for (Player a : this.playerList) {
  45.             System.out.println(a);
  46.         }
  47.        
  48.     }
  49.    
  50.     public void setMaxSize(int maxSize) {
  51.         this.maxSize = maxSize;
  52.     }
  53.    
  54.     public int size() {
  55.         return this.size;
  56.     }
  57.    
  58. }
  59.  
  60.  
  61. // Class Player.java starts here
  62.  
  63. public class Player {
  64.    
  65.     private String playerName;
  66.     private int goals;
  67.    
  68.     public Player (String playerName , int goals) {
  69.         this.playerName = playerName;
  70.         this.goals = goals;
  71.     }
  72.    
  73.     public Player (String playerName) {
  74.         this.playerName = playerName;
  75.     }
  76.    
  77.     public String getName() {
  78.         return this.playerName;
  79.     }
  80.    
  81.     public int goals() {
  82.         return this.goals;
  83.     }
  84.    
  85.     public String toString() {
  86.         return "Player: " + this.playerName + ", goals: " + this.goals;
  87.        
  88.     }
  89.      
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement