Created
September 21, 2012 08:33
-
-
Save patio11/3760402 to your computer and use it in GitHub Desktop.
Java can be fun!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Student> studentsWhoHaveNotTakenAtLeastOneTest() { | |
List<Student> allStudents = this.getStudents().clone(); /* shallow copy to avoid mutation */ | |
Iterator<Student> studentIt = allStudents.iterator(); | |
/* select down to students missing at least one test */ | |
List<Student> studentsMissingAtLeastOneTest = new ArrayList<Student>(); | |
for(Student student : studentIt) { | |
List<Test> tests = student.getTests(); /* no shallow copy this time */ | |
if (tests.equals(null) || (tests.size == 0)) { | |
continue; | |
} else { | |
boolean shouldSelectThisStudent = false; | |
Iterator<Tests> testIt = tests.iterator(); | |
for(Test test : testIt) { | |
/* many Java programmers would short circuit this -- I don't usually do that.*/ | |
shouldSelectThisStudent = shouldSelectThisStudent || (!test.isTaken()); | |
} | |
if (shouldSelectThisStudent) { | |
studentsMissingAtLeastOneTest.addLast(student); | |
} | |
} | |
} | |
/* now sort by last name */ | |
Comparator<Student> lastNameSorter = new Comparator<Student,Student> { | |
public int compare(Student student1, Student student2) { | |
return student1.getLastName().compare(student2.getLastName()); /* dangerous code, please don't template off this, but it's legion in the field */ | |
} | |
} | |
return studentsMissingAtLeastOneTest.sort(lastNameSorter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment