简体   繁体   中英

What is the best way to Sort clubs into leagues in java

Hello and thank you for your time in advanced.

I am trying to create a few different football leagues (eg the Italian national league (Serie A ), the English national league (Premier league), ... , second division Italian league (Serie B), etc).

Right now I'm trying to figure out what's the best way to separate clubs of different country's so that a Italian club cant compete in a Spanish league and vice versa.

As of now I have created the Player, PlayerBuilder, Team and TeamBuilder classes.

public class FootBallClub {

private String name;
private String stadium;
private String mainCoach;
private String secondCoach;
private ArrayList<FootballPlayer> playerList;
private ArrayList Matches;
private String county;

private int gamesPlayed;
private int gamesWon;
private int gamesDrawn;
private int gamesLost;
private int goalsScored;
private int goalsConceded;

private FootballPlayer topGoalScorer;

public FootBallClub() {
}

public void addPlayer(FootballPlayer player) {
    playerList.add(player);
}

public void addPlayers (ArrayList<FootballPlayer> players) {
    for (FootballPlayer player: players){
        playerList.add(player);
    }
}


public ArrayList<FootballPlayer> getPlayerList() {
    ArrayList<FootballPlayer> players = new ArrayList<>();
    for (FootballPlayer player : players) {
        try {
            players.add(player.clone());
        } catch (CloneNotSupportedException e) {
            System.out.println("Football Player " + player.getName() + " could not be cloned");
            System.out.println(e.getStackTrace());
        }
    }
    return players;
}

// getters and setters for all the fields

public static ArrayList<FootballPlayer> createTeam(){

    String first = "Liam,Noah,Oliver,Elijah,William,James,Benjamin,Lucas,Henry,Alexander,Mason,Michael,Ethan,Daniel,Jacob,Logan,Jackson,Levi,Sebastian,Mateo,Jack,Owen,Theodore,Aiden,Samuel,Joseph,John,David,Wyatt,Matthew,Luke,Asher,Carter,Julian,Grayson,Leo,Jayden,Gabriel,Isaac,Lincoln,Anthony,Hudson,Dylan,Ezra,Thomas";
    String [] firstNames = first.split(",");

    String last = "Smith,Johnson,Williams,Brown,Jones,Garcia,Miller,Davis,Rodriguez,Martinez,Hernandez,Lopez,Gonzalez,Wilson,Anderson,Thomas,Taylor,Moore,Jackson,Martin,Lee,Perez,Thompson,White,Harris,Sanchez,Clark,Ramirez,Lewis,Robinson,Walker,Young,Allen,King,Wright,Scott,Torres,Nguyen,Hill,Flores,Green,Adams,Nelson,Baker,Hall,Rivera,Campbell,Mitchell,Carter,Roberts,Gomez,Phillips,Evans,Turner,Diaz,Parker,Cruz,Edwards,Collins,Reyes,Stewart,Morris,Morales,Murphy,Cook,Rogers,Gutierrez,Ortiz,Morgan,Cooper,Peterson,Bailey,Reed,Kelly,Howard,Ramos,Kim,Cox,Ward,Richardson,Watson,Brooks,Chavez,Wood,James,Bennett,Gray,Mendoza,Ruiz,Hughes,Price,Alvarez,Castillo,Sanders,Patel,Myers,Long,Ross,Foster,Jimenez,Powell,Jenkins,Perry,Russell,Sullivan,Bell,Coleman,Butler,Henderson,Barnes,Gonzales,Fisher,Vasquez,Simmons,Romero,Jordan,Patterson,Alexander,Hamilton,Graham,Reynolds,Griffin,Wallace,Moreno,West,Cole,Hayes,Bryant,Herrera,Gibson,Ellis,Tran,Medina,Aguilar,Stevens,Murray,Ford,Castro,Marshall,Owens,Harrison,Fernandez,Mcdonald,Woods,Washington,Kennedy,Wells,Vargas,Henry,Chen,Freeman,Webb,Tucker,Guzman,Burns,Crawford,Olson,Simpson,Porter,Hunter,Gordon,Mendez,Silva,Shaw,Snyder,Mason,Dixon,Munoz,Hunt,Hicks,Holmes,Palmer,Wagner,Black,Robertson,Boyd,Rose,Stone,Salazar,Fox,Warren,Mills,Meyer,Rice,Schmidt,Garza,Daniels";
    String [] lastNames = last.split(",");

    Random random = new Random();
    final List<FootballPosition> footballPositions = Collections.unmodifiableList(Arrays.asList(FootballPosition.values()));
    final List<DominantSide> sides = Collections.unmodifiableList(Arrays.asList(DominantSide.values()));

    ArrayList<FootballPlayer> footballPlayers = new ArrayList<FootballPlayer>();

    for (int i =0; i<footballPositions.size() ; i++) {
        String randomeName = firstNames[random.nextInt(1000)]+ " "+ lastNames[random.nextInt(1000)];

        int randomAge = 18 + random.nextInt(23);
        int randomHeight = 165 + random.nextInt(30);
        int randomWeight = (int) ((randomHeight - 100) * 0.85 + random.nextInt(25));

        DominantSide randomSide = sides.get(random.nextInt(3));
        boolean isHealthy = true;
        int randomSalary = 700000 + random.nextInt(5000000);

        FootballPosition primaryPosition = footballPositions.get(i);
        List<FootballPosition> allPosition = new ArrayList<FootballPosition>();
        allPosition.add(primaryPosition);
        EnumSet set = EnumSet.noneOf(FootballPosition.class);
        for (int j = 0; j < random.nextInt(4); j++) {
            FootballPosition newPosition = footballPositions.get(random.nextInt(footballPositions.size()));
            if(allPosition.contains(newPosition)){
                i--;
            }
            set.add(newPosition);
        }
        FootballPlayer randomPlayer = new FootballPlayerBuilder()
                .setName(randomeName)
                .setAge(randomAge)
                .setHeight(randomHeight)
                .setWeight(randomWeight)
                .setDominantSide(randomSide)
                .setIsHealthy(isHealthy)
                .setSalary(randomSalary)
                .setPrimaryPosition(primaryPosition)
                .setSecondaryPositions(set)
                .build();

        footballPlayers.add(randomPlayer);

    }
    return footballPlayers;
}

}

