Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Set;
- import java.util.HashSet;
- import java.util.Collections;
- /**
- * Write a description of class SchoolClass here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class SchoolClass
- {
- private String name;
- private Set<Student> students = new HashSet<Student>();
- public SchoolClass(String name){
- if(name == null){
- throw new IllegalArgumentException("name is null!");
- }
- if(name.length() == 0){
- throw new IllegalArgumentException("name not long enough!");
- }
- this.name = name;
- }
- public String getName(){
- return name;
- }
- public void addStudent(Student student) throws ClassException{
- if(student == null){
- throw new NullPointerException("null student!");
- }
- if(students.contains(student)){
- throw new ClassException("student is already in the class!");
- }
- students.add(student);
- }
- public void removeStudent(Student student) throws ClassException{
- if(!(students.contains(student))){
- throw new ClassException("student not in the class!");
- }
- students.remove(student);
- }
- public int getClassAbsence(){
- int totalAbsence = 0;
- for(Student s : students){
- totalAbsence += s.getAbsences();
- }
- return totalAbsence;
- }
- public Set<Student> getAllStudents(){
- return students;
- }
- public Set<Student> getStudentsWithAbsence(){
- Set<Student> absenceStudents = new HashSet<Student>();
- for(Student s : students){
- if(s.getAbsences() > 0){
- absenceStudents.add(s);
- }
- }
- return absenceStudents;
- }
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!upg
- public Set<Student> getStudentsWithoutAbsence(){
- Set<Student> symmetricDiff = new HashSet<Student>(getAllStudents());
- symmetricDiff.addAll(getAllStudents());
- // symmetricDiff now contains the union
- Set<Student> tmp = new HashSet<Student>(getStudentsWithAbsence());
- tmp.retainAll(getStudentsWithAbsence());
- // tmp now contains the intersection
- symmetricDiff.removeAll(tmp);
- // union minus intersection equals symmetric-difference
- /*
- Set<Student> absenceStudents = new HashSet<Student>();
- for(Student s : students){
- if(s.getAbsences() == 0){
- absenceStudents.add(s);
- }
- }
- return absenceStudents;
- */
- return symmetricDiff;
- }
- public Set<Student> removeStudents(String name){
- Set<Student> removedStudents = new HashSet<Student>();
- try{
- for(Student s : students){
- if(s.getName() == name){
- removedStudents.add(s);
- removeStudent(s);
- }
- }
- }
- catch(ClassException ex){
- }
- return removedStudents;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement