1 package examples; 2 3 import java.util.Collections ; 4 import java.util.Iterator ; 5 import java.util.LinkedHashSet ; 6 import java.util.Set ; 7 8 9 12 public class Student extends Person 13 { 14 protected String school; 15 protected Set courses = new LinkedHashSet (); 16 17 public void setSchool(String school) 18 { 19 this.school = school; 20 } 21 22 public String getSchool() 23 { 24 return this.school; 25 } 26 27 public void addCourse(Course course) 28 { 29 courses.add(course); 30 } 31 32 public void removeCourse(Course course) 33 { 34 courses.remove(course); 35 } 36 37 public Set getCourses() 38 { 39 return Collections.unmodifiableSet(courses); 40 } 41 42 public String toString() 43 { 44 StringBuffer buf = new StringBuffer (); 45 buf.append("{Name = " +name).append(", School = " +school); 46 if (address != null) 47 buf.append(", Address = " + address.getSimpleAddress()); 48 buf.append("}\n"); 49 buf.append("Courses:\n"); 50 for (Iterator iter = getCourses().iterator(); iter.hasNext(); ) 51 buf.append(iter.next()); 52 53 return buf.toString(); 54 } 55 56 } 57 | Popular Tags |