编程语言
首页 > 编程语言> > 学生信息管理系统 Java

学生信息管理系统 Java

作者:互联网

在这里插入图片描述
Menu.java

package studentmanagesystem;

public class Menu {
    static void showFirst(){
        System.out.println("+--------------学生信息管理系统--------------+");
        System.out.println("|\t\t\t\t1.增加学员信息\t\t\t\t|");
        System.out.println("|\t\t\t\t2.查找学员信息\t\t\t\t|");
        System.out.println("|\t\t\t\t3.更新学员信息\t\t\t\t|");
        System.out.println("|\t\t\t\t4.删除学员信息\t\t\t\t|");
        System.out.println("|\t\t\t\t5.退出\t\t\t\t\t\t|");
        System.out.println("+-------------------------------------------+");
    }
    static void showSecond(){
        System.out.print("请选择操作项:(1)增加  (2)查找  (3)更新  (4)删除  (5)注销  :");
    }

}

Student.java

package studentmanagesystem;

public class Student {
    String stuNo;
    String name;
    double score;

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "学员信息:" +
                "学号:" + stuNo +
                "  姓名:" + name  +
                "  成绩:" + score +
                '\n';
    }
}

StudentManage.java

package studentmanagesystem;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
public class StudentManager {
    static Scanner input = new Scanner(System.in);

    //显示操作菜单
    void showMenu(){
        Menu.showFirst();
        Menu.showSecond();
    }

    //开始方法
    void start(){
        int choose;
        char answer;
        do {
            showMenu();

            choose = input.nextInt();
            switch (choose){
                case 1:{
                    addStudent();
                    break;
                }
                case 2:{
                    System.out.print("请输入学号:");
                    String stuNo = input.next();
                    Student student=findByNo(stuNo);
                    if(student.getStuNo()==null)
                        System.out.println("没有学号为"+stuNo+"的学生。");
                    else System.out.println(student);
                    break;
                }
                case 3:{
                    Student student = inputInfo();
                    updateStudent(student);
                    System.out.println("学生信息更新成功!");
                    break;
                }
                case 4:{
                    System.out.print("请输入学号:");
                    String stuNo = input.next();
                    if (removeStudent(stuNo)) {
                        System.out.println("删除成功!");
                    }
                    else System.out.println("删除失败!没有学号为"+stuNo+"的学生信息");
                    break;
                }

            }
            System.out.print("返回操作菜单(y/n):");
            answer=input.next().charAt(0);
        }while (answer!='n');

    }

    //添加学生对象到数组中
    void addStudent(){
        char answer;
        do {
            ExcelReader excelReader = ExcelUtil.getReader("E:\\蓝桥\\Java初级\\学生信息.xlsx");
            ExcelWriter excelWriter = ExcelUtil.getWriter("E:\\蓝桥\\Java初级\\学生信息.xlsx");
            Student student = inputInfo();
            excelWriter.writeCellValue(0, excelReader.getRowCount(),student.getStuNo());
            excelWriter.writeCellValue(1, excelReader.getRowCount(),student.getName());
            excelWriter.writeCellValue(2, excelReader.getRowCount(),student.getScore());
            excelWriter.flush();
            excelWriter.close();
            System.out.println("增加学员成功!");
            System.out.println(student);
            System.out.print("是否继续增加(y/n):");
            answer=input.next().charAt(0);
        }while (answer!='n');

    }

    //通过学号查找学员
    Student findByNo(String stuNo){
        ExcelReader excelReader=ExcelUtil.getReader("E:\\蓝桥\\Java初级\\学生信息.xlsx");
        List<List<Object>> students = excelReader.read();
        Student student1=new Student();
        for (List<Object> student : students) {
            if (stuNo.equals(student.get(0))) {
                student1.setStuNo((String)student.get(0));
                student1.setName((String)student.get(1));
                student1.setScore((double)student.get(2));
                break;
            }
        }
        return student1;
    }

    //输入学生信息,并返回学生对象
    Student inputInfo(){
        Student student=new Student();
        System.out.print("请输入学号:");
        student.stuNo = input.next();
        System.out.print("请输入学生姓名:");
        student.name = input.next();
        System.out.print("请输入成绩:");
        student.score = input.nextDouble();
        return student;
    }

    void updateStudent(Student student){
        ExcelWriter excelWriter= ExcelUtil.getWriter("E:\\蓝桥\\Java初级\\学生信息.xlsx");
        ExcelReader excelReader=ExcelUtil.getReader("E:\\蓝桥\\Java初级\\学生信息.xlsx");
        int rowCount = excelReader.getRowCount();
        for (int i = 1; i < rowCount; i++) {
            List<Object> row = excelReader.readRow(i);
            if (row.get(0).equals(student.getStuNo())) {
                excelWriter.writeCellValue(0, i,student.getStuNo());
                excelWriter.writeCellValue(1, i,student.getName());
                excelWriter.writeCellValue(2, i,student.getScore());
                excelWriter.flush();
                excelWriter.close();
                return;
            }
        }

    }

    boolean removeStudent(String stuNo){
        ExcelReader excelReader = ExcelUtil.getReader("E:\\蓝桥\\Java初级\\学生信息.xlsx");
        int rowCount = excelReader.getRowCount();
        int i;
        boolean isfind=false;
        for (i = 1; i < rowCount; i++) {
            List<Object> row = excelReader.readRow(i);
            if (row.get(0).equals(stuNo)) {
                isfind=true;
                break;
            }
        }
        if (!isfind) {
            return false;
        }
        for (int j = i+1; j < rowCount; j++) {
            ExcelWriter excelWriter = ExcelUtil.getWriter("E:\\蓝桥\\Java初级\\学生信息.xlsx");
            List<Object> row = excelReader.readRow(j);
            excelWriter.writeCellValue(0, j-1,row.get(0));
            excelWriter.writeCellValue(1, j-1,row.get(1));
            excelWriter.writeCellValue(2, j-1,row.get(2));
            excelWriter.flush();
            excelWriter.close();

        }
        ExcelReader excelReader1 = ExcelUtil.getReader("E:\\蓝桥\\Java初级\\学生信息.xlsx");
        Workbook workbook=excelReader1.getWorkbook();
        Sheet sheet=workbook.getSheetAt(0);
        Row row=sheet.getRow(rowCount-1);
        sheet.removeRow(row);
        try {
            workbook.write(new FileOutputStream("E:\\蓝桥\\Java初级\\学生信息1.xlsx"));
            workbook.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        return true;
    }


    public static void main(String[] args) {
        StudentManager studentManager=new StudentManager();
        studentManager.start();
    }
}

标签:stuNo,Java,System,学生,student,excelWriter,println,信息管理系统,out
来源: https://blog.csdn.net/shaoNianABin123/article/details/111769063