Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import java.util.Set;
  2. import java.util.HashSet;
  3. import java.util.Collections;
  4.  
  5. /**
  6.  * Write a description of class SchoolClass here.
  7.  *
  8.  * @author (your name)
  9.  * @version (a version number or a date)
  10.  */
  11. public class SchoolClass
  12. {
  13.     private String name;
  14.     private Set<Student> students = new HashSet<Student>();
  15.    
  16.     public SchoolClass(String name){
  17.         if(name == null){
  18.             throw new IllegalArgumentException("name is null!");
  19.         }
  20.         if(name.length() == 0){
  21.             throw new IllegalArgumentException("name not long enough!");
  22.         }
  23.         this.name = name;
  24.     }
  25.     public String getName(){
  26.         return name;
  27.     }
  28.     public void addStudent(Student student) throws ClassException{
  29.         if(student == null){
  30.             throw new NullPointerException("null student!");
  31.         }
  32.         if(students.contains(student)){
  33.             throw new ClassException("student is already in the class!");
  34.         }
  35.         students.add(student);
  36.     }
  37.     public void removeStudent(Student student) throws ClassException{
  38.         if(!(students.contains(student))){
  39.             throw new ClassException("student not in the class!");
  40.         }
  41.         students.remove(student);
  42.     }
  43.     public int getClassAbsence(){
  44.         int totalAbsence = 0;
  45.         for(Student s : students){
  46.             totalAbsence += s.getAbsences();
  47.         }
  48.         return totalAbsence;
  49.     }
  50.     public Set<Student> getAllStudents(){
  51.         return students;
  52.     }
  53.     public Set<Student> getStudentsWithAbsence(){
  54.         Set<Student> absenceStudents = new HashSet<Student>();
  55.         for(Student s : students){
  56.             if(s.getAbsences() > 0){
  57.                 absenceStudents.add(s);
  58.             }
  59.         }
  60.         return absenceStudents;
  61.     }
  62.     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!upg
  63.     public Set<Student> getStudentsWithoutAbsence(){
  64.         Set<Student> symmetricDiff = new HashSet<Student>(getAllStudents());
  65.         symmetricDiff.addAll(getAllStudents());
  66.         // symmetricDiff now contains the union
  67.         Set<Student> tmp = new HashSet<Student>(getStudentsWithAbsence());
  68.         tmp.retainAll(getStudentsWithAbsence());
  69.         // tmp now contains the intersection
  70.         symmetricDiff.removeAll(tmp);
  71.         // union minus intersection equals symmetric-difference
  72.         /*
  73.         Set<Student> absenceStudents = new HashSet<Student>();
  74.         for(Student s : students){
  75.             if(s.getAbsences() == 0){
  76.                 absenceStudents.add(s);
  77.             }
  78.         }
  79.         return absenceStudents;
  80.         */
  81.         return symmetricDiff;
  82.     }
  83.     public Set<Student> removeStudents(String name){
  84.         Set<Student> removedStudents = new HashSet<Student>();
  85.         try{
  86.             for(Student s : students){
  87.                
  88.                 if(s.getName() == name){
  89.                     removedStudents.add(s);
  90.                     removeStudent(s);
  91.                 }
  92.             }
  93.         }
  94.         catch(ClassException ex){
  95.            
  96.         }
  97.         return removedStudents;
  98.     }
  99.    
  100.    
  101.    
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement