1

We want to design a simple tournament that consist of teams with name and citizenship. In this tournament, a set of matches is organized between invited teams and each match opposes two teams. The team with the highest score wins the match. If the result of the match is draw each team gets 1 point, the winning team gets 2 points and no point for the loser. We would like to get the total of points of a team in a tournament to know the winner. The winner is the one with the highest points.

So we managed to create three classes: Team, Match and Tournament and the main class.

In the main class we have this

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();
}
  }

In the team class we did this

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 + '}';
} 
  }

In the Match class we did this

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;
    }
}
  }

In the Tournament Class we did this

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);
 }


   }

So please, we are stuck at trying to implement the total points for each teams and to get the winner based on the total points

Zoe
  • 27,060
  • 21
  • 118
  • 148
JP Douglas
  • 45
  • 1
  • 12
  • Why not move the points member variable from `Match` to `Team`? It makes more sense there, as each team will have some points. – Nicholas K Sep 27 '18 at 16:38
  • What we did is that , In the Match class, for one match, a team can have either 0, 1 or 2 points. What we are now trying to do is , with the matches we have created , increment for each team and for each match the number of points, but we don't know where to store the incremented variable that counts the total points for each team – JP Douglas Sep 27 '18 at 16:44

1 Answers1

1

I would advice to move points member variable from Match to Team. The reason being that each team will have some points at any point of time, so it makes sense that each team has a points field.

Now you would make the following changes to the methods

Team.java

public class Team {
   private int points;
   // getters and setters for points

   /* Rest of your class */
}

Match.java

We should combine your draw() and matchWinner() to one method say decideResult(), as own their own they make no sense.

public void decideResult() {
    if (scoreTeam1 == scoreTeam2) {
        team1.setPoints(team1.getPoints() + 1);
        team2.setPoints(team2.getPoints() + 1);
    } else if (scoreTeam1 > scoreTeam2) {
        team1.setPoints(team1.getPoints() + 2);
    } else {
        team2.setPoints(team2.getPoints() + 2);
    }
}

To find the winner you can just fetch the score from the respective Team object. For eg : frTeam.getPoints() and compare this with another countries .getPoints()

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • Thank you very much its very helpful. We added matchWinner and it seems ok. For now in the main class, we just created three teams to try our program, but later we will have many teams , so it will difficult to compared the points of each teams one by one. Do you have any idea? – JP Douglas Sep 27 '18 at 17:04
  • 1
    If you have many teams you can add all teams to a collection and sort it by the number of points. This will eliminate your manual effort. – Nicholas K Sep 27 '18 at 17:08
  • About the decideResult(), we prefer to keep both draw() and matchWinner() , as later we would like to add an overtime in case of a draw match etc – JP Douglas Sep 27 '18 at 17:10
  • Hmm, why not call the overtime method if it's a draw directly from `decideResult()` :) – Nicholas K Sep 27 '18 at 17:11
  • We tried comparing as you suggested us to do in order to get the winner, but we have a problem doing that. we tried to compare in the Tournament class but we got confused? can you please help us out? thanks – JP Douglas Sep 27 '18 at 17:23
  • Hmm, that's off-topic but *one* question wouldn't hurt, please go ahead. – Nicholas K Sep 27 '18 at 17:25
  • I mean, how can we compute and print the winner of tournament (the one with most points). that's where we are stuck at. But the decideResult() was so so helpful Thanks :))) – JP Douglas Sep 27 '18 at 17:31
  • [I'll give you a hint rather than just giving the answer away](https://stackoverflow.com/questions/6957631/sort-java-collection) – Nicholas K Sep 27 '18 at 17:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180900/discussion-between-jp-douglas-and-nicholas-k). – JP Douglas Sep 27 '18 at 18:12
  • We added a method in the tournament class called tournamentWinner() and we tried to do what you just said. But we would like you to check what we did because we would like to only print the first winner. thanks :)) – JP Douglas Sep 27 '18 at 18:18