package com.tinyivc.jdk.util.collection.chooseCourse;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.TreeSet;

public class ChooseCourse {
  public static void main(String[] args) {
    SchoolClass c = new SchoolClass();
    //		创建学生对象
    Student s1 = new Student("zhangsan", 30),
        s2 = new Student("lisi", 30),
        s3 = new Student("wangwu", 30),
        s4 = new Student("zhaoliu", 30),
        s5 = new Student("tianqi", 30);
    //		创建课程对象
    Course c1 = new Course("java"),
        c2 = new Course("mysql"),
        c3 = new Course("jsp"),
        c4 = new Course("servlet"),
        c5 = new Course("spring");
    s1.addCourse(c1);
    s1.addCourse(c2);
    s1.addCourse(c5);
    s1.addCourse(c3);

    s2.addCourse(c2);
    s2.addCourse(c4);
    s2.addCourse(c3);

    s3.addCourse(c1);
    s3.addCourse(c4);
    s3.addCourse(c3);

    s4.addCourse(c1);
    s4.addCourse(c3);
    s4.addCourse(c4);
    s4.addCourse(c2);

    s5.addCourse(c3);
    s5.addCourse(c1);
    c.addStudent(s1);
    c.addStudent(s2);
    c.addStudent(s3);
    c.addStudent(s4);
    c.addStudent(s5);
    //		c.addStudent(s5);
    System.out.println(c.getSs().size());
    HashMap<Course, Integer> ci = new HashMap<Course, Integer>();
    for (Student stu : c.getSs()) {
      for (Course key : stu.getCs()) {
        Integer value = ci.get(key);
        if (value == null) {
          ci.put(key, 1);
        } else {
          ci.put(key, value + 1);
        }
      }
    }
    //		高效输出
    for (Entry<Course, Integer> row : ci.entrySet()) {
      System.out.printf("%-8s被选次数%d\n", row.getKey().getName(), row.getValue());
    }
    //		自定义TreeSet
    TreeSet<ChooseCourseEntry> tsentry =
        new TreeSet<ChooseCourseEntry>(new Comparator<ChooseCourseEntry>() {
          @Override
          public int compare(ChooseCourseEntry o1, ChooseCourseEntry o2) {
            return o1.compareTo(o2);
          }
        });
    for (Entry<Course, Integer> en : ci.entrySet()) {
      tsentry.add(new ChooseCourseEntry(en.getKey(), en.getValue()));
    }
    for (ChooseCourseEntry cc : tsentry) {
      System.out.println(cc);
    }
  }
}
