Avatar of oneillb

oneillb's solution

to Grade School in the Java Track

Published at Sep 18 2019 · 0 comments
Instructions
Test suite
Solution

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?

Setup

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

https://exercism.io/tracks/java/installation

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.

Source

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.

SchoolTest.java

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;

    @Before
    public void setUp() {
        school = new School();
    }

    @Test
    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")
    @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")
    @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")
    @Test
    public void rosterReturnsAnEmptyListIfThereAreNoStudentsEnrolled() {
        school = new School();
        List<String> expected = Arrays.asList();
        assertEquals(expected, school.roster());
    }

    @Ignore("Remove to run test")
    @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")
    @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")
    @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;

public class School {
    private Map<Integer, List<String>> grades = new TreeMap<>();

    public List<String> roster() {
        return grade(-1);
    }

    public void add(String name, int grade) {
        grades.computeIfAbsent(grade, (key) -> new ArrayList<>()).add(name);
        Collections.sort(grades.get(grade));
    }

    public List<String> grade(int grade) {
        return grades.entrySet()
                .stream()
                .filter(x -> -1 == grade || x.getKey() == grade )
                .map(x -> x.getValue())
                .flatMap(x -> x.stream())
                .collect(Collectors.toList());
    }
}

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?