Avatar of AlKYala

AlKYala's solution

to Grade School in the Java Track

Published at Oct 01 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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class School {
	
	public School() {}
	
	HashMap<Integer, ArrayList<String>> mySchool = new HashMap<Integer, ArrayList<String>>(); //ArrayList for each grade 
	ArrayList<Integer> classes = new ArrayList<Integer>(); //Saves which classes have been used
	
	public void add(String name, int grade) {
		try {
			this.mySchool.get(grade).add(name); //gets the list for the grade, adds name to said list
		} catch(NullPointerException e) {
			this.classes.add(grade);
			ArrayList<String> names = new ArrayList<String>();
			names.add(name);
			this.mySchool.put(grade, names);
		}
		
	}
	
	
	public List<String> grade(int grade){
		/*try {
		List<String> ret = this.mySchool.get(grade);
		} catch(NullPointerException e) {
			
			return Collections.emptyList();
		}*/
		if(this.mySchool.get(grade) != null) {
			List<String> ret = this.mySchool.get(grade);
		if(ret.size() >= 1) {
			Collections.sort(ret);
			return ret;
		}
		else {
			return Collections.emptyList();
		}
		}else {
			return Collections.emptyList();
		}
	}
	
	public List<String> roster(){
		Collections.sort(this.classes);
		if(classes.isEmpty()) { //nobody enrolled
			return Arrays.asList();
		}
		
		ArrayList<String> ret = (ArrayList<String>) this.grade(this.classes.get(0)); //youngest class
		
		for(int i = 1; i < this.classes.size(); i++) {
			ArrayList<String> b = (ArrayList<String>) this.grade(this.classes.get(i)); //get next class
			Collections.sort(b); //sort it
			for(int j = 0; j < b.size(); j++) {
				ret.add(b.get(j)); //now add
			}
		}

			return ret;
		
	}
	
	/*public static void main(String[] args) {
		School a = new School();
		a.add("B", 1);
		a.add("C", 1);
		a.add("A", 1);
		a.add("A", 3);
		a.add("G", 2);
		a.add("F", 2);
		System.out.println(a.grade(4));
		System.out.println(a.roster());
	}*/

}


class Student{ //planned but not used in this project
	
	private String name;
	private int grade;
	
	public Student(String name, int grade) {
		this.name = name;
		this.grade = grade;
	}
	
	public String getName() {
		return this.name;
	}
	
	public int getGrade() {
		return this.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?