/* * ======================================================= * Student.java; Create student objects ... * ======================================================= */ import java.util.Collection; import java.util.ArrayList; import java.util.Iterator; public class Student { private int id; private String name; // Collection of departments in which the student enrols. private Collection departments; // Constuctor method ... public Student() { departments = new ArrayList(); } // Setup student Ids ... public int getId() { return id; } public void setId(int id) { this.id = id; } // Set student name ... public String getName() { return name; } public void setName(String name) { this.name = name; } // Student enrols in a department ... public void addDepartment(Department department) { // Add new department to students resume .. if ( getDepartments().contains(department) == false ) { getDepartments().add(department); } // Update list of students enrolled in the department .. if ( department.getStudents().contains(this) == false ) { department.getStudents().add(this); } } // Student drops-out of a department ... public void removeDepartment(Department department) { // Remove department from students resume .. if ( getDepartments().contains(department) == true ) { getDepartments().remove(department); } // Update list of students enrolled in the department .. if ( department.getStudents().contains(this) == true ) { department.getStudents().remove(this); } } // Return collection of departments ... public Collection getDepartments() { return departments; } public void setDepartment( Collection departments) { this.departments = departments; } // Create String representation of student object ... public String toString() { String s = "Student: " + name + "\n"; s = s + "Departments: "; if ( departments.size() == 0 ) s = s + " none \n"; else { Iterator iterator1 = departments.iterator(); while ( iterator1.hasNext() != false ) { Department dept = (Department) iterator1.next(); s = s + dept.getName() + " "; } s = s + "\n"; } return s; } }