Course_Student_SchoolClass(Day14)

package SchoolClass; import java.util.*; public class Student { private String studentID; private String studentName; private HashSet<Course> hs=new HashSet<Course>(); public Student(String studentID, String studentName) { super(); this.studentID = studentID; this.studentName = studentName; } public String getStudentID() { return studentID; } public String getStudentName() { return studentName; } public HashSet<Course> getHs() { return hs; } @Override public boolean equals(Object obj) { if(obj instanceof Student){ Student s=(Student)obj; return this.studentID.equals(s.studentID); } return false; } @Override public int hashCode() { return this.studentID.hashCode()^0xbd89f423; } @Override public String toString() { return super.toString()+"[studentID="+this.studentID+",studentName="+this.studentName+"]"; } public boolean addCourse(Course c){ if(hs.add(c)){ return true; }else{ return false; } } public boolean removeCourse(String name){ Iterator<Course> it=hs.iterator(); while(it.hasNext()){ if(it.next().getCourseName().equals(name)){ it.remove(); return true; } } return false; } } package SchoolClass; public class Course { private String courseID; private String courseName; public Course(String courseID, String courseName) { super(); this.courseID = courseID; this.courseName = courseName; } public String getCourseID() { return courseID; } public String getCourseName() { return courseName; } @Override public boolean equals(Object obj) { if(obj instanceof Course){ Course c=(Course)obj; return this.courseID.equals(c.courseID); } return false; } @Override public int hashCode() { return this.courseID.hashCode()^0x5ab989d0; } @Override public String toString() { return super.toString()+"[courseID="+this.courseID+",courseName="+this.courseName+"]"; } } package SchoolClass; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; public class SchoolClass { private HashSet<Student> hs = new HashSet<Student>(); public boolean addStudent(Student s) { if (hs.add(s)) { return true; } else { return false; } } public boolean removeStudent(String name) { Iterator<Student> it = hs.iterator(); while (it.hasNext()) { if (it.next().getStudentName().equals(name)) { it.remove(); return true; } } return false; } public Map<String, Integer> account() { Map<String, Integer> map = new HashMap<String, Integer>(); Iterator<Student> it_student = hs.iterator(); while (it_student.hasNext()) { Student s = it_student.next(); HashSet hs = s.getHs(); Iterator<Course> it_course = hs.iterator(); while (it_course.hasNext()) { Course c = it_course.next(); // System.out.println("================="+map.get(c.getCourseName())); if(map.get(c.getCourseName())==null){ map.put(c.getCourseName(), 1); }else{ map.put(c.getCourseName(), map.get(c.getCourseName()) + 1); } } } return map; } } package SchoolClass; import java.util.Iterator; import java.util.Map; import java.util.Set; public class SchoolClassTest { /** * @param args */ public static void main(String[] args) { Student s1=new Student("studentID_001","hello"); Student s2=new Student("studentID_002","haha"); Student s3=new Student("1001","hehe"); Course c1=new Course("courseID_01","aaa"); Course c2=new Course("courseID_02","bbb"); Course c3=new Course("courseID_03","ccc"); Course c4=new Course("courseID_04","dddd"); Course c5=new Course("02","ccc"); s1.addCourse(c1); s1.addCourse(c2); s1.addCourse(c3); s2.addCourse(c3); s2.addCourse(c4); s2.addCourse(c5); s3.addCourse(c1); s3.addCourse(c2); s3.addCourse(c3); s3.addCourse(c4); s3.addCourse(c5); SchoolClass sc=new SchoolClass(); sc.addStudent(s1); sc.addStudent(s2); sc.addStudent(s3); Map<String, Integer> map=sc.account(); Set<String> keyset=map.keySet(); Iterator<String> it=keyset.iterator(); while(it.hasNext()){ String temp=it.next(); System.out.println("course:"+temp+" , number:"+map.get(temp)); } } }

你可能感兴趣的:(Course_Student_SchoolClass(Day14))