public class FootballPlayer {

private String name;
private FootballPosition primaryPosition;
private EnumSet<FootballPosition> secondaryPositions;
private double height;
private double weight;
private int age;
private Enum dominantSide;
private boolean isHealthy;
private int salary;

public FootballPlayer() {
}


// getters and setters for all the fields

@Override
public String toString() {
    return "FootballPlayer{" +
            "name='" + name + '\'' +
            ", primaryPosition=" + primaryPosition +
            ", secondaryPositions=" + secondaryPositions +
            ", height=" + height +
            ", weight=" + weight +
            ", age=" + age +
            ", dominantSide=" + dominantSide +
            ", isHealthy=" + isHealthy +
            ", salary=" + salary +
            '}';
}

@Override
public FootballPlayer clone() throws CloneNotSupportedException {
    FootballPlayer clone = new FootballPlayerBuilder()
            .setName(this.name)
            .setAge(this.age)
            .setHeight((int) this.height)
            .setWeight((int) this.weight)
            .setDominantSide(this.dominantSide)
            .setIsHealthy(this.isHealthy)
            .setSalary(this.salary)
            .setPrimaryPosition(this.primaryPosition)
            .setSecondaryPositions(this.secondaryPositions)
            .build();
    return clone;
}

}

Player and Team simply hold information about the player and team through their field and the builders build the objects.

None of the classes implement or extend any classes.

My question is: What's the best way implementing the League class so that a league from a certain county only accepts teams form the same country.

Should I save a string, int, enum..., on the team class and check before adding to the league table? Is the a clever way to use interfaces to salve this problem?

Honestly, I am thinking of adding a string that indicates the country and then check if this string is compatible with the league.

Consider making an enum, it is definitely better than a generic string:

enum Country {
    Spain,
    Germany,
    UK
}

Each league may contain a HashSet<FootBallClub> . So, you can put football clubs into leagues as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM