Java(学生信息管理系统)(源代码)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JACK_JYH/article/details/52425188
package jack;

import java.util.*;

class Student{
    int id;
    String name;
    String gender;
    Date birth;
    public Student(int id,String name,String gender,Date birth){
        this.id=id;
        this.name=name;
        this.gender=gender;
        this.birth=birth;
    }
    public String toString()
    {
        return id+"-"+name+"-"+gender+"-"+birth;
    }
}
public class StuManager {
    int STU_NUM=10;
    TreeMap stuList=new TreeMap<>();
    void initStudents(){
        for(int i=0;i<STU_NUM;i++)
        {
            int id=i;
            String name="name"+(int)(Math.random()*1000);
            String gender="女";
            if(i%2==0)
            {
                gender="男";
            }
            Date birth=new Date(new Date().getTime()+(long )(Math.random()*Long.parseLong("1000000000001")));
            Student stu=new Student(id, name, gender, birth);
            stuList.put(id, stu);
        }
    }
    Student searchByID(int id){
        Student stu=(Student)stuList.get(id);
        return stu;
    }
    List sortStudentByDate(){
        List stus=new ArrayList();
        stus.addAll(stuList.values());
        for(int i=0;i<stus.size();i++)
        {
            for(int j=0;j<stus.size();j++)
            {
                Student tmp1=(Student)stus.get(i);
                Student tmp2=(Student)stus.get(j);
                if(tmp1.birth.before(tmp2.birth))
                {
                    stus.set(i,tmp2);
                    stus.set(j,tmp1);
                }
            }
        }
        return stus;        
    }
    public static void main(String args[])
    {
        StuManager manager=new StuManager();
        manager.initStudents();
        int id=(int)(Math.random()*10);
        System.out.println("检索学号:"+id);
        Student stu=manager.searchByID(id);
        if(stu==null)
        {
            System.out.println("未检索到学生的信息");
        }else
            System.out.println("检索到的学生信息是:"+stu);
        System.out.println("按照日期排序后的结果:");
        List l=manager.sortStudentByDate();
        for(Iterator iterator=l.iterator();iterator.hasNext();)
        {
            Student tmp=(Student)iterator.next();
            System.out.println(tmp);
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页