JavaSE-初级阶段项目-基于excel表格的学生管理系统
作者:互联网
1.需求分析
- 配置
项目文件
环境:IDEA IntelliJ
工具包 :hutool-poi-apidocs
工具包导入:
右击项目文件
open module settings
3.具体实现
(1)学生管理类StudentManager。java
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.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* ClassName StudentManager
* Description
*
* @author 阿木木
* @date 2020/12/26 10:26
* Version 1.0
*/
public class StudentManager {
static Scanner input = new Scanner(System.in);
public static Student[] studentArray = new Student[1000];
/**
*Description 显示主菜单
**/
public void showMenu(){
Menu.showOperation();
start();
}
/**
*Description 系统驱动类
**/
public void start(){
switch (input.nextInt()){
//增加学员信息
case 1:{
String flag;
Student student = new Student();
do {
if (addStudent(student)) {
System.out.println("增加学员成功");
//增加学员成功,显示成功加入的学员信息
for (Student student1 : studentArray) {
if (student1 == null) {
break;
}
System.out.println(student1);
}
} else {
System.out.println("增加学员失败");
}
System.out.println("是否继续增加:y/n?");
flag = input.next();
}while ("y".equals(flag));
excelInsert();
//执行完一次增加操作,清空student对象数组
for (int i = 0; i < studentArray.length; i++) {
if (studentArray[i] == null) {
break;
}
studentArray[i] = null;
}
break;
}
case 2:{
//查询学生信息
String flag;
do{
System.out.print("请输入学号:");
Student student = findByNo(input.next());
if (student.getStudentNo() == null) {
System.out.println("该学号不存在");
}else {
System.out.println(student);
}
System.out.println("是否继续查询:y/n?");
flag = input.next();
}while ("y".equals(flag));
break;
}
case 3:{
//更新学生信息
String flag;
Student student = new Student();
do{
System.out.println("请输入要更新的学生信息");
System.out.print("请输入学号:");
student.setStudentNo(input.next());
System.out.print("请输入学生姓名:");
student.setStudentName(input.next());
System.out.print("请输入成绩:");
student.setStudentScore(input.next());
updateStudent(student);
System.out.println("是否继续更新:y/n?");
flag = input.next();
}while ("y".equals(flag));
break;
}
case 4:{
System.out.println("请输入要删除的学号:");
removeStudent(input.next());
}
case 5:{
break;
}
default:{
break;
}
}
}
/**
*Description 信息写入excel表格
**/
void excelInsert(){
//信息写入excel表格
for (Student student1 : studentArray) {
if (student1 == null) {
break;
}
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
ExcelWriter excelWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
int rowCount = excelReader.getRowCount();
excelWriter.writeCellValue(0, rowCount , student1.getStudentName());
excelWriter.writeCellValue(1, rowCount , student1.getStudentNo());
excelWriter.writeCellValue(2, rowCount , student1.getStudentScore());
excelWriter.flush();
excelWriter.close();
}
}
/**
*Description 添加学生对象到数组中
**/
boolean addStudent(Student student){
student = inputInfo();
for (int i = 0; i < studentArray.length; i++) {
if (studentArray[i] == null) {
studentArray[i] = student;
return true;
}
}
return false;
}
/**
*Description 输入学生信息,并返回学生对象
**/
Student inputInfo(){
Student student = new Student();
System.out.print("请输入学号:");
student.setStudentNo(input.next());
System.out.print("请输入学生姓名:");
student.setStudentName(input.next());
System.out.print("请输入成绩:");
student.setStudentScore(input.next());
return student;
}
/**
*Description 按学号查找学员信息
**/
Student findByNo(String stuNo) {
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
Student student = new Student();
for (int i = 1; i < excelReader.getRowCount(); i++) {
List<Object> objects = excelReader.readRow(i);
if (objects == null) {
String studentNo = objects.get(1).toString();
if (stuNo.equals(studentNo)) {
student.setStudentName(objects.get(0).toString());
student.setStudentNo(studentNo);
student.setStudentScore(objects.get(2).toString());
}
}
}
return student;
}
/**
*Description 更新学生数据
**/
void updateStudent(Student student){
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
for (int i = 1; i < excelReader.getRowCount(); i++) {
List<Object> objects = excelReader.readRow(i);
String studentNo = objects.get(1).toString();
if (student.getStudentNo().equals(studentNo)) {
ExcelWriter excelWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
excelWriter.writeCellValue(0, i, student.getStudentName());
excelWriter.writeCellValue(1, i, student.getStudentNo());
excelWriter.writeCellValue(2, i, student.getStudentScore());
excelWriter.flush();
excelWriter.close();
System.out.println("更新成功");
}
}
}
/**
*Description 删除学生信息
**/
void removeStudent(String stuNo){
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
List<Object> objects = new ArrayList<>();
for (int i = 1; i < excelReader.getRowCount(); i++) {
objects = excelReader.readRow(i);
if (objects.get(1).equals(stuNo)) {
Workbook workbook = excelReader.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(i);
sheet.removeRow(row);
try {
workbook.write(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\学生成绩表2.xlsx"));
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(2)菜单类Menu.java
在这里插入代码片
package com.studentscoresystem;
/**
-
ClassName Menu
-
Description
-
@author 阿木木
-
@date 2020/12/26 10:37
-
Version 1.0
*/
public class Menu {/**
*Description 输出学生信息管理系统操作
**/
static void showOperation(){
System.out.println(“±--------------学生信息管理系统--------------------+”);
System.out.println("|\t\t1.增加学员信息\t\t\t");
System.out.println("|\t\t2.查找学员信息\t\t\t");
System.out.println("|\t\t3.更新学员信息\t\t\t");
System.out.println("|\t\t4.删除学员信息\t\t\t");
System.out.println("|\t\t5.退出 \t\t\t");
System.out.println(“±------------------------------------------------+”);
System.out.println(“请选择操作项:(1)增加 (2)查找 (3)更新 (4)删除 (5) 注销”);
}
}
(3)学生信息数据类StudeBeam.java
package com.studentscoresystem;
/**
* ClassName Student
* Description
*
* @author 阿木木
* @date 2020/12/26 10:49
* Version 1.0
*/
public class Student {
private String studentNo;
private String studentName;
private String studentScore;
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentScore() {
return studentScore;
}
public void setStudentScore(String studentScore) {
this.studentScore = studentScore;
}
@Override
public String toString() {
return "学生信息:" +
"学号" + studentNo + '\t' +
"姓名:" + studentName + '\t' +
"成绩:" + studentScore ;
}
}
(4)系统测试类Main.java
import java.util.Scanner;
/**
* ClassName Main
* Description 系统测试主类
*
* @author 阿木木
* @date 2020/12/26 12:54
* Version 1.0
*/
public class Main {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
String flag;
do {
studentManager.showMenu();
System.out.println("返回操作菜单:y/n?");
flag = input.next();
} while ("y".equals(flag));
}
}
4.测试截图
(1)增加
(2)减少
(3)更新
(4)删除
标签:初级阶段,String,excel,System,student,Student,println,JavaSE,out 来源: https://blog.csdn.net/qq_44364267/article/details/111770619