Avatar of MrGarri

MrGarri's solution

to Grade School in the Java Track

Published at Aug 11 2019 · 0 comments
Test suite

Given students' names along with the grade that they are in, create a roster for the school.

In the end, you should be able to:

  • Add a student's name to the roster for a grade
    • "Add Jim to grade 2."
    • "OK."
  • Get a list of all students enrolled in a grade
    • "Which students are in grade 2?"
    • "We've only got Jim just now."
  • Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
    • "Who all is enrolled in school right now?"
    • "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"

Note that all our students only have one name. (It's a small town, what do you want?)

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • If you're working in a language with mutable data structures and your implementation allows outside code to mutate the school's internal DB directly, see if you can prevent this. Feel free to introduce additional tests.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?


Go through the setup instructions for Java to install the necessary dependencies:


Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.


A pairing session with Phil Battos at gSchool http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class SchoolTest {
    private School school;

    public void setUp() {
        school = new School();

    public void addingAStudentAddsThemToTheSortedRoster() {
        school = new School();
        school.add("Aimee", 2);
        List<String> expected = Arrays.asList("Aimee");
        assertEquals(expected, school.roster());

    @Ignore("Remove to run test")
    public void addingMoreStudentsAddsThemToTheSameSortedRoster() {
        school = new School();
        int grade = 2;
        school.add("Blair", grade);
        school.add("James", grade);
        school.add("Paul", grade);
        List<String> expected = Arrays.asList("Blair", "James", "Paul");
        assertEquals(expected, school.roster());

    @Ignore("Remove to run test")
    public void addingStudentsToDifferentGradesAddsThemToTheSameSortedRoster() {
        school = new School();
        school.add("Chelsea", 3);
        school.add("Logan", 7);
        List<String> expected = Arrays.asList("Chelsea", "Logan");
        assertEquals(expected, school.roster());

    @Ignore("Remove to run test")
    public void rosterReturnsAnEmptyListIfThereAreNoStudentsEnrolled() {
        school = new School();
        List<String> expected = Arrays.asList();
        assertEquals(expected, school.roster());

    @Ignore("Remove to run test")
    public void studentNamesWithGradesAreDisplayedInTheSameSortedRoster() {
        school = new School();
        school.add("Peter", 2);
        school.add("Anna", 1);
        school.add("Barb", 1);
        school.add("Zoe", 2);
        school.add("Alex", 2);
        school.add("Jim", 3);
        school.add("Charlie", 1);
        List<String> expected = Arrays.asList("Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim");
        assertEquals(expected, school.roster());

    @Ignore("Remove to run test")
    public void gradeReturnsTheStudentsInThatGradeInAlphabeticalOrder() {
        school = new School();
        school.add("Franklin", 5);
        school.add("Bradley", 5);
        school.add("Jeff", 1);
        List<String> expected = Arrays.asList("Bradley", "Franklin");
        assertEquals(expected, school.grade(5));

    @Ignore("Remove to run test")
    public void gradeReturnsAnEmptyListIfThereAreNoStudentsInThatGrade() {
        school = new School();
        List<String> expected = Arrays.asList();
        assertEquals(expected, school.grade(1));
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class School {

    private Map<Integer, Set<String>> students = new TreeMap<>();

    void add(String name, int grade) {
        students.merge(grade, new TreeSet<>(Collections.singletonList(name)),
                (o1, o2) -> new TreeSet<>(Stream.concat(o1.stream(), o2.stream()).collect(Collectors.toList())));

    List<String> roster() {
        return students.values()

    List<String> grade(int grade) {
        return new ArrayList<>(students.getOrDefault(grade, new TreeSet<>()));


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?