-
-
Save anonymous/a01c4c136768478dd763924e83d62ac8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.Random; | |
public class Main { | |
private static String[] teamNames, playerNames; | |
public static void main(String[] args) { | |
teamNames = new String[] { | |
"Java FC", | |
"Manchester United", | |
"Arsenal", | |
"Chelsea", | |
"Liverpool" | |
}; | |
playerNames = new String[] {"Async", "Jack", "William", "Robin", "Callum"}; | |
Team[] teams = generateTeams(5); | |
for (Team t : teams) { | |
System.out.println("Team Name: " + t.getName()); | |
for (Player p : t.getPlayers()) { | |
System.out.println("Player Name: " + p.getName()); | |
} | |
System.out.println(); | |
} | |
} | |
private static Team[] generateTeams(int amount) { | |
Team[] teams = new Team[amount]; | |
Random rand = new Random(); | |
for (int i = 0; i < amount; i++) { | |
String teamName = teamNames[rand.nextInt(teamNames.length)]; | |
Team t = new Team(teamName); | |
Player[] players = new Player[11]; | |
for (int j = 0; j < 11; j++) { | |
String playerName = playerNames[rand.nextInt(playerNames.length)]; | |
players[j] = new Player(playerName, t); | |
} | |
t.setPlayers(players); | |
teams[i] = t; | |
} | |
return teams; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
public class Player { | |
private String name; | |
private Team team; | |
public Player(String name, Team team) { | |
this.name = name; | |
this.team = team; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Team getTeam() { | |
return team; | |
} | |
public void setTeam(Team team) { | |
this.team = team; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
public class Team { | |
private String name; | |
private Player[] players; | |
public Team(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Player[] getPlayers() { | |
return players; | |
} | |
public void setPlayers(Player[] players) { | |
if (players.length != 11) { | |
System.out.println("Error message here"); | |
return; | |
} | |
this.players = players; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment