Team

import java.util.ArrayList;

public class Team<T> {

    private final ArrayList<T> aList = new ArrayList<>();

    // adds a member (an object of T) to aList
    public void addToTeam(T t) {
        aList.add(t);
    }

    // removes the last member who joined aList (i.e., remove the member at the end of aList)
    // returns the removed element or null if empty
    public T removeFromTeam() {
        if (aList.isEmpty()) {
            return null;
        }
        return aList.remove(aList.size() - 1);
    }

    // removes a member (an object of T) from aList (first occurrence)
    // returns true if removed
    public boolean removeFromTeam(T t) {
        return aList.remove(t);
    }

    // returns the number of members in aList
    public int getNumberOfMembers() {
        return aList.size();
    }

    // returns true if there is no member in aList, false otherwise
    public boolean hasNoMember() {
        return aList.isEmpty();
    }

    // returns a shallow copy as a regular ArrayList to use Collections.sort
    public ArrayList<T> toArrayListCopy() {
        return new ArrayList<>(aList);
    }

    // outputs all members, one per line
    @Override
    public String toString() {
        if (aList.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (T t : aList) {
            sb.append(t).append(System.lineSeparator());
        }
        return sb.toString();
    }
}

 

Scroll to Top