Avatar of artemkorsakov

artemkorsakov's solution

to Grade School in the C# Track

Published at Apr 02 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?

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit GradeSchool.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

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

GradeSchoolTest.cs

// This file was auto-generated based on version 1.0.0 of the canonical data.

using System;
using Xunit;

public class GradeSchoolTest
{
    [Fact]
    public void Adding_a_student_adds_them_to_the_sorted_roster()
    {
        var sut = new GradeSchool();
        sut.Add("Aimee", 2);
        var expected = new[] { "Aimee" };
        Assert.Equal(expected, sut.Roster());
    }

    [Fact(Skip = "Remove to run test")]
    public void Adding_more_student_adds_them_to_the_sorted_roster()
    {
        var sut = new GradeSchool();
        sut.Add("Blair", 2);
        sut.Add("James", 2);
        sut.Add("Paul", 2);
        var expected = new[] { "Blair", "James", "Paul" };
        Assert.Equal(expected, sut.Roster());
    }

    [Fact(Skip = "Remove to run test")]
    public void Adding_students_to_different_grades_adds_them_to_the_same_sorted_roster()
    {
        var sut = new GradeSchool();
        sut.Add("Chelsea", 3);
        sut.Add("Logan", 7);
        var expected = new[] { "Chelsea", "Logan" };
        Assert.Equal(expected, sut.Roster());
    }

    [Fact(Skip = "Remove to run test")]
    public void Roster_returns_an_empty_list_if_there_are_no_students_enrolled()
    {
        var sut = new GradeSchool();
        var expected = Array.Empty<string>();
        Assert.Empty(sut.Roster());
    }

    [Fact(Skip = "Remove to run test")]
    public void Student_names_with_grades_are_displayed_in_the_same_sorted_roster()
    {
        var sut = new GradeSchool();
        sut.Add("Peter", 2);
        sut.Add("Anna", 1);
        sut.Add("Barb", 1);
        sut.Add("Zoe", 2);
        sut.Add("Alex", 2);
        sut.Add("Jim", 3);
        sut.Add("Charlie", 1);
        var expected = new[] { "Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim" };
        Assert.Equal(expected, sut.Roster());
    }

    [Fact(Skip = "Remove to run test")]
    public void Grade_returns_the_students_in_that_grade_in_alphabetical_order()
    {
        var sut = new GradeSchool();
        sut.Add("Franklin", 5);
        sut.Add("Bradley", 5);
        sut.Add("Jeff", 1);
        var expected = new[] { "Bradley", "Franklin" };
        Assert.Equal(expected, sut.Grade(5));
    }

    [Fact(Skip = "Remove to run test")]
    public void Grade_returns_an_empty_list_if_there_are_no_students_in_that_grade()
    {
        var sut = new GradeSchool();
        var expected = Array.Empty<string>();
        Assert.Empty(sut.Grade(1));
    }
}
using System.Collections.Generic;
using System.Linq;

public class GradeSchool
{
    private readonly List<Student> _students = new List<Student>();

    public void Add(string student, int grade)
    {
        _students.Add(new Student(student, grade));
    }

    public IEnumerable<string> Roster() => _students
        .OrderBy(s => s.Grade).ThenBy(s => s.Name).Select(s => s.Name);

    public IEnumerable<string> Grade(int grade) => _students
        .Where(s => s.Grade == grade).OrderBy(s => s.Name).Select(s => s.Name);

    private class Student
    {
        internal string Name { get; }
        internal int Grade { get; }

        internal Student(string name, int grade)
        {
            Name = name;
            Grade = grade;
        }
    }
}

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?