集合テーマ1

5878 ワード

集合テーマ:1、Courseクラスを定義し、カリキュラムを代表します.生徒を表すStudioクラスを定義し、Studioクラスに1つの属性が含まれるHashSetのオブジェクトを定義し、生徒が選択したすべてのコースを格納し、対応するaddCourse(Course c)メソッド2を提供します.クラスを表すSchoolClassを定義します.クラスに1つの属性がHashSetのオブジェクトであり、クラス内のすべてのStudioを格納するために使用されます.対応するaddStudent(Student s)メソッド3を提供し、マスターメソッドでSchoolClassオブジェクトを生成し、複数の学生を追加し、各学生に複数のカリキュラムを追加し、最後に各カリキュラムの選択人数を統計します.(ランダム)
Courseクラス:
package exercise3;

public class Course {
	private String cname;

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public Course(String cname) {
		this.cname = cname;
	}

	public boolean equals(Object o) {
		if (o instanceof Course) {
			Course c = (Course) o;
			if (c.cname.equals(this.cname)) {
				return true;
			}
		}
		return false;
	}

	public int hashCode() {
		return this.cname.hashCode();
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		return sb.append(cname).toString();
	}
}

Studioクラス:
package exercise3;

import java.util.HashSet;

public class Student {
	private String sname;
	private HashSet<Course> courses;

	//     
	public Student(String sname) {
		this.sname = sname;
		courses = new HashSet<Course>();
	}

	//     
	public boolean addCourse(Course c) {
		return courses.add(c);
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public HashSet<Course> getCourses() {
		return courses;
	}

	public void setCourses(HashSet<Course> courses) {
		this.courses = courses;
	}

	public boolean equals(Object o) {
		if (o instanceof Student) {
			Student s = (Student) o;
			if (s.sname.equals(this.sname)) {
				return true;
			}
		}
		return false;
	}

	public int hashCode() {
		return this.sname.hashCode();
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		return sb.append("Student [Sname=").append(sname).append(courses)
				.append("]").toString();
	}

}

 
 
SchoolClassクラス:
package exercise3;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class SchoolClass {
	private String name;
	private HashSet<Student> students;//         

	public SchoolClass(String name) {
		this.name = name;
		students = new HashSet<Student>();
	}

	public boolean addStudent(Student s) {
		return students.add(s);
	}

	public Map<Course, Integer> account() {//            ,key    ,value    。
		Map<Course, Integer> map = new HashMap<Course, Integer>();
		Iterator<Student> it = students.iterator();
		while (it.hasNext()) {//       
			Student stu = it.next();
			Iterator<Course> is = stu.getCourses().iterator();
			while (is.hasNext()) {//            
				Course c = is.next();
				map.put(c, map.get(c) == null ? 1 : map.get(c) + 1);
			}
		}
		return map;
	}

	public void printCount() {
		Set<Course> set = account().keySet();
		Iterator<Course> it = set.iterator();
		while (it.hasNext()) {
			Object key = it.next();
			System.out.println(key + "," + account().get(key));
		}
	}
}

 
Testテストクラス:
package exercise3;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Course[] course = { new Course("Java  "), new Course("  "),
				new Course("  "), new Course("   ") };

		SchoolClass class1 = new SchoolClass("Class1");
		for (int i = 0; i < 20; i++) {
			Student stu = new Student("Student-" + (i + 1));
			int num = (int) (Math.random() * course.length);
			for (int j = 0; j < num; j++) {
				stu.addCourse(course[(int) (Math.random() * course.length)]);
			}
			class1.addStudent(stu);
			System.out.println(stu);
		}
		System.out.println("==================================");

		class1.printCount();
	}
}

 
実行結果:
Student [Sname=Student-1[]]
Student [Sname=Student-2[]]
Student [Sname=Student-3[Java  ]]
Student [Sname=Student-4[   ]]
Student [Sname=Student-5[Java  ,   ,   ]]
Student [Sname=Student-6[  ,    ]]
Student [Sname=Student-7[]]
Student [Sname=Student-8[Java  ,   ]]
Student [Sname=Student-9[   ]]
Student [Sname=Student-10[  ]]
Student [Sname=Student-11[  ,   ]]
Student [Sname=Student-12[Java  ]]
Student [Sname=Student-13[]]
Student [Sname=Student-14[  ]]
Student [Sname=Student-15[   ,   ]]
Student [Sname=Student-16[  , Java  ,    ]]
Student [Sname=Student-17[]]
Student [Sname=Student-18[]]
Student [Sname=Student-19[  ]]
Student [Sname=Student-20[  ]]
==================================
   ,5
Java  ,5
  ,9
  ,3