Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of rootulp

rootulp's solution

to Grade School in the Java Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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?

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.


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 org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Collection;

import org.hamcrest.Matcher;
import org.hamcrest.collection.IsIterableContainingInOrder;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

public class SchoolTest {
  private School school;

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

  public void startsWithNoStudents() {
    assertThat(school.numberOfStudents(), is(0));

  @Ignore("Remove to run test")
  public void addsStudents() {
    school.add("Aimee", 2);
    assertThat(school.grade(2), hasItem("Aimee"));

  @Ignore("Remove to run test")
  public void addsMoreStudentsInSameGrade() {
    int grade = 2;
    school.add("James", grade);
    school.add("Blair", grade);
    school.add("Paul", grade);

    assertThat(school.grade(grade).size(), is(3));
    assertThat(school.grade(grade), allOf(hasItem("James"), hasItem("Blair"), hasItem("Paul")));

  @Ignore("Remove to run test")
  public void addsStudentsInMultipleGrades() {
    school.add("Chelsea", 3);
    school.add("Logan", 7);

    assertThat(school.numberOfStudents(), is(2));
    assertThat(school.grade(3).size(), is(1));
    assertThat(school.grade(3), hasItem("Chelsea"));
    assertThat(school.grade(7).size(), is(1));
    assertThat(school.grade(7), hasItem("Logan"));

  @Ignore("Remove to run test")
  public void getsStudentsInEmptyGrade() {

  @Ignore("Remove to run test")
  public void sortsSchool() {
    school.add("Kyle", 4);
    school.add("Zed", 4);
    school.add("Adam", 4);
    school.add("Jennifer", 4);
    school.add("Kareem", 6);
    school.add("Christopher", 4);
    school.add("Kylie", 3);
    Map<Integer, Matcher> sortedStudents = new HashMap<Integer, Matcher>();
    sortedStudents.put(6, IsIterableContainingInOrder
    sortedStudents.put(4, IsIterableContainingInOrder
      .contains("Adam", "Christopher", "Jennifer", "Kyle", "Zed"));
    sortedStudents.put(3, IsIterableContainingInOrder

    Map schoolStudents = school.studentsByGradeAlphabetical();
    for (Map.Entry<?, Matcher> entry : sortedStudents.entrySet()) {

      assertThat((Collection) schoolStudents.get(entry.getKey()), entry.getValue());

  @Ignore("Remove to run test")
  public void modifyingFetchedGradeShouldNotModifyInternalDatabase() {
    String shouldNotBeAdded = "Should not be added to school";
    int grade = 1;

    Collection students = school.grade(grade);

    try {
    } catch (Exception exception) {
      // Also valid that the add operation throws an exception
      // Such as UnsupportedOperationException when an umodifiable collection type is used

    assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded)));

  @Ignore("Remove to run test")
  public void modifyingSortedStudentsShouldNotModifyInternalDatabase() {
    int grade = 2;
    String studentWhichShouldNotBeAdded = "Should not be added";
    List<String> listWhichShouldNotBeAdded = new ArrayList<>();

    Map sortedStudents = school.studentsByGradeAlphabetical();

    try {
      sortedStudents.put(grade, listWhichShouldNotBeAdded);
    } catch (Exception exception) {
      // Also valid that the put operation throws an exception
      // Such as UnsupportedOperationException when an unmodifiableMap is used

import java.util.*;

public class School {

  private Map<Integer, List<String>> db = new HashMap<Integer, List<String>>();

  public Map<Integer, List<String>> db() {
    return db;

  public void add(String name, Integer grade) {
    List<String> temp = grade(grade);
    db.put(grade, temp);

  public List<String> grade(Integer grade) {
    if (db.containsKey(grade)) {
      return db.get(grade);
    return new ArrayList<String>();

  public Map<Integer, List<String>> sort() {
    for (Integer grade : db.keySet()) {
      List<String> curr = db.get(grade);
    return db;


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?