Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created May 8, 2020 09:27
Show Gist options
  • Save SiAust/8dfb8b6326dcc8d5380df4acaa115997 to your computer and use it in GitHub Desktop.
Save SiAust/8dfb8b6326dcc8d5380df4acaa115997 to your computer and use it in GitHub Desktop.
Using generics to restrict object initialisation and commparable interface to sort.
import java.util.ArrayList;
import java.util.Collections;
public class Udemy {
public static void main(String[] args) {
League<FootballTeam> premierLeague = new League<>("Premier League");
FootballTeam tottenham = new FootballTeam("Tottenham Hotspur",38,0, 0);
FootballTeam arsenal = new FootballTeam("Arsenal", 0,0, 38);
FootballTeam bournemouth = new FootballTeam("AFC Bournemouth", 23, 7, 8);
premierLeague.addTeam(tottenham);
premierLeague.addTeam(arsenal);
premierLeague.addTeam(bournemouth);
premierLeague.printLeagueTable();
/*
Premier League
Tottenham Hotspur W:38 D:0 L:0 Points: 114
AFC Bournemouth W:23 D:7 L:8 Points: 76
Arsenal W:0 D:0 L:38 Points: 0
*/
System.out.println(premierLeague.toString());
/*
Tottenham Hotspur
AFC Bournemouth
Arsenal
*/
League<CricketTeam> indianPremierLeague = new League<>("Indian Premier League");
CricketTeam chennai = new CricketTeam("Chennai Super Kings", 10, 0, 0);
CricketTeam delhi = new CricketTeam("Delhi Capitals", 9, 1, 0);
CricketTeam kings = new CricketTeam("Kings XI Punjab", 7, 2, 1);
indianPremierLeague.addTeam(chennai);
indianPremierLeague.addTeam(delhi);
indianPremierLeague.addTeam(kings);
/* Compile time error, type is not a CricketTeam. Prevents teams being added to the wrong league
using generic classes and type bounds */
// indianPremierLeague.addTeam(bournemouth);
indianPremierLeague.printLeagueTable();
/*
Indian Premier League
Chennai Super Kings W:10 D:0 L:0 Points: 30
Delhi Capitals W:9 D:0 L:1 Points: 27
Kings XI Punjab W:7 D:1 L:2 Points: 22
*/
}
}
/* Generic class that accepts types of SportsTeam and subclasses of SportsTeam. Upper type bounded */
class League<T extends SportsTeam> {
private String leagueName;
/* ArrayList only accepts class type T. These will be objects of SportsTeam and subclasses */
public ArrayList<T> teams = new ArrayList<>();
public League(String leagueName) {
this.leagueName = leagueName;
}
public void addTeam(T team) {
teams.add(team);
}
public void printLeagueTable() {
Collections.sort(teams);
System.out.println(leagueName);
for (T team : teams) {
System.out.println(team.toString());
}
}
@Override
public String toString() {
StringBuilder teamName = new StringBuilder();
for (T team : teams) {
teamName.append(team.getName()).append("\n");
}
return teamName.toString();
}
}
class SportsTeam implements Comparable<SportsTeam> {
private String name;
private int wins;
private int draws;
private int losses;
@Override
public String toString() {
return name + " W:" + wins + " D:" + draws + " L:" + losses + " Points: " + getRanking();
}
public SportsTeam(String name, int wins, int draws, int losses) {
this.name = name;
this.wins = wins;
this.draws = draws;
this.losses = losses;
}
public int getRanking() {
return wins * 3 + draws;
}
public String getName() {
return name;
}
/* This method will be used with Collections.sort() to order the collection of SportsTeam objects */
@Override
public int compareTo(SportsTeam otherTeam) {
if (this.getRanking() > otherTeam.getRanking()) {
return -1;
} else if (this.getRanking() == otherTeam.getRanking()) {
return 0;
} else {
return 1;
}
}
}
class FootballTeam extends SportsTeam {
public FootballTeam(String name, int wins, int draws, int losses) {
super(name, wins, draws, losses);
}
}
class RugbyTeam extends SportsTeam {
public RugbyTeam(String name, int wins, int draws, int losses) {
super(name, wins, losses, draws);
}
}
class CricketTeam extends SportsTeam {
public CricketTeam(String name, int wins, int draws, int losses) {
super(name, wins, losses, draws);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment