学生选课统计练习java

package com.java;

import java.util.HashMap;
import java.util.HashSet;

public class Test
{
	public static void main(String[] args)
	{
		Course english=new Course("英语");
		Course math=new Course("数学");
		Student lily=new Student("Lily");
		Student anan=new Student("Anan");
		
		lily.addCourse(english);
		lily.addCourse(math);
		
		anan.addCourse(english);
		
		//s2.removeCourse("英语");
		
		
		SchoolClass schoolClass=new SchoolClass();
		schoolClass.addStudent(lily);
		schoolClass.addStudent(anan);
		
		//schoolClass.removeStudent("Anan");
		
		
		HashMap<String, Integer> mapAccount = schoolClass.mapAccount();
		for(HashMap.Entry<String,Integer> e : mapAccount.entrySet())
		{
			System.out.println(e.getKey()+" : "+e.getValue());
		}
	}
}
class Course
{
	private String name;

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

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	
}

class Student
{
	private String name;
	private HashSet<Course> courseSet;
	public Student()
	{
		super();
		this.courseSet=new HashSet<Course>();
	}
	public Student(String name)
	{
		super();
		this.name = name;
		this.courseSet=new HashSet<Course>();
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public HashSet<Course> getCourseSet()
	{
		return courseSet;
	}
	public void setCourseSet(HashSet<Course> courseSet)
	{
		this.courseSet = courseSet;
	}
	
	public void  addCourse(Course c)
	{
		this.courseSet.add(c);
	}
	public void removeCourse(String name)
	{
		for(Course c:this.courseSet)
		{
			if(c.getName().equals(name))
				this.courseSet.remove(c);
		}
	}
	
	
	
}
class SchoolClass
{
	private HashSet<Student> studentSet;
	
	public SchoolClass()
	{
		super();
		studentSet=new HashSet<Student>();
	}

	public HashSet<Student> getStudentSet()
	{
		return studentSet;
	}

	public void setStudentSet(HashSet<Student> studentSet)
	{
		this.studentSet = studentSet;
	}
	
	public void addStudent(Student s)
	{
		studentSet.add(s);
	}
	public void removeStudent(String name)
	{
		for(Student s:this.studentSet)
		{
			if(s.getName().equals(name))
				this.studentSet.remove(s);
		}
	}
	
	public HashMap<String, Integer> mapAccount()
	{
		HashMap<String, Integer> resultMap=new HashMap<String,Integer>();
		for(Student s:this.studentSet)
		{
			HashSet<Course> courseSet = s.getCourseSet();
			for(Course c:courseSet)
			{
				Integer value = resultMap.get(c.getName());
				resultMap.put(c.getName(), value==null?1:++value);
			}
		}
		
		return resultMap;
	}
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值