// A class gradebook using parallel arrays.

/// Returns the mean of an array of numbers, or 0.0 for an empty one.
double average(int[] numbers) {
    if (numbers.length == 0) {
        return 0.0;
    }
    int total = 0;
    for (int number : numbers) {
        total += number;
    }
    return (double) total / numbers.length;
}

/// Returns the letter grade for a numeric score.
String letterGrade(int score) {
    if (score >= 90) {
        return "A";
    } else if (score >= 80) {
        return "B";
    } else if (score >= 70) {
        return "C";
    } else if (score >= 60) {
        return "D";
    } else {
        return "F";
    }
}

/// Prints each student with their score and letter grade.
void printRoster(String[] names, int[] scores) {
    IO.println(String.format("%-12s%6s%7s", "Student", "Score", "Grade"));
    IO.println("-".repeat(25));
    for (int i = 0; i < names.length; i++) {
        IO.println(String.format("%-12s%6d%7s",
                names[i], scores[i], letterGrade(scores[i])));
    }
}

/// Prints class statistics.
void printSummary(String[] names, int[] scores) {
    int best = 0;
    int lowest = scores[0];
    int passing = 0;
    for (int i = 0; i < scores.length; i++) {
        if (scores[i] > scores[best]) {
            best = i;
        }
        if (scores[i] < lowest) {
            lowest = scores[i];
        }
        if (scores[i] >= 60) {
            passing++;
        }
    }

    IO.println("-".repeat(25));
    IO.println(String.format("Class average: %.1f", average(scores)));
    IO.println("Highest:       " + scores[best] + " (" + names[best] + ")");
    IO.println("Lowest:        " + lowest);
    IO.println("Passing:       " + passing + " of " + scores.length);
}

void main() {
    String[] names = {"Ada", "Grace", "Alan", "Katherine", "Edsger"};
    int[] scores = {88, 92, 45, 95, 71};

    printRoster(names, scores);
    printSummary(names, scores);
}

// From Groundwork, chapter 7 (ch07-arrays-and-lists), box 30.
// This example is from Groundwork, licensed CC BY-NC-SA 4.0.
// Run with: java ch07_30.java
