Мы хотим разработать простой турнир, состоящий из команд с именем и гражданством.В этом турнире организован набор матчей между приглашенными командами, и каждый матч противостоит двум командам.Команда с наибольшим количеством очков побеждает в матче.Если результат матча ничейный, каждая команда получает 1 очко, победившая команда получает 2 очка, а проигравший не получает ни одного очка.Мы хотели бы получить общее количество очков команды в турнире, чтобы узнать победителя.Победителем становится тот, кто набрал наибольшее количество баллов.
Итак, нам удалось создать три класса: Team, Match и Tournament и основной класс.
В основном классе у нас есть
public class ProgramTournaments {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Defining each team
Team frTeam, inTeam, cnTeam;
//Creation of three objects (Teams)
frTeam = new Team("French Blue Team", "French"); // New Means I want to create an Object (frTeams)
inTeam = new Team("Indian Blue Team", "India");
cnTeam = new Team("Chinese Red Team", "China");
//Create a new Tournament
Tournament tournament = new Tournament();
//Invite teams to the tourname
tournament.inviteTeam(frTeam);
tournament.inviteTeam(inTeam);
tournament.inviteTeam(cnTeam);
//Add matches to Tournament
Match m1 = new Match(frTeam, inTeam, true);
Match m2 = new Match(frTeam, cnTeam, true);
Match m3 = new Match(inTeam, cnTeam, true);
tournament.addMatch(m1);
tournament.addMatch(m2);
tournament.addMatch(m3);
//Check If all matches Have been Pleayed
tournament.allMatchPlayed();
}
}
В командном классе мы сделали это
public class Team {
//Defining the attributes
private String name; //Private means it is limited only to this Class (team)
private String citizenship;
public String getName() {
return name;
}
public String getCitizenship() {
return citizenship;
}
// Constructor inorder to initialized values
public Team (String name, String citizenship){
this.name = name; //Initializing name of team
this.citizenship = citizenship; //Initializing name of Citizenship of team
}
//Printing to strings
@Override
public String toString() {
return "Team{" + "name=" + name + ", citizenship=" + citizenship + '}';
}
}
В классе Match мы сделали это
public class Match {
private Team team1, team2;
private int scoreTeam1;
private int scoreTeam2;
private int pointTeam1, pointTeam2;
boolean play;
//Constructor
public Match(Team team1, Team team2, boolean play) {
this.team1 = team1;
this.team2 = team2;
this.scoreTeam1 = generateRandomScore();
this.scoreTeam2 = generateRandomScore();
this.play = play;
}
//All Methods
public int getScoreTeam1() {
return scoreTeam1;
}
public void setScoreTeam1(int scoreTeam1) {
this.scoreTeam1 = scoreTeam1;
}
public int getScoreTeam2() {
return scoreTeam2;
}
public void setScoreTeam2(int scoreTeam2) {
this.scoreTeam2 = scoreTeam2;
}
public Team getTeam1() {
return team1;
}
public void setTeam1(Team team1) {
this.team1 = team1;
}
public Team getTeam2() {
return team2;
}
public void setTeam2(Team team2) {
this.team2 = team2;
}
public boolean isPlay() {
return play;
}
public void setPlay(boolean play) {
this.play = play;
}
//Generate Random Score
private int generateRandomScore() {
Random random = new Random();
return random.nextInt(5);
}
public boolean draw() {
if (scoreTeam1 == scoreTeam2) {
pointTeam1 = 1;
pointTeam2 = 1;
return true;
}
return false;
}
public Team matchWinner() {
if (scoreTeam1 > scoreTeam2) {
pointTeam1 = 2;
pointTeam2 = 0;
return team1;
} else {
pointTeam2 = 2;
pointTeam1 = 0;
return team2;
}
}
}
В классе турнира мы сделали это
public class Tournament {
private List<Team> ListOfTeams = new ArrayList<>();
private List<Match> ListOfMatches = new ArrayList<>();
//Methods
public void inviteTeam(Team team) { //Inviting Teams
ListOfTeams.add(team);
}
public void addMatch(Match m) {
ListOfMatches.add(m);
}
public boolean allMatchPlayed() {
for (Match match : ListOfMatches) {
if (match.isPlay() == false) {
return false;
}
}
return true;
}
public void tournamentWinner(){
for (Match match : ListOfMatches){
match.decideResult();
}
Comparator <Team> team = new Comparator<Team>(){
@override
public int compare(Team t1, Team t2){
return t1.getScore() - t2.getScore();
}
};
Collections.sort(ListOfTeams, t);
System.out.println("The winner of the tournament is: " + ListOfTeams);
}
}
Поэтому, пожалуйста, мы застряли в попытках набрать общее количество очков для каждой команды и получить победителя на основе общего количества очков