Java期末作业--学生成绩管理系统

Java期末作业--学生管理成绩系统

一共只有两个文件

java文件Manager
这个是文件的主函数用于显示界面

// An highlighted block
package studetAdmin;

import java.util.ArrayList;
import java.util.Scanner;

public class Manager {
    public static Scanner scanner = new Scanner(System.in);
    public static ArrayList<Student> students = new ArrayList<>();




    public static void main(String[] args) {
        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("-------------【9】统计不及格人数------------");
            System.out.println("-------------【0】退出管理系统-------------");

            Student student = new Student();
            System.out.println("输入你的选择");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    student.addScore(students);
                    break;
                case "2":
                    student.saveMessage(students);
                    break;
                case "3":
                    student.messageRead(students);
                    break;
                case "4":
                    student.deleteStudent(students);
                    break;
                case "5":
                    student.sortStudent(students);
                    break;
                case "6":
                    student.lookupStudent(students);
                    break;
                case "7":
                    student.lookupStudentClass(students);
                    break;
                case "9":
                    student.lookupStudentClassPass(students);
                    break;
                case "0":
                    System.exit(0);
                default:
                    break;
            }
        }
    }
}

java文件Student
这是函数功能的实现

package studetAdmin;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Student {
    public static Scanner scanner = new Scanner(System.in);
    //    学号
    private long id;
    //    姓名
    private String name;
    //性别
    private String sex;
    //    班级
    private String grade;
    //    高数
    private float math;
    //    英语
    private float english;
    //    体育
    private float sport;
    //    Java
    private float java;
    //    C++
    private float cPlusPlus;
    //    总分
    private float sum;
    //    平均分
    private float average;
    public Student() {
    }
    public Student(long id, String name, String sex,String grade,float math, float english, float sport, float java, float cPlusPlus, float sum, float average) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.grade = grade;
        this.math = math;
        this.english = english;
        this.sport = sport;
        this.java = java;
        this.cPlusPlus = cPlusPlus;
        this.sum = sum;
        this.average = average;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    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 getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public float getMath() {
        return math;
    }
    public void setMath(float math) {
        this.math = math;
    }
    public float getEnglish() {
        return english;
    }
    public void setEnglish(float english) {
        this.english = english;
    }
    public float getSport() {
        return sport;
    }
    public void setSport(float sport) {
        this.sport = sport;
    }
    public float getJava() {
        return java;
    }
    public void setJava(float java) {
        this.java = java;
    }
    public float getcPlusPlus() {
        return cPlusPlus;
    }
    public void setcPlusPlus(float cPlusPlus) {
        this.cPlusPlus = cPlusPlus;
    }
    public float getSum(Student student) {

        return student.getMath()+student.getEnglish()+student.getSport()+student.getJava()+student.getcPlusPlus();
    }
    public void setSum(float sum) {
        this.sum = sum;
    }
    public void setAverage(float average) {
        this.average = average;
    }
    public float getAvg(Student student) {
        return (  student.getcPlusPlus() + student.getEnglish() + student.getSport() + student.getJava() +  student.getMath()) / 5;
    }


    //录入学生的信息
    public void addScore(ArrayList<Student> students) {
        System.out.println("----------录入学生数据----------");
        System.out.println("请依次输入如下数据:");
        long id;
        while (true) {
            System.out.println("学号");
            id = scanner.nextInt();
            if (isValid(students, id)) {
                System.out.println("学号重复,请重新输入");
            } else {
                break;
            }
        }
        System.out.println("姓名");
        String name = scanner.next();
        System.out.println("性别");
        String sex = scanner.next();
        System.out.println("年级");
        String grade = scanner.next();
        System.out.println("数学");
        float math = scanner.nextFloat();
        System.out.println("英语");
        float english = scanner.nextFloat();
        System.out.println("体育");
        float sport = scanner.nextFloat();
        System.out.println("Java");
        float java = scanner.nextFloat();
        System.out.println("C++");
        float cPlusPlus = scanner.nextFloat();
        //创建对象,并设置属性,然后将其加入学生对象集合
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setSex(sex);
        student.setGrade(grade);
        student.setMath(math);
        student.setEnglish(english);
        student.setcPlusPlus(cPlusPlus);
        student.setJava(java);
        student.setSport(sport);
        student.setSum((math+english+sport+java+cPlusPlus));
        student.setAverage((math+english+sport+java+cPlusPlus)/5);
        //        添加到集合
        students.add(student);
        //        提示信息
        System.out.println("添加成功");
    }
    //判断学号是否可用
    public boolean isValid(ArrayList<Student> students, long id) {
        for (Student student : students) {
            if (student.getId() == id) {
                return true;
            }
        }
        return false;
    }
    //将内存中的文件写入到csv文件中
    public void saveMessage(ArrayList<Student> students) {
        System.out.println("输入保存的文件名");
        String file = scanner.next();
        File f = new File(file+".csv");
        FileOutputStream fop = null;
    /*    int flag = -1;*/
        try {
            fop = new FileOutputStream(f, true);
            OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
            // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
            if (students.isEmpty()) {
                System.out.println("当前无数据,请先添加数据");
            } else {
                for (Student student : students) {
                    long id = student.getId();
                    String name = student.getName();
                    String sex = student.getSex();
                    String grade = student.getGrade();
                    float math = student.getMath();
                    float english = student.getEnglish();
                    float sport = student.getSport();
                    float java = student.getJava();
                    float cPlusPlus = student.getcPlusPlus();
                    float sum = student.getSum(student);
                    double average = student.getAvg(student);
/*                    float polity = student.getPolity();
                    float algorithm = student.getAlgorithm();*/

                    writer.append(id+","+name+","+sex+","+grade+","+math+","+english+","+sport+","+java+","+cPlusPlus+","+sum+","+average);
                    writer.append("\r\n");
                }
            }
            writer.close();
            // 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
            fop.close();
            // 关闭输出流,释放系统资源
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 构建FileOutputStream对象,文件不存在会自动新建


    }
    //读取csv文件中的输入放入内存中
    public void messageRead(ArrayList<Student> students) {
        System.out.println("输入读取的文件名");
        String file = scanner.next();
        File file1 = new File(file+".csv");
        if (!file1.exists()){
            System.out.println("文件不存在");
        }else {
            FileReader fr = null;
            BufferedReader br = null;
            try {
                fr = new FileReader(file1);
                br = new BufferedReader(fr);
                String content = null;

                while (true) {
                    content = br.readLine();
                    if (content == null) {
                        break;
                    }
                    String item[] = content.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分
                    Student student = new Student();
                    student.setId(Long.parseLong(item[0]));
                    student.setName(item[1]);
                    student.setSex(item[2]);
                    student.setGrade(item[3]);
                    student.setMath(Float.parseFloat(item[4]));
                    student.setEnglish(Float.parseFloat(item[5]));
                    student.setSport(Float.parseFloat(item[6]));
                    student.setJava(Float.parseFloat(item[7]));
                    student.setcPlusPlus(Float.parseFloat(item[8]));
                    student.setSum(Float.parseFloat(item[9]));
                    student.setAverage(Float.parseFloat(item[10]));
                    students.add(student);

                }
            } catch (FileNotFoundException var21) {
            } catch (IOException var22) {
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException var20) {
                        var20.printStackTrace();
                    }
                }

                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException var19) {
                        var19.printStackTrace();
                    }
                }

            }
        }

    }
    //删除学生信息
    public void deleteStudent(ArrayList<Student> students) {
        System.out.println("----------删除学生成绩----------");
        if (students.isEmpty()) {
            System.out.println("当前无数据,请添加后重试");
        } else {
            System.out.println("-------------【1】使用学号查找-------------");
            System.out.println("-------------【2】使用性名查找-------------");
            System.out.println("输入你的选择");
            String choice = scanner.next();
            switch (choice){
                case "1":
                    System.out.println("输入要删除学生成绩的学号");
                    long id = scanner.nextLong();

                    int index = -1;
                    for (int i = 0; i < students.size(); i++) {
                        Student student = students.get(i);
                        /*System.out.println(student);*/
                        if (student.getId() == id) {
                            index = i;
                            break;
                        }
                    }
                    if (index == -1) {
                        System.out.println("未找到对应学号学生信息,请确认后再删除!");
                    } else {
                        students.remove(index);
                        System.out.println("----------删除成功----------");
                    }
                    break;
                case "2":
                    System.out.println("输入要删除学生成绩的姓名");
                    String name = scanner.next();
                    /*System.out.println(name);*/
                    int index1 = -1;
                    for (int i = 0; i < students.size(); i++) {
                        Student student = students.get(i);
                        System.out.println(student.getName());
                        /*System.out.println(student);*/
                        if (name.equals(student.getName())) {
                            index1 = i;
                            break;
                        }
                    }
                    if (index1 == -1) {
                        System.out.println("未找到对应姓名学生信息,请确认后再删除!");
                    } else {
                        students.remove(index1);
                        System.out.println("----------删除成功----------");
                    }
                    break;
                default:
                    break;

                }

            }

    }
    //计算总分和平均分 并按总分降序排列
    public void sortStudent(ArrayList<Student> students) {
        if (students.isEmpty()) {
            System.out.println("当前无数据,请添加后重试");
        } else {
            for (int i = 0; i < students.size() - 1; i++) {
                if (students.get(i).getSum(students.get(i)) < students.get(i + 1).getSum(students.get(i + 1))) {
                    Student tmp = students.get(i);
                    students.set(i, students.get(i + 1));
                    students.set(i + 1, tmp);
                }
            }

            System.out.println("排序后的学生成绩");
            System.out.println("学号\t\t姓名\t\t性别\t\t班级\t\t总分\t\t平均分");
            for (Student student : students) {
                System.out.format("%d\t\t%s\t\t%s\t\t%s\t\t%.2f\t%.2f\n", student.getId(), student.getName(), student.getSex(), student.getGrade(), student.getSum(student) ,student.getAvg(student));
            }
        }
    }
    //利用学生的姓名,学号进行查找学生(可模糊查找)
    public void lookupStudent(ArrayList<Student> students) {
        int flag = -1;
        boolean isMatch=false;
        ArrayList<Integer> integerArrayList = new ArrayList<>();


        System.out.println("----------查找学生成绩----------");
        if (students.size() == 0) {
            System.out.println("当前无数据,请添加后重试");
        } else {
            System.out.println("-------------【1】使用学号查找-------------");
            System.out.println("-------------【2】使用姓名查找-------------");
            System.out.println("输入你的选择");
            String choice = scanner.next();
            switch (choice){
                case "1":
                    System.out.println("请输入要查找学生的学号:");
                    Long id = scanner.nextLong();
/*                    System.out.println("字符串中是否包含了 '数字' 字符串? " + isMatch);*/
                    Student student = new Student();

                    //        查找对应学号,找到就退出
                    for (int i = 0; i < students.size(); i++) {
                        student = students.get(i);
                        if (isMatch = Pattern.matches("\\d*"+String.valueOf(id)+"\\d*", String.valueOf(student.getId()))) {
                            flag = i;
                            integerArrayList.add(i);
                        }

                    }
                    if (flag == -1) {
                        System.out.println("未找到对应学号的学生,请确认后重新输入!");
                    } else {
                        System.out.println("对应学号的学生成绩如下:");
                        System.out.println("学号\t\t姓名\t\t性别\t\t班级\t\t数学\t\t英语\t\t体育\t\tJava\t\tC++");
                        for (Integer integer:integerArrayList){
                            System.out.format("%d\t%s\t\t%s\t\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students.get(integer).getId(), students.get(integer).getName(),
                                    students.get(integer).getSex(), students.get(integer).getGrade(), students.get(integer).getMath(), students.get(integer).getEnglish(),
                                    students.get(integer).getSport(),students.get(integer).getJava(), students.get(integer).getcPlusPlus());
                        }

                        System.out.println("查找信息成功!");
                    }

                    break;
                case "2":
                    System.out.println("输入要查找学生的姓名");
                    String name = scanner.next();
                    /*System.out.println(name);*/
                    Student student1 = new Student();
                    //        查找对应学号,找到就退出
                    for (int i = 0; i < students.size(); i++) {
                        student1 = students.get(i);
                        if (isMatch = Pattern.matches(".*"+name+".*", student1.getName())) {
                            flag = i;
                            integerArrayList.add(i);
                        }
                    }
                    if (flag == -1) {
                        System.out.println("未找到对应姓名的学生,请确认后重新输入!");
                    } else {
                        System.out.println("对应姓名的学生成绩如下:");
                        System.out.println("学号\t\t姓名\t\t性别\t\t班级\t\t数学\t\t英语\t\t体育\t\tJava\t\tC++");
                        for (Integer integer:integerArrayList){
                            System.out.format("%d\t%s\t\t%s\t\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students.get(integer).getId(), students.get(integer).getName(),
                                    students.get(integer).getSex(), students.get(integer).getGrade(), students.get(integer).getMath(), students.get(integer).getEnglish(),
                                    students.get(integer).getSport(),students.get(integer).getJava(), students.get(integer).getcPlusPlus());
                        } System.out.println("查找信息成功!");
                    }
                    break;
                default:
                    break;
            }




        }

    }
    //按班级信息查找学生
    public void lookupStudentClass(ArrayList<Student> students) {
        int flag = -1;
        ArrayList<Integer> integerArrayList = new ArrayList<>();
        System.out.println("----------按班级查找----------");
        if (students.size() == 0) {
            System.out.println("当前无数据,请添加后重试");
        } else {
            System.out.println("输入要查找学生的班级");
            String grade = scanner.next();
            Student student1 = new Student();
            //        查找对应班级,并记录
            for (int i = 0; i < students.size(); i++) {
                student1 = students.get(i);
                if (grade.equals(student1.getGrade())) {
                    flag = i;
                    integerArrayList.add(i);
                }
            }
            if (flag == -1) {
                System.out.println("未找到对应班级的的学生,请确认后重新输入!");
            } else {
                System.out.println("对应班级的学生成绩如下:");
                System.out.println("学号\t\t姓名\t\t性别\t\t数学\t\t英语\t\t体育\t\tJava\t\tC++");
                for (Integer integer:integerArrayList){
                    System.out.format("%d\t%s\t\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students.get(integer).getId(), students.get(integer).getName(),
                            students.get(integer).getSex(), students.get(integer).getMath(), students.get(integer).getEnglish(),
                            students.get(integer).getSport(),students.get(integer).getJava(), students.get(integer).getcPlusPlus());
                    }
                System.out.println("查找信息成功!");
            }

        }
    }
    //按班级查找学生,并统计不及格的人数
    public void lookupStudentClassPass(ArrayList<Student> students) {

        ArrayList<Integer> integerArrayList = new ArrayList<>();
        ArrayList<String> stringArrayList = new ArrayList<>();
        System.out.println("----------按班级查找不及格的人数----------");
        if (students.size() == 0) {
            System.out.println("当前无数据,请添加后重试");
        } else {
            int i=0,j=0;
            int flag = -1,stringList = -1,stu=-1;
            Student student1 = new Student();
            stringArrayList.add(students.get(0).getGrade());
            for (i=1; i < students.size(); i++) {
                student1 = students.get(i);
                String temp = student1.getGrade();
                for ( j = 0; j < stringArrayList.size(); j++) {
                    if (temp.equals(stringArrayList.get(j))){
                        stu++;

                    }
                }

                if (stu==-1 ){
                    stringArrayList.add(students.get(j).getGrade());
                } else {
                    stu=-1;
                    continue;
                }
            }
            for (int s=0; s < stringArrayList.size(); s++) {
                String temp = stringArrayList.get(0);
                for (int q = 0; q < students.size(); q++) {
                    student1 = students.get(q);
                    if (temp.equals(student1.getGrade()) ){
                        integerArrayList.add(q);
                    }
                }
                int cmath = 0, cenglish=0,csport=0,cjava=0,ccpp=0;
                System.out.println("班级\t\t数学不及格\t\t英语不及格\t\t体育不及格\t\tJava不及格\t\tC++不及格");
                for (Integer integer:integerArrayList){
                    if (students.get(integer).getMath()<60) cmath++;
                    if (students.get(integer).getEnglish()<60) cenglish++;
                    if (students.get(integer).getSport()<60) csport++;
                    if (students.get(integer).getJava()<60) cjava++;
                    if (students.get(integer).getcPlusPlus()<60) ccpp++;
                    }
                System.out.println(stringArrayList.get(s)+"\t\t\t\t"+cmath+"\t\t\t\t"+cenglish+"\t\t\t\t"+csport+"\t\t\t\t"+cjava+"\t\t\t\t"+ccpp+"\t\t\t\t");
                integerArrayList.clear();
            }
        }
    }
}

其中第8项没做,第六项模糊查找用的是正则表达式。进行操作务必将数据载入到内存中,只在内存中操作,不可以直接对文件操作。

你可能感兴趣的:(java)