this is the code
import java.util.ArrayList;
import java.util.List;
public class ScheduleGenerator {
public static void main(String[] args) {
List<Team> teams = new ArrayList<>();
int numTeams = 9;
for (int i = 1; i <= numTeams; i++) {
teams.add(new Team("Team " + i));
}
String[] pistes = {"Piste A", "Piste B", "Piste C", "Piste D", "Piste E", "Piste F"};
List<Round> rounds = generateSchedule(teams);
for (Round round : rounds) {
System.out.println("Round " + round.getNumber());
List<Match> matches = round.getMatches();
for (int i = 0; i < matches.size(); i++) {
Match match = matches.get(i);
String piste = pistes[i];
System.out.println(piste + " " + match.getTeam1().getName() + " vs " + match.getTeam2().getName());
}
System.out.println();
}
}
public static List<Round> generateSchedule(List<Team> teams) {
int numTeams = Math.round(teams.size());
int numRounds = numTeams;
int halfSize = numTeams / 2;
List<Team> list = new ArrayList<>(teams);
int listSize = list.size();
List<Round> rounds = new ArrayList<>();
if (numTeams % 2 != 0) {
teams.add(new Team("Rest"));
numTeams++;
}
for (int round = 0; round < numRounds; round++) {
Round currentRound = new Round(round);
List<Match> matches = new ArrayList<>();
int teamIdx = round % listSize;
Team team1 = list.get(teamIdx);
Team team2 = teams.get(numTeams - 1);
matches.add(new Match(team1, team2));
for (int i = 1; i < halfSize; i++) {
int firstTeam = (round + i) % listSize;
int secondTeam = (round + listSize - i) % listSize;
matches.add(new Match(list.get(firstTeam), list.get(secondTeam)));
}
currentRound.setMatches(matches);
rounds.add(currentRound);
}
return rounds;
}
}
class Team {
private String name;
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return name;
}
}
class Round {
private int number;
private List<Match> matches;
public Round(int number) {
this.number = number;
this.matches = new ArrayList<>();
}
public int getNumber() {
return number;
}
public void addMatch(Match match) {
matches.add(match);
}
public void setMatches(List<Match> matches) {
this.matches = matches;
}
public List<Match> getMatches() {
return matches;
}
public boolean hasMatch(Team team1, Team team2) {
for (Match match : matches) {
if ((match.getTeam1() == team1 && match.getTeam2() == team2) ||
(match.getTeam1() == team2 && match.getTeam2() == team1)) {
return true;
}
}
return false;
}
}
class Match {
private Team team1;
private Team team2;
public Match(Team team1, Team team2) {
this.team1 = team1;
this.team2 = team2;
}
public Team getTeam1() {
return team1;
}
public Team getTeam2() {
return team2;
}
}
The output needs to be like this
Round 0
Piste A Team 1 vs Team 9
Piste B Team 2 vs Team 8
Piste C Team 3 vs Team 7
Piste D Team 4 vs Team 6
Piste E Rest vs Team 5
Round 1
Piste A Team 1 vs Team 9
Piste B Team 2 vs Team 8
Piste C Team 3 vs Team 7
Piste D Team 4 vs Team 6
Piste E Rest vs Team 5
Round 2
Piste A Team 9 vs Team 8
Piste B Team 1 vs Team 7
Piste C Team 2 vs Team 6
Piste D Team 3 vs Team 5
Piste E Rest vs Team 4
Round 3
Piste A Team 8 vs Team 7
Piste B Team 9 vs Team 6
Piste C Team 1 vs Team 5
Piste D Team 2 vs Team 4
Piste E Rest vs Team 3
Round 4
Piste A Team 7 vs Team 6
Piste B Team 8 vs Team 5
Piste C Team 9 vs Team 4
Piste D Team 1 vs Team 3
Piste E Rest vs Team 2
Round 5
Piste A Team 6 vs Team 5
Piste B Team 7 vs Team 4
Piste C Team 8 vs Team 3
Piste D Team 9 vs Team 2
Piste E Rest vs Team 1
Round 6
Piste A Team 5 vs Team 4
Piste B Team 6 vs Team 3
Piste C Team 7 vs Team 2
Piste D Team 8 vs Team 1
Piste E Rest vs Team 9
Round 7
Piste A Team 4 vs Team 3
Piste B Team 5 vs Team 2
Piste C Team 6 vs Team 1
Piste D Team 7 vs Team 9
Piste E Rest vs Team 8
Round 8
Piste A Team 3 vs Team 2
Piste B Team 4 vs Team 1
Piste C Team 5 vs Team 9
Piste D Team 6 vs Team 8
Piste E Rest vs Team 7
Round 9
Piste A Team 2 vs Team 1
Piste B Team 3 vs Team 9
Piste C Team 4 vs Team 8
Piste D Team 5 vs Team 7
Piste E Rest vs Team 6
but as you can see in the first bit of the code here
for (Round round : rounds) {
System.out.println("Round " + round.getNumber());
List<Match> matches = round.getMatches();
for (int i = 0; i < matches.size(); i++) {
Match match = matches.get(i);
String piste = pistes[i];
System.out.println(piste + " " + match.getTeam1().getName() + " vs " + match.getTeam2().getName());
}
System.out.println();
}
this loop makes it that the maximum index is 4 that means that every time I run this code it will be missing Piste E and the output will be something like this
Round 0
Piste A Team 1 vs Rest
Piste B Team 2 vs Team 9
Piste C Team 3 vs Team 8
Piste D Team 4 vs Team 7
Round 1
Piste A Team 2 vs Rest
Piste B Team 3 vs Team 1
Piste C Team 4 vs Team 9
Piste D Team 5 vs Team 8
Round 2
Piste A Team 3 vs Rest
Piste B Team 4 vs Team 2
Piste C Team 5 vs Team 1
Piste D Team 6 vs Team 9
Round 3
Piste A Team 4 vs Rest
Piste B Team 5 vs Team 3
Piste C Team 6 vs Team 2
Piste D Team 7 vs Team 1
Round 4
Piste A Team 5 vs Rest
Piste B Team 6 vs Team 4
Piste C Team 7 vs Team 3
Piste D Team 8 vs Team 2
Round 5
Piste A Team 6 vs Rest
Piste B Team 7 vs Team 5
Piste C Team 8 vs Team 4
Piste D Team 9 vs Team 3
Round 6
Piste A Team 7 vs Rest
Piste B Team 8 vs Team 6
Piste C Team 9 vs Team 5
Piste D Team 1 vs Team 4
Round 7
Piste A Team 8 vs Rest
Piste B Team 9 vs Team 7
Piste C Team 1 vs Team 6
Piste D Team 2 vs Team 5
Round 8
Piste A Team 9 vs Rest
Piste B Team 1 vs Team 8
Piste C Team 2 vs Team 7
Piste D Team 3 vs Team 6
Round 9
Piste A Team 1 vs Rest
Piste B Team 2 vs Team 9
Piste C Team 3 vs Team 8
Piste D Team 4 vs Team 7
I am just really ignorant with this type of error it’s been almost 3 days with me trying to figure out a thing. Thanks in advance
>Solution :
You’re using the size of the matches list for the loop that prints the matches, which doesn’t account for the missing matches when there are fewer matches than the number of pistes. You need to modify the loop that prints the matches by using the length of the pistes array to ensure that you print all the required pistes.
for (Round round : rounds) {
System.out.println("Round " + round.getNumber());
List<Match> matches = round.getMatches();
for (int i = 0; i < pistes.length; i++) {
if (i < matches.size()) {
Match match = matches.get(i);
System.out.println(pistes[i] + " " + match.getTeam1().getName() + " vs " + match.getTeam2().getName());
} else {
System.out.println(pistes[i] + " -");
}
}
System.out.println();
}