I don't have the right solution for your problem but I can think for another way if it's good for you. check it out :
For Player class :
private String name;
private int age;
private int height;
public Player(String name, int Age , int height){
this.name=name;
this.age= Age;
this.height = height;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public int getHeight(){
return this.height;
}
@Override
public String toString(){
return this.name +" , "+ this.age +" , "+ this.height;
}
For Manager class :
private String name;
public Manager(String name){
this.name = name;
}
public String getManager(){
return this.name;
}
For Manager class :
Manager theManager;
Player[] thePlayer;
String name;
public Team(Manager theManager, Player[] thePlayer , String name){
this.theManager = theManager;
this.thePlayer = thePlayer;
this.name = name;
}
@Override
public String toString(){
String list = null ;
for (int i = 0;i<thePlayer.length;i++){
list += thePlayer[i].toString()+"\n";
}
return "Team name : " + this.name + "\n Team manager : " + this.theManager.getManager() + "\n Team players : \n" + list;
}
For team class :
Manager theManager;
Player[] thePlayer;
String name;
public Team(Manager theManager, Player[] thePlayer , String name){
this.theManager = theManager;
this.thePlayer = thePlayer;
this.name = name;
}
@Override
public String toString(){
String list = null ;
for (int i = 0;i<thePlayer.length;i++){
list += thePlayer[i].toString()+"\n";
}
return "Team name : " + this.name + "\n Team manager : " + this.theManager.getManager() + "\n Team players : \n" + list;
}
and finally the test class , so if you noticed you don't really need to put that loop inside the team constructor for player :
For test class(main) :
Manager A = new Manager("Flic");
Player P1 = new Player("Robert Levandowski",32,182);
Player P2 = new Player("David Alaba",32,179);
Player P3 = new Player("Joshoa Kimmich",23,175);
Player[] Aplayers = {P1,P2,P3};
Team B = new Team(A , Aplayers ,"FcBayern");
System.out.println(B.toString());
}
And I'm a barcelona fan by the way hahaha still want the revenge for that 8-2 have a great day !