This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
2.2 KiB

import java.util.List;
import java.util.HashSet;
import java.util.Set;
/*
* SD2x Homework #10
* Modify the method below so that it uses defensive programming.
* Please be sure not to change the method signature!
*/
public class FriendFinder {
protected ClassesDataSource classesDataSource;
protected StudentsDataSource studentsDataSource;
public FriendFinder(ClassesDataSource cds, StudentsDataSource sds) {
classesDataSource = cds;
studentsDataSource = sds;
}
public Set<String> findClassmates(Student theStudent) {
if(theStudent == null) throw new IllegalArgumentException();
if(theStudent.getName() == null) throw new IllegalArgumentException();
if(this.classesDataSource == null) throw new IllegalStateException();
if(this.studentsDataSource== null) throw new IllegalStateException();
String name = theStudent.getName();
if (name == null) throw new IllegalStateException();
// find the classes that this student is taking
List<String> myClasses = classesDataSource.getClasses(name);
if (myClasses == null) return null;
Set<String> classmates = new HashSet<String>();
// use the classes to find the names of the students
for (String myClass : myClasses) {
// list all the students in the class
if(myClass == null) continue;
List<Student> students = studentsDataSource.getStudents(myClass);
if (students == null) continue;
for (Student student : students) {
if (student == null) continue;
if(student.getName() == null) continue;
// find the other classes that they're taking
List<String> theirClasses = classesDataSource.getClasses(student.getName());
if (theirClasses == null) continue;
// see if all of the classes that they're taking are the same as the ones this student is taking
boolean same = true;
for (String c : myClasses) {
if (c == null) continue;
if (theirClasses.contains(c) == false) {
same = false;
break;
}
}
if (same) {
if (student.getName().equals(name) == false && classmates.contains(student.getName()) == false)
classmates.add(student.getName());
}
}
}
if (classmates.isEmpty()) {
return null;
}
else return classmates;
}
}