dimanche 3 janvier 2016

Printing out selected objects from ArrayList depending on attribute value

I have a method for printing out Athletes results in a competition. This method prints out the highest scores for the input category. The problem is that it prints out every result for every athlete. I only want it to print out the highest value each athlete has gotten for the input category. Ive searched far and wide for an answer but cannot find out to how to do it with my limited knowledge. So my question is: How to only print out the highest value from 1 athlete so I get a real scoreboard?

void typeOutHighScores() {
    ArrayList<Athlete> athletes= AthleteList.getArrayList();
Collections.sort(resultlist);
    String categoryToShow = null;
    System.out.println("Which category would you like to show highscores for?");
    categoryToShow = scanner.nextLine();
    categoryToShow = normalize(categoryToShow); //Just makes all letters small with first letter as capital.
    category matchedCategory= null;
    ArrayList<category> categories = CategoryList.getArrayList();
    for (int i = 0; i < categories.size(); i++) {
        category c = categories .get(i);
        if (c.categoryName().equals(categoryToShow)) {
            matchedCategory= c;
            break;
        }
    }
    if (matchedCategory== null) {
        System.out.println("Couldn't find " + categoryToShow + ".");
    } else {
        System.out.println("Resultatlist for " + categoryToShow + ": ");
        for (int i = 0; i < resultlist.size(); i++) {

            Athlete matched = null;
            int order = 0;
            Result res = resultlist.get(i);
            if (res.categoryName().equals(categoryToShow)) {
                for (int x = 0; x < athletes.size(); x++) {
                    Athlete del = athletes.get(x);
                    if (res.athleteStartNumber() == del.startNumber()) {
                        matched = del;
                        order = i + 1;
                        System.out.println(order + ". " + matched.surName() + " " + matched.lastName()
                                + " has the result: " + res.categoryValue());
                        break;
                    }
                }
            }
        }

    }
}

Aucun commentaire:

Enregistrer un commentaire