Reputation: 1
As part of my assignment, we are given an interface which we can't change, and some pre-defined tests to use to develop our methods.
The interface is as follows:
public interface ContactTracer {
public void addCourse(String courseID, String courseName);
public void addStudent(String id, String name, String email);
public void loadStudentCourseList(Map<String, String> enrolments);
public String getStudentName(String studentID);
public String getStudentEmail(String studentID);
public String getCourseForStudent(String studentID);
public String getCourseName(String courseID);
public List<String> findStudentsForCourse(String courseID);
public List<String> findContacts(String studentID);
}
The particular test I'm running:
@Test
void testLoadStudentCourseList() {
ContactTracer ct = new PartOneContactTracer();
Map<String, String> enrolments = new HashMap<>();
enrolments.put("S101", "SOFT01");
enrolments.put("S102", "NET02");
enrolments.put("S103", "SOFT01");
ct.loadStudentCourseList(enrolments);
List<String> students = ct.findStudentsForCourse("SOFT01");
assertEquals(2, students.size());
assertTrue(students.contains("S101"));
assertTrue(students.contains("S103"));
}
And the method I need to create from the test:
@Override
public List<String> findStudentsForCourse(String courseID) {
return null;
}
I can tell from the test that within the findStudentsForCourse
method I need to create List<String> students
and fill it with matching students from the enrolments Map, and then return the the List.
I have created the following fields:-
public class PartOneContactTracer implements ContactTracer {
private Map<String, String> courseDetails = new HashMap<>();
private Map<String, String> studentName = new HashMap<>();
private Map<String, String> studentEmail = new HashMap<>();
private Map<String, String> enrolments = new HashMap<>();
...
}
What I can't get my head around is how to pass the data in the enrolments Map into the students List in order to make the Assertion in the test true.
Upvotes: 0
Views: 114
Reputation: 721
You don't need to create a list and fill it just search through the map all the keys (students) that has the value (course id):
public List<String> findStudentsForCourse(String courseId) {
return enrolments.entrySet().stream()
.filter(e -> e.getValue().equals(courseId))
.map(e -> e.getKey())
.collect(Collectors.toList());
}
Upvotes: 1