Sha256: 91f889def49e596eceb4c80254d0152dad6de219c898ad14c14f2843921b1dcb

Contents?: true

Size: 1.06 KB

Versions: 78

Compression:

Stored size: 1.06 KB

Contents

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class School {

  private final Map<Integer, Set<String>> database = new HashMap<Integer, Set<String>>();

  public Map<Integer, Set<String>> db() {
    // Leaks internal storage to caller
    return database;
  }

  public void add(String student, int grade) {
    Set<String> students = grade(grade);
    students.add(student);
  }

  public Set<String> grade(int grade) {
    // Leaks internal storage to caller
    if (!database.containsKey(grade)) {
      database.put(grade, new TreeSet<String>());
    }
    return database.get(grade);
  }

  public Map<Integer, List<String>> sort() {
    Map<Integer, List<String>> sortedStudents = new HashMap<Integer, List<String>>();
    for (Integer grade : database.keySet()) {
      // Relies on using a TreeSet internally
      List<String> sortedGrade = new ArrayList<String>(database.get(grade));
      sortedStudents.put(grade, sortedGrade);
    }
    return sortedStudents;
  }
}

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
trackler-2.0.6.22 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.21 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.20 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.19 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.18 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.17 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.16 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.15 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.14 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.13 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.12 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.11 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.10 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.9 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.8 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.7 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.6 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.5 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.4 tracks/java/exercises/grade-school/src/example/java/School.java
trackler-2.0.6.3 tracks/java/exercises/grade-school/src/example/java/School.java