lundi 10 août 2020

Random generation of teams according to age and gender in Java

I'm trying to develop a short program with which you can select random groups from a list of people (read from a csv file), which are nevertheless so fairly distributed that each group contains people of different ages and genders. The user can specify how many groups / teams there should be in total. In the meantime I have reached the point where I can load a csv file, select it and create an array list that contains an object of the "Person" class for each person. This is how the person class is structured:

public class Person implements Comparable<Person> {

    private Integer birthYear;
    private String name;
    private String surname;
    private String gender;

    public Person(String birthYear, String name, String surname, String gender) {
        this.birthYear = Integer.parseInt(birthYear);
        this.name = name;
        this.surname = surname;
        this.gender = gender;
    }

    @Override
    public int compareTo(Persono) {
        return this.getBirthYear().compareTo(o.getBirthYear());
    }

    public String printInfo() {
        String info = "Surname: " + surname + " Name: " + name + " Year of Birth: " + birthYear + " Gender: " + gender;
        System.out.println(info);
        return info;
    }

    public Integer getBirthYear() {
        return birthYear;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getGender() {
        return gender;
    }

    public void setBirthYear(Integer birthYear) {
        this.birthYear = birthYear;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public void setGender(String gender) {
        this.gender= gender;
    }

}

I'm trying to create a class "Manager" that already splits an array list with people into two array lists according to gender. Another method in the manager class uses an array list with people to create a map that has an integer year of birth as a key and an array list with the people who have the value of the integer as a year of birth as the value.

public class Manager {

    ArrayList<Person> male = new ArrayList<>();
    ArrayList<Person> female = new ArrayList<>();

    public Manager(ArrayList<Person> people, int numOfTeams) {
        splitSex(people);
        sortByAge();
        generateGroups(male, female, numOfTeams);
    }

    private void splitSex(ArrayList<Person> people) {
        if (people != null) {
            for (Person person : Person) {
                    if (person.getGender().equals("m")) {
                        male.add(person);
                    } else if (person.getGender().equals("f")) {
                        female.add(person);
                    } else
                        System.err.println(person.getGender() + "is not a Gender.");
                }
            }
        }

    }

    private void sortByAge() {
        Collections.sort(male);
        Collections.sort(female);
    }

    private void generateGroups(ArrayList<Person> male, ArrayList<Person> female, int numOfGroups) {
        males = getAgeSortedGroups(male);
        females = getAgeSortedGroups(female);
    }

    private Map<Iteger, List<Person> getAgeSortedGroups(ArrayList<Person> list) {
        Map<Integer, List<Person>> ageGroups = list.stream().collect(Collectors.groupingBy(Person::getBirthYear));
        return ageGroups;
    }

}

(I think the method "sortByAge()" and the implementation of "Compareble" aren't necessary anymore bcause of the method "getAgeSortedGroups(ArrayList list)").

Now i want to somehow write a method that generates similar, fair, but still random groups / teams from the two maps (one map with male people, the other with female people).

E.g.: The user says he wants to generate 10 groups/teams and the Csv-File includes 60 People of males and females and all born between 2002 and 2010. Now I want that each of the 10 teams includes the same proportion of males and females and of the different ages.

I guess I have to somehow write a loop in which the groups (as lists) are created and the people are assigned. but i have no clue how. I hope you get my problem (I am no native speaker).

Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire