Reputation: 149
Say i let players create teams and creating a team calls a new instance of the team class which has an array list called members.
Now in the main class how would i add a player to a team after being invited? i have an addPlayer method in the team class that simply add them to the arraylist but what if there are currently multiple instances of the teams class(other players have created teams) how would it know which one to join?
I do have a variable in the Teams class for the teamLeader which gets set when creating the instance if that could help me edit a certain instance.
Team team = new Team(this, leader);
Any help is appreiciated
Upvotes: 1
Views: 168
Reputation: 7069
As per your design, You need to keep all teams in a list after creation.
ArrayList teamsList=new ArrayList ();
Team team = new Team(this, leader);
teamsList.add(team);
Then Loop through all teams in addPlayer method and then compare leader and then add a player to it. Something like this -
public void addPlayer (Player player,String leader){
for(int i=0; i<teamListSize;i++)
Team tempTeam=teamsList.get(i);
if(tempTeam.getLeader().equalsIgnoreCase(leader)){
tempTeam.add(player);
break;
}
}
Upvotes: 0
Reputation: 68715
You need an identifier to uniquely distinguish each team and you can use that identifier to store the teams in a Map
. Something like this:
Map<String,Team> teamMap = new HashMap<String,Team>();
Chose the key type as per your requirement, I chose String for an example
Upvotes: 1