java学生成绩管理系统(集合与泛型、文件读写)

目录

1学生成绩管理系统部分

2student类

3课程类:存储学生的课程名和成绩

4StudentAvgComparator尺子:实现以学生的平均成绩进行排序 


1学生成绩管理系统部分

将学生成绩管理系统定义为一个static变量,实现持久化

使用了ObjectInputStream(.writeObject将对象写入文件)和ObjectOutput(.readObject将文件中的对象读入流),这就要求流中的对象必须是可序列化的,即实现了Serializable接口

package StudentManagerSystem;

import java.io.*;
import java.util.*;

public class Ex8 {
    private static ArrayList<Student> studentList = new ArrayList<Student>();//静态studentList集合存储学生信息

    public static void main(String[] args) throws IOException {

        readStudentFromFile();//先从文件中读入原来保存过的学生数据
        Scanner temp = new Scanner(System.in);

        while (true) {
            System.out.println("请选择操作");
            System.out.println("1 查看所有学生");
            System.out.println("2 添加学生信息");
            System.out.println("3 修改学生信息");
            System.out.println("4 删除学生信息");
            System.out.println("5 退出系统");
            System.out.println("6 根据学生的学号查找学生信息");
            System.out.println("7 根据学生的平均分降序排序");
            System.out.println("8 按照学生的学号升序");
            System.out.println("9 根据学生的姓名查找学生信息");
            System.out.println("10 查看所有学生的总分和平均分");
            //输入选项
            String choice = temp.nextLine();
            //判断
            switch (choice) {
                case "1":
                    show(studentList);
                    System.out.println("系统中共存有"+ studentList.size()+"个学生");
                    break;
                case "2":
                    add(studentList);
                    break;
                case "3":
//                    modify(array);
                    break;
                case "4":
                    delete(studentList);
                    writeStudentToFile(studentList);//修改数据之后立即更新硬盘文件
                    break;
                case "6":
                    findStudentBystudentID();
                    break;
                case "7":
                    orderByAvgScore();
                    break;
                case "8":
                    orderByNum();
                    break;
                case "9":
                    findStudentBystudentName();
                    break;
                case "10":
                    showStudentAvgAndTotalScore();
                    break;
                case "5":
                    System.out.println("系统已成功退出");
                    System.exit(0);
                default:
                    System.out.println("您的输入有误,请重新选择!");
            }
        }
    }

    //查询学生信息方法
    private static void show(ArrayList<Student> array){
        System.out.println("共储存了"+array.size()+"名学生信息");
        if(array.size() == 0){
            System.out.println("暂时未存储任何学生信息,请重新选择。");
            return;
        }
        for(int i=0;i<array.size();i++){
            Student s = array.get(i);
            System.out.println("第"+i+"同学的学号为"+s.getNum()+"第"+i+"同学的姓名为"+s.getName()+"第"+i+"同学的性别为"+s.getSex()+"第"+i+"同学的班级为"+s.getsClass());
            s.getCourses();
        }
    }

    //添加学生信息方法
    private static void add(ArrayList<Student> array){
        Scanner temp = new Scanner(System.in);
        String num;
        while(true){
            System.out.println("请输入新同学学号:");
            num = temp.nextLine();
            //判断是否重复学号
            boolean flag = false;//默认未重复
            for(int i=0;i<array.size();i++){
                Student s = array.get(i);
                if(s.getNum().equals(num)){
                    System.out.println("该学号已经存在,请重新输入!");
                    flag = true;
                    break;
                }
            }
            if(!flag){
                break;
            }
        }
        System.out.println("请输入新同学姓名:");
        String name = temp.nextLine();
        System.out.println("请输入新同学年龄:");
        String age = temp.nextLine();
        System.out.println("请输入新同学所在班级:");
        String city = temp.nextLine();
        Student s = new Student();
        s.setNum(num);
        s.setName(name);
        s.setSex(age);
        s.setsClass(city);
        s.setCourses();
        array.add(s);
        System.out.println("添加学生信息成功!");

        writeStudentToFile(array);

    }

