分拣存储3-HashMap实现学生选课

为加深分拣存储概念,熟练代码,写了一个学生选课demo
思路和前面基本一致,一共设计了四个类,Course类,Student类,分别封装了关于课程和学生的数据,StuCourse类,属性有学生姓名和List〈Course〉,以及核心代码类Mytest。

有以下几点需要注意:
1. StuCourse类中需要在构造方法中初始化List,无论有没有形参,不然会出现空指针异常
2. StuCourse类提供一个只带name的构造方法
3. map是唯一的,不需要每一次选课都新new一个map,所以要把它放在方法外面,map就涵盖了所有的学生以及学生对应选择的所有课程,不过我们这里运行都是只有一个学生
4. 遍历list的时候可以用加强for语句,也可以有迭代器,我都是用的foreach
5. last but not least ,一定要搞清楚这里的StuCourse类存在的必要,不直接把List〈Course〉设置为Student类的属性是为了方便进行分拣存储,为Map添加泛型

代码运行:
根据提示创建一个学生对象,然后进行选课,每次选择一个,可以继续选择,或者选择退出,如图
分拣存储3-HashMap实现学生选课_第1张图片
分拣存储3-HashMap实现学生选课_第2张图片

源代码:

/** * 课程类 * @author wwyDEPP * */
public class Course {
    private String name;
    private String teacher;
    public Course() {
        // TODO Auto-generated constructor stub
    }
    public Course(String name, String teacher) {
        super();
        this.name = name;
        this.teacher = teacher;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTeacher() {
        return teacher;
    }
    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }

}
import java.util.ArrayList;
import java.util.List;
/** * 学生类 * @author wwyDEPP * */
public class Student {
    private String name;
    private String id;
    private int year;
    private String number;
    private String sex;

    public Student() {
        // TODO Auto-generated constructor stub
    }

    public Student(String name, String id, int year, String number, String sex) {
        super();
        this.name = name;
        this.id = id;
        this.year = year;
        this.number = number;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }   
}
import java.util.ArrayList;
import java.util.List;
/** * StuCourse类 * @author wwyDEPP * */
public class StuCourse {
    private String name;
    private List<Course> stuCourseList;

    public StuCourse() {
        //防止空指针异常
        stuCourseList=new ArrayList<Course>();
    }

    public StuCourse(String name) {
        super();
        this.name = name;
        stuCourseList=new ArrayList<Course>();

    }

    public StuCourse(String name, List<Course> stuCourseList) {
        super();
        this.name = name;
        this.stuCourseList = stuCourseList;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Course> getStuCourseList() {
        return stuCourseList;
    }
    public void setStuCourseList(List<Course> stuCourseList) {
        this.stuCourseList = stuCourseList;
    }
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/** * MyTest类 * @author wwyDEPP * */
public class MyTest {
    Scanner console=new Scanner(System.in);
    //课程list
    List<Course> courseList;
    //学生选课map
    Map<String,StuCourse> map=new HashMap<String,StuCourse>();

    //创建课程列表
    public List<Course> creatCourse(){
        courseList=new ArrayList<Course>();
        courseList.add(new Course("001","james"));
        courseList.add(new Course("002","tom"));
        courseList.add(new Course("003","depp"));
        System.out.println("name---teacher");
        for(Course c:courseList){
            System.out.println(c.getName()+"----"+c.getTeacher());
        }
        return courseList;
    }

    //遍历课程返回学生所选课程对象
    public Course foreachCourse(String courseName){
        for(Course c:courseList){
            if(c.getName().equals(courseName)){
                return c;
            }
        }
        return null;
    }

    //创建学生对象
    public Student creatStu(){
        Student stu=new Student();
        System.out.println("Please input your name:");
        stu.setName(console.next());
        System.out.println("Please input your id:");
        stu.setId(console.next());
        System.out.println("Please input your year:");
        stu.setYear(console.nextInt());
        System.out.println("Please input your number:");
        stu.setNumber(console.next());
        System.out.println("Please input your sex:");
        stu.setSex(console.next());
        return stu;
    }

    //学生选课
    public void chooseCourse(Student stu){
        String stuName=stu.getName();
        StuCourse sc =map.get(stuName);
        if(sc==null){
            sc=new StuCourse(stuName);
            map.put(stuName,sc);
        }
        creatCourse();
        System.out.println("Please choose one class from list and input class name:");
        //学生选择课程名字
        String className=console.next();
        Course c=foreachCourse(className);
        //判断如果所选课程不为空,则添加到stuCourse对象的List中
        if(c!=null){
            sc.getStuCourseList().add(c);
            System.out.println("Choose course success!");
        }
        System.out.println("Here is you courses");
        for(Course cou:sc.getStuCourseList()){
            System.out.println(cou.getName()+"---"+cou.getTeacher());
        }

        printStu(stu);
    }

    //判断是否继续选课
    public void printStu(Student stu){
        System.out.println("Do you want choose more class? yes/no");
        String temp=console.next();
        if(temp.equals("yes")){
            chooseCourse(stu);
        }
    }

    public static void main(String[] args) {
        MyTest test=new MyTest();
        Student stu=test.creatStu();
        test.chooseCourse(stu); 
    }
}

你可能感兴趣的:(HashMap,学生选课,分拣存储)