(하단에 문제)
Player(abstract class) - Team(generics class) - league(generics class) - main
player (sub class) (team 여러개) (league여러개)
player -generics로 간단하게
나머지 class 코딩은 기본예제와 동일 (Player, Team)
League class - generics extends Team.
package com.timbuchalka;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by dev on 18/10/2015.
*/
public class League<T extends Team> {
public String name;
private ArrayList<T> league = new ArrayList<>();
public League(String name) {
this.name = name;
}
public boolean add(T team) {
if(league.contains(team)) {
return false;
}
league.add(team);
return true;
}
public void showLeagueTable() {
Collections.sort(league); //collection.sort : arrayList값들 정렬
for(T t : league) { // 알파벳순, 혹은 숫자순으로 자동으로 저장.
System.out.println(t.getName() + ": " + t.ranking());
}
}
}
Main
package com.timbuchalka;
public class Main {
public static void main(String[] args) {
League<Team<FootballPlayer>> footballLeague = new League<>("AFL");
Team<FootballPlayer> adelaideCrows = new Team<>("Adelaide Crows");
Team<FootballPlayer> melbourne = new Team<>("Melbourne");
Team<FootballPlayer> hawthorn= new Team<>("Hawthorn");
Team<FootballPlayer> fremantle= new Team<>("Fremantle");
Team<BaseballPlayer> baseballTeam = new Team<>("Chicago Cubs");
hawthorn.matchResult(fremantle, 1, 0);
hawthorn.matchResult(adelaideCrows, 3, 8);
adelaideCrows.matchResult(fremantle, 2, 1);
footballLeague.add(adelaideCrows);
footballLeague.add(melbourne);
footballLeague.add(hawthorn);
footballLeague.add(fremantle);
// footballLeague.add(baseballTeam);
footballLeague.showLeagueTable();
// 아래처럼 League<Team<T>>, Team<T> 값처럼 generics 안쓰면 unchecked오류 안뜬다.코딩때
BaseballPlayer pat = new BaseballPlayer("Pat");
SoccerPlayer beckham = new SoccerPlayer("Beckham");
Team rawTeam = new Team("Raw Team");
rawTeam.addPlayer(beckham); // unchecked warning
rawTeam.addPlayer(pat); // unchecked warning
footballLeague.add(rawTeam); // unchecked warning
League<Team> rawLeague = new League<>("Raw");
rawLeague.add(adelaideCrows); // no warning
rawLeague.add(baseballTeam); // no warning
rawLeague.add(rawTeam); // no warning
League reallyRaw = new League("Really raw");
reallyRaw.add(adelaideCrows); // unchecked warning
reallyRaw.add(baseballTeam); // unchecked warning
reallyRaw.add(rawTeam); // unchecked warning
}
}
Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=51059:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Generics-Generics-Challenge-Source-code\out\production\GenericsChallenge com.timbuchalka.Main
Hawthorn beat Fremantle
Hawthorn lost to Adelaide Crows
Adelaide Crows beat Fremantle
Adelaide Crows: 4
Hawthorn: 2
Melbourne: 0
Fremantle: 0
Beckham picked for team Raw Team
Pat picked for team Raw Team
Process finished with exit code 0
나머지 class들 참고 /나머지 클래스들 기본예제와 동일.
Player class (abstract class)
package com.timbuchalka;
/**
* Created by dev on 17/10/2015.
*/
public abstract class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
sub class 2개 - player
package com.timbuchalka; //subClass 1
/**
* Created by dev on 17/10/2015.
*/
public class BaseballPlayer extends Player {
public BaseballPlayer(String name) {
super(name);
}
}
//subClass 2
package com.timbuchalka;
/**
* Created by dev on 17/10/2015.
*/
public class SoccerPlayer extends Player {
public SoccerPlayer(String name) {
super(name);
}
}
Team class (generics)
package com.timbuchalka;
import java.util.ArrayList;
/**
* Created by dev on 17/10/2015.
*/
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) {
if(this.ranking() > team.ranking()) {
return -1;
} else if(this.ranking() < team.ranking()) {
return 1;
} else {
return 0;
}
}
}
문제.
// ArrayList<Team> teams;
// Collections.sort(teams);
// 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.
'JavaCode(review)' 카테고리의 다른 글
nSum(int n), factorial(int n) 계승, fibonacci(int n) 수열 코딩 (0) | 2020.06.27 |
---|---|
자바 연산 (0) | 2020.06.27 |
Naming convention (0) | 2020.06.24 |
Operator (0) | 2020.06.23 |
FloatingPointNumbers/Double (0) | 2020.06.23 |