    //删除学生信息方法
    private static void delete(ArrayList<Student> array){
        Scanner temp = new Scanner(System.in);
        System.out.println("请输入需要删除的学生学号:");
        String num = temp.nextLine();
        for(int i=0;i<array.size();i++){
            Student s = array.get(i);
            if(s.getNum().equals(num)){
                System.out.println("请确认删除信息(Y/N):");
                System.out.println("第"+i+"同学的学号为"+s.getNum()+"第"+i+"同学的姓名为"+s.getName()+"第"+i+"同学的性别为"+s.getSex()+"第"+i+"同学的班级为"+s.getsClass());
                s.getCourses();
                String confirm = temp.nextLine();
                //判断确认删除
                if(confirm.equals("y") || confirm.equals("Y")){
                    if(array.remove(s)){
                        System.out.println("删除成功!");
                        return;
                    }
                }else{
                    System.out.println("已取消删除!");
                    return;
                }
            }
        }
        System.out.println("未找到所对应学生学号,请确认学号信息是否有误!");
    }
    private static void writeStudentToFile(ArrayList array){
        File file= new File("d:\\myjava\\workers.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        ObjectOutputStream oOut=null;
        try {
            oOut=new ObjectOutputStream(new FileOutputStream(file));
            oOut.writeObject(array);//将add到array的student对象写入
            oOut.close();
            System.out.println("写入成功");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static void readStudentFromFile(){
        File file= new File("d:\\myjava\\workers.txt");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("初次启动初始化完成!");
        }
        ObjectInputStream in=null;

        try {
            in=new ObjectInputStream(new FileInputStream("d:\\myjava\\workers.txt"));
            studentList=(ArrayList<Student>)in.readObject();//读入的是Object类型,需要强制类型转换
//            Object object=in.readObject();
            if (studentList.size()==0) {
                System.out.println("未读取到数据");
            }
            else {
                studentList =(ArrayList<Student>)studentList;
            }
            System.out.println("从指定文件读取学生数据完毕");
        } catch (EOFException e){
            System.out.println("读入完毕,没有数据可读了");;
        } catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void findStudentBystudentID() {
        Scanner temp = new Scanner(System.in);
        System.out.println("请输入需要查找的学生学号:");
        String num = temp.nextLine();
        for (int i = 0; i < studentList.size(); i++) {
            Student s = studentList.get(i);
            if (s.getNum().equals(num)) {
                System.out.println("第"+i+"同学的学号为"+s.getNum()+"第"+i+"同学的姓名为"+s.getName()+"第"+i+"同学的性别为"+s.getSex()+"第"+i+"同学的班级为"+s.getsClass());
                s.getCourses();
            }
        }
    }

    public static void findStudentBystudentName() {
        Scanner temp = new Scanner(System.in);
        System.out.println("请输入需要查找的学生学号:");
        String name = temp.nextLine();
        for (int i = 0; i < studentList.size(); i++) {
            Student s = studentList.get(i);
            if (s.getName().equals(name)) {
                System.out.println("第"+i+"同学的学号为"+s.getNum()+"第"+i+"同学的姓名为"+s.getName()+"第"+i+"同学的性别为"+s.getSex()+"第"+i+"同学的班级为"+s.getsClass());
                s.getCourses();
            }
        }
    }


    public static void orderByAvgScore () {
        Comparator<Student> avgComparator = new StudentAvgComparator();
        Collections.sort(studentList,avgComparator);
        System.out.println(studentList.toString());
    }
    public static void orderByNum(){
        Collections.sort(studentList);//按照学号升序排序
        System.out.println(studentList.toString());
    }

    public static void showStudentAvgAndTotalScore(){
        for (int i=0;i<studentList.size();i++){
            System.out.println("第"+i+"学生的总成绩为"+studentList.get(i).getTotalScore());
            System.out.println("第"+i+"学生的平均分为"+studentList.get(i).getAvg());
        }
    }
}

2student类

package StudentManagerSystem;

import java.io.Serializable;
import java.util.*;

public class Student implements Comparable<Student>,Serializable {
    private String num;//学号
    private String name;//姓名
    private String sex;//性别
    private String sClass;//班级
    private double Avg;//平均分
    private List<Course> course =new ArrayList<Course>();//成绩
//    private List<Course> listCourse=new ArrayList<Course>();

    public Student() {
    }

    public void setCourses() {
        ArrayList<Course> c1=new ArrayList<Course>();
        Scanner sc = new Scanner(System.in);
        Scanner sc1 =new Scanner(System.in);
        System.out.println("请输入课程的数量:");
        int num = sc.nextInt();
        for (int i = 0;i<num;i++){
            System.out.println("请输入第"+i+"个课程的名称和成绩:");
            String cname=sc.next();
            double cscore=sc1.nextDouble();
            c1.add(i,new Course(cname,cscore));
        }
        this.course=c1;//方法中实例化的course类传给student的course属性

    }

    public double getAvg(){
        return getTotalScore()/(int)(course.size());
    }

    public double getTotalScore(){
        double sum=0;
        for (Course course:course){
            sum=sum+course.getCourseSorce();
        }
        return sum;
    }

    public void getCourses(){
        System.out.println("以下是"+this.num+"号学生"+this.name+"的课程:");
        List<Course> list=new ArrayList<Course>();
        list=this.course;
//        for (Object num:list){
//            System.out.println(num);
//        }
        for (int i=0;i<list.size();i++){
            System.out.println("课程名为:"+list.get(i).getCourseName()+"课程成绩为:"+list.get(i).getCourseSorce());
        }
    }


    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getsClass() {
        return sClass;
    }

    public void setsClass(String sClass) {
        this.sClass = sClass;
    }

    public int compareTo(Student o) {
        return this.num.compareTo(o.num);
    }



    @Override
    public String toString() {
        return "Student{" +
                "num='" + num + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", sClass='" + sClass + '\'' +
                ", course=" + course +
                ", 平均成绩为" + this.getAvg() +
                '}';
    }
}

3课程类:存储学生的课程名和成绩

package StudentManagerSystem;

import java.io.Serializable;

public class Course implements Serializable {
    public String courseName;//课程姓名
    public double courseSorce;//课程成绩

    public Course(String courseName, double courseSorce) {
        this.courseName = courseName;
        this.courseSorce = courseSorce;
    }

    public Course() {
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public double getCourseSorce() {
        return courseSorce;
    }

    public void setCourseSorce(double courseSorce) {
        this.courseSorce = courseSorce;
    }

    @Override
    public String toString() {
        return "Course{" +
                "courseName='" + courseName + '\'' +
                ", courseSorce=" + courseSorce +
                '}';
    }
}

4StudentAvgComparator尺子:实现以学生的平均成绩进行排序 

package StudentManagerSystem;


import java.util.Comparator;

public class StudentAvgComparator implements Comparator<Student> {
    public int compare(Student o1,Student o2){
        return (int) (o2.getAvg()-o1.getAvg());
    }

}

  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值