// Arup Guha
// 10/21/2026
// Team Class for example showing use of ArrayList

import java.util.*;

public class Team {

	// My 3 instance variables.
	private ArrayList<Player> all;
	private ArrayList<Player> offense;
	private ArrayList<Player> defense;
	
	// Everything starts empty.
	public Team() {
		all = new ArrayList<Player>();
		offense = new ArrayList<Player>();
		defense = new ArrayList<Player>();
	}
	
	// Add p to this team.
	public void addPlayer(Player p) {
		all.add(p);
		if (p.isOffense())
			offense.add(p);
		else
			defense.add(p);
	}
	
	// Returns the player with the name name or null if there's no such player
	// on this team.
	public Player getPlayer(String name) {
		
		// Just the old fashioned way.
		for (Player p: all)
			if (p.isPlayerName(name))
				return p;
			
		// If we get here, name isn't on the team.
		return null;
	}
	
	// Removes p from the team. Returns true if the player used to be on the team
	// false if they weren't and no action was taken.
	public boolean remove(Player p) {
		
		// Otherwise my code will crash!
		if (p == null) return false;
		
		// Get rid of this player from the team.
		boolean retval = all.remove(p);
		
		/*** The second one somtimes runes even when they aren't on the team but
		it's okay!
		***/
		
		// Add either office or defense.
		if (p.isOffense())
			offense.remove(p);
		else
			defense.remove(p);
		
		// We need to return this.
		return retval;
	}
	
	// Returns a string representation of the team.
	public String toString() {
		
		// List offense first.
		String res = "Offense:\n";
		
		/*** could do
		for (int i=0; i<offense.size(); i++)
			res = res + offense.get(i)+"\n";
		***/
		
		// Add each person.
		for (Player p: offense)
			res = res + p + "\n";
		
		// Then defense...
		res = res +"\nDefense:\n";
		for (Player p: defense)
			res = res + p + "\n";
		
		// Ta da!
		return res;
	}
}