Java 學習記錄54 — 代碼挑戰16

Create a generic class to implement a league table for a sport.

The class should allow teams to be added to the list, and store

a list of teams that belong to the league.

Your class should have a method to print out the teams in order,

with the team at the top of the league printed first.

Only teams of the same type should be added to any particular

instance of the league class — the program should fail to compile

if an attempt is made to add an incompatible team.

解答參考:

Player.java

public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

FootballPlayer.java

public class FootballPlayer extends Player {
public FootballPlayer(String name) {
super(name);
}
}

BaseballPlayer.java

public class BaseballPlayer extends Player {
public BaseballPlayer(String name) {
super(name);
}
}

Team.java

import java.util.ArrayList;public class Team<T extends Player> implements Comparable<Team<T>> {
int played = 0;
int won = 0;
int lost = 0;
int tied = 0;
private String name;
private ArrayList<T> members = new ArrayList<>();
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean addPlayer(T player) {
if (this.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 to ";
} else if (ourScore == theirScore) {
tied++;
message = " draw with ";
} else {
lost++;
message = " lost to ";
}
played++;
if (opponent != null) {
System.out.println(getName() + message + opponent.getName());
opponent.matchResult(null, theirScore, ourScore);
}
}
public int ranking() {
return (won * 2) + tied;
}
@Override
public int compareTo(Team<T> team) {
if (this.ranking() > team.ranking()) {
return -1;
} else if (this.ranking() < team.ranking()) {
return 1;
} else {
return 0;
}
}
}

League.java

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
public class League<T extends Team> {
ArrayList<T> teams = new ArrayList<>();
private String name;
public League(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean addTeam(T team) {
if (this.teams.contains(team)) {
System.out.println(team.getName() + " already in the league.");
return false;
} else {
teams.add(team);
System.out.println(team.getName() + " be add to " + this.getName());
return true;
}
}
public void teamOnLeagueByOrder() {
Collections.sort(teams);
for (int i = 0; i < teams.size(); i++) {
System.out.println("No." + (i+1) + " is team " + teams.get(i).getName() + " ranking : " + teams.get(i).ranking());
}
}
}

MainPlayer.java

public class MainPlayer {
public static void main(String[] args) {
League<Team<FootballPlayer>> footballLeague = new League<>("football league");
Team<FootballPlayer> football_Team_1 = new Team<FootballPlayer>("football-1");
Team<FootballPlayer> football_Team_2 = new Team<FootballPlayer>("football-2");
Team<FootballPlayer> football_Team_3 = new Team<FootballPlayer>("football-3");
footballLeague.addTeam(football_Team_1);
footballLeague.addTeam(football_Team_2);
footballLeague.addTeam(football_Team_3);
football_Team_1.matchResult(football_Team_2, 1, 0);
football_Team_1.matchResult(football_Team_2, 2, 0);
football_Team_1.matchResult(football_Team_2, 3, 0);
football_Team_1.matchResult(football_Team_3, 1, 0);
football_Team_1.matchResult(football_Team_3, 2, 0);
football_Team_1.matchResult(football_Team_2, 3, 0);
football_Team_2.matchResult(football_Team_2, 3, 0);
football_Team_2.matchResult(football_Team_2, 4, 0);
football_Team_2.matchResult(football_Team_2, 5, 0);
footballLeague.teamOnLeagueByOrder();


}
}

記錄成為軟體工程師的過程