I have three classes called League
, Team
and Player
, League has an array with teams, and I have a method called showLeagueTable
that shows team sorted by ranking
(which is a method that exists in the Team
class) and the class Team has an array of players.
League.java
public class League<T extends Team> {
public String name;
private List<T> teams = new ArrayList<>();
public League(String name) {
this.name = name;
}
public boolean add(T team) {
if (teams.contains(team)) {
return false;
}
return teams.add(team);
}
public void showLeagueTable() {
Collections.sort(this.teams);
for (T t: teams) {
System.out.println(t.getName() + ": " + t.ranking());
}
}
}
Team.java
public class Team<T extends Player> implements Comparable<Team<T>> {
private String name;
int played = 0;
int won = 0;
int lost = 0;
int tied = 0;
private ArrayList<T> members = new ArrayList<>();
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean addPlayer(T player) {
if (members.contains(player)) {
System.out.println(player.getName() + " is already on this team");
return false;
} else {
members.add(player);
System.out.println(player.getName() + " picked for team " + this.name);
return true;
}
}
public int numPlayers() {
return this.members.size();
}
public void matchResult(Team<T> opponent, int ourScore, int theirScore) {
String message;
if (ourScore > theirScore) {
won++;
message = " beat ";
} else if (ourScore == theirScore) {
tied++;
message = " drew with ";
} else {
lost++;
message = " lost to ";
}
played++;
if (opponent != null) {
System.out.println(this.getName() + message + opponent.getName());
opponent.matchResult(null, theirScore, ourScore);
}
}
public int ranking() {
return (won * 2) + tied;
}
@Override
public int compareTo(Team<T> team) {
return Integer.compare(team.ranking(), this.ranking());
}
}
Player.java
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
but in the line Collections.sort(this.teams);
from the class League
, I got the warning Unchecked method 'sort(List<T>)' invocation
, I have read that maybe the problems is that I haven't implemented the Comparable interface in my class Team with a Type, but I did it you can see it at the line:
public class Team<T extends Player> implements Comparable<Team<T>>
can someone help me, please, thanks!