commit 049ee30434981f7eaec7149b8758a8940aa55792
Author: zyx <1029606625@qq.com>
Date: Fri Nov 4 20:03:19 2022 +0800
2022/11/04 20:00 上传第9次作业题1
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..35410ca
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..deb9b5b
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..aa00ffa
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..abb532a
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..132404b
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..8f6e8b3
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,40 @@
+
+
+ 4.0.0
+
+ cn.czyx007.week10
+ week10-work1
+ 1.0-SNAPSHOT
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+ net.sourceforge.jexcelapi
+ jxl
+ 2.6.12
+
+
+ com.google.code.gson
+ gson
+ 2.10
+
+
+ org.projectlombok
+ lombok
+ 1.18.20
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
+
+
\ No newline at end of file
diff --git a/src/main/java/cn/czyx007/week10/Driver.java b/src/main/java/cn/czyx007/week10/Driver.java
new file mode 100644
index 0000000..bec1bb1
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/Driver.java
@@ -0,0 +1,38 @@
+package cn.czyx007.week10;
+
+import cn.czyx007.week10.bean.User;
+import cn.czyx007.week10.service.MainMenu;
+import cn.czyx007.week10.service.UserLogin;
+
+import java.util.Scanner;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class Driver {
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+
+ int cnt = 0;
+ User user = null;
+ while (cnt < 3) {
+ System.out.print("请输入用户名:");
+ String username = scanner.nextLine();
+ System.out.print("请输入密码:");
+ String password = scanner.nextLine();
+ user = new User(username, password);
+ if (UserLogin.login(user)) {
+ break;
+ } else {
+ System.out.println("您输入的用户名或密码不正确");
+ }
+ cnt++;
+ }
+ if (cnt == 3){
+ System.out.println("尝试超限!程序已退出");
+ return;
+ }
+ MainMenu.menu(user);
+ }
+}
diff --git a/src/main/java/cn/czyx007/week10/bean/Student.java b/src/main/java/cn/czyx007/week10/bean/Student.java
new file mode 100644
index 0000000..9caedbe
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/bean/Student.java
@@ -0,0 +1,39 @@
+package cn.czyx007.week10.bean;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Data
+public class Student {
+ /**
+ * 学号
+ */
+ private String id;
+
+ /**
+ * 姓名
+ */
+ private String name;
+
+ /**
+ * 性别
+ */
+ private String gender;
+
+ /**
+ * 课程名称
+ */
+ private String lessonName;
+
+ /**
+ * 成绩
+ */
+ private Double score;
+}
diff --git a/src/main/java/cn/czyx007/week10/bean/User.java b/src/main/java/cn/czyx007/week10/bean/User.java
new file mode 100644
index 0000000..163fdab
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/bean/User.java
@@ -0,0 +1,24 @@
+package cn.czyx007.week10.bean;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Data
+public class User {
+ /**
+ * 用户名
+ */
+ private String username;
+
+ /**
+ * 密码
+ */
+ private String password;
+}
diff --git a/src/main/java/cn/czyx007/week10/service/MainMenu.java b/src/main/java/cn/czyx007/week10/service/MainMenu.java
new file mode 100644
index 0000000..c4ad68b
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/service/MainMenu.java
@@ -0,0 +1,110 @@
+package cn.czyx007.week10.service;
+
+import cn.czyx007.week10.bean.Student;
+import cn.czyx007.week10.bean.User;
+import cn.czyx007.week10.utils.ExcelUtil;
+import cn.czyx007.week10.utils.JsonUtil;
+import cn.czyx007.week10.utils.StudentUtil;
+import cn.czyx007.week10.utils.TxtUtil;
+
+import java.util.List;
+import java.util.Scanner;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class MainMenu {
+ /**
+ * 主菜单界面和业务逻辑控制
+ * @param user 已成功登录的用户
+ */
+ public static void menu(User user){
+ int opt;
+ List students = null;
+ Scanner scanner = new Scanner(System.in);
+ while (true){
+ System.out.println("=====学生成绩管理系统=======\n" +
+ "1.从excel文件中加载数据\n" +
+ "2.从文本文件中加载数据\n" +
+ "3.从json文件中加载数据\n" +
+ "4.键盘输入数据\n" +
+ "5.成绩查询\n" +
+ "6.输出到excel文件\n" +
+ "7.输出到纯文本文件\n" +
+ "8.输出到json文件\n" +
+ "9.修改密码\n" +
+ "10.退出\n" +
+ "请输入选项:>>>");
+ do {
+ opt = checkInput(1, 10);
+ } while (opt == -1);
+
+ switch (opt){
+ case 1://1.从excel文件中加载数据
+ students = ExcelUtil.loadDataFromExcel();
+ break;
+ case 2://2.从文本文件中加载数据
+ students = TxtUtil.loadDataFromTxt();
+ break;
+ case 3://3.从json文件中加载数据
+ students = JsonUtil.loadDataFromJson();
+ break;
+ case 4://4.键盘输入数据
+ students = StudentUtil.inputDataFromKeyBoard();
+ break;
+ case 5://5.成绩查询
+ System.out.println("请输入待查询学生的学号:");
+ String id = new Scanner(System.in).nextLine();
+ StudentUtil.selectAllScoreById(id, students);
+ break;
+ case 6://6.输出到excel文件
+ ExcelUtil.exportDataToExcel(students);
+ break;
+ case 7://7.输出到纯文本文件
+ TxtUtil.exportDataToTxt(students);
+ break;
+ case 8://8.输出到json文件
+ JsonUtil.exportDataToJson(students);
+ break;
+ case 9://9.修改密码
+ UserLogin.changePassword(user);
+ break;
+ case 10://10.退出
+ while (true) {
+ System.out.println("确认退出系统吗?(Y/N):");
+ String option = scanner.nextLine();
+ if ("Y".equalsIgnoreCase(option)) {
+ System.exit(0);
+ } else if ("N".equalsIgnoreCase(option)) {
+ break;
+ } else {
+ System.out.println("输入错误!请重新输入!");
+ }
+ }
+ }
+ System.out.println();
+ }
+ }
+
+ /**
+ * 检查菜单选项输入合法性
+ * @param low 选项下界
+ * @param high 选项上界
+ * @return 合法选项值
+ */
+ public static int checkInput(int low, int high) {
+ Scanner scanner = new Scanner(System.in);
+ if (scanner.hasNextInt()) {
+ int opt = scanner.nextInt();
+ if (opt >= low && opt <= high) {
+ return opt;
+ }
+ System.out.println("输入超限,请输入" + low + "-" + high + "的数字");
+ } else {
+ System.out.println("输入数据类型错误,请输入" + low + "-" + high + "的数字");
+ scanner.next();
+ }
+ return -1;
+ }
+}
diff --git a/src/main/java/cn/czyx007/week10/service/UserLogin.java b/src/main/java/cn/czyx007/week10/service/UserLogin.java
new file mode 100644
index 0000000..ccb45e4
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/service/UserLogin.java
@@ -0,0 +1,104 @@
+package cn.czyx007.week10.service;
+
+import cn.czyx007.week10.bean.User;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class UserLogin {
+ /**
+ * 登录
+ * @param user 待登录的User对象,属性为输入的用户名和密码
+ * @return true:登录成功 false: 登录失败
+ */
+ public static boolean login(User user){
+ try(BufferedReader br = new BufferedReader(new FileReader("src/main/resources/users.csv"))) {
+ String aLine;
+ while ((aLine=br.readLine()) != null){
+ String[] nameAndPwd = aLine.split(" ");
+ if(nameAndPwd[0].equals(user.getUsername()) && nameAndPwd[1].equals(user.getPassword())){
+ return true;
+ }
+ }
+ } catch (Exception e){
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ /**
+ * 验证输入的 原密码 是否正确
+ * @param user 待验证密码的User对象
+ * @param oldPassword 输入的原密码
+ * @return true:输入的原密码正确 false:输入的原密码错误
+ */
+ public static boolean checkPassword(User user, String oldPassword){
+ return user.getPassword().equals(oldPassword);
+ }
+
+ /**
+ * 修改密码并写回到users.csv文件中
+ * @param user 待修改密码的User对象
+ */
+ public static void changePassword(User user){
+ Scanner scanner = new Scanner(System.in);
+ System.out.println("请输入原密码:");
+ String oldPassword = scanner.nextLine();
+ if (!UserLogin.checkPassword(user, oldPassword)) {
+ System.out.println("密码输入错误!");
+ } else {
+ System.out.println("请输入新密码:");
+ String newPassword = scanner.nextLine();
+ user.setPassword(newPassword);
+
+ updateUserDataFile(user);
+ System.out.println("密码已修改!");
+ }
+ try {
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 更新用户数据文件users.csv中的某用户的密码
+ * @param user 待更新密码的User对象
+ */
+ public static void updateUserDataFile(User user){
+ //读取原有用户数据
+ List users = new ArrayList<>();
+ try(BufferedReader br = new BufferedReader(new FileReader("src/main/resources/users.csv"))) {
+ String aLine;
+ while ((aLine= br.readLine()) != null){
+ String[] nameAndPwd = aLine.split(" ");
+ users.add(new User(nameAndPwd[0], nameAndPwd[1]));
+ }
+ } catch (Exception e){
+ e.printStackTrace();
+ }
+
+ //修改指定用户密码
+ for (User usr : users) {
+ if(usr.getUsername().equals(user.getUsername())){
+ usr.setPassword(user.getPassword());
+ }
+ }
+
+ //将修改后的数据写回到文件中
+ try(BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/users.csv"))) {
+ for (User usr : users) {
+ bw.write(usr.getUsername() + " " + usr.getPassword() + "\n");
+ }
+ } catch (Exception e){
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/main/java/cn/czyx007/week10/utils/ExcelUtil.java b/src/main/java/cn/czyx007/week10/utils/ExcelUtil.java
new file mode 100644
index 0000000..e802251
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/utils/ExcelUtil.java
@@ -0,0 +1,125 @@
+package cn.czyx007.week10.utils;
+
+import cn.czyx007.week10.bean.Student;
+import jxl.Sheet;
+import jxl.Workbook;
+import jxl.write.Label;
+import jxl.write.WritableSheet;
+import jxl.write.WritableWorkbook;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class ExcelUtil {
+ /**
+ * 从excel文件中加载数据
+ * @return 所有不重复的学生数据的List集合
+ */
+ public static List loadDataFromExcel() {
+ List students = new ArrayList<>();
+
+ Map> studentMap = new HashMap<>();
+ Workbook wb = null;
+ try {
+ wb = Workbook.getWorkbook(new File("src/main/resources/student.xls"));
+ Sheet sheet = wb.getSheet(0);
+
+ int cnt = 0;
+ int rows = sheet.getRows();
+ for (int i = 1; i < rows; i++) {
+ String id = sheet.getCell(0,i).getContents();
+ String name = sheet.getCell(1,i).getContents();
+ String gender = sheet.getCell(2,i).getContents();
+ String lessonName = sheet.getCell(3,i).getContents();
+ Double score = Double.parseDouble(sheet.getCell(4,i).getContents());
+
+ if(StudentUtil.checkIfStudentScoreDuplicate(studentMap, id, lessonName)){
+ students.add(new Student(id, name, gender, lessonName, score));
+ cnt++;
+ }
+ }
+
+ System.out.println("成功导入 " + cnt + " 条学生数据");
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (Exception e) {
+ System.out.println("数据导入失败!");
+ e.printStackTrace();
+ }finally {
+ if (wb != null){
+ try {
+ wb.close();
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ }
+ }
+ return students;
+ }
+
+ /**
+ * 导出学生成绩信息到excel文件中
+ * @param students 待导出成绩信息的学生数据的List集合
+ */
+ public static void exportDataToExcel(List students) {
+ WritableWorkbook wb = null;
+ try {
+ wb = Workbook.createWorkbook(new File("src/main/resources/score.xls"));
+ WritableSheet ws = wb.createSheet("学生成绩", 0);
+
+ ws.addCell(new Label(0,0,"学号"));
+ ws.addCell(new Label(1,0,"姓名"));
+ ws.addCell(new Label(2,0,"性别"));
+ ws.addCell(new Label(3,0,"总分"));
+ ws.addCell(new Label(4,0,"平均分"));
+
+ int studentCnt = 0;
+ Map isWritten = new HashMap<>();
+ for (int i = 0; i < students.size(); i++) {
+ if(isWritten.containsKey(students.get(i).getId())){
+ continue;
+ }
+
+ ws.addCell(new Label(0,studentCnt+1,students.get(i).getId()));
+ ws.addCell(new Label(1,studentCnt+1,students.get(i).getName()));
+ ws.addCell(new Label(2,studentCnt+1,students.get(i).getGender()));
+
+ int lessonCnt=0;
+ double allScore=0;
+ for (Student stu : students) {
+ if(stu.getId().equals(students.get(i).getId())){
+ lessonCnt++;
+ allScore += stu.getScore();
+ }
+ }
+ ws.addCell(new Label(3, studentCnt+1, Double.toString(allScore)));
+ ws.addCell(new Label(4, studentCnt+1, Double.toString(allScore/lessonCnt)));
+ studentCnt++;
+
+ isWritten.put(students.get(i).getId(), true);
+ }
+ wb.write();
+ System.out.println("成功导出 " + studentCnt + " 条学生成绩信息到 excel 文件中");
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (Exception e) {
+ System.out.println("数据导出失败!");
+ e.printStackTrace();
+ } finally {
+ if (wb != null) {
+ try {
+ wb.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/java/cn/czyx007/week10/utils/JsonUtil.java b/src/main/java/cn/czyx007/week10/utils/JsonUtil.java
new file mode 100644
index 0000000..de3991b
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/utils/JsonUtil.java
@@ -0,0 +1,101 @@
+package cn.czyx007.week10.utils;
+
+import cn.czyx007.week10.bean.Student;
+import com.google.gson.*;
+import com.google.gson.reflect.TypeToken;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class JsonUtil {
+ /**
+ * 从json文件中加载数据
+ * @return 所有不重复的学生数据的List集合
+ */
+ public static List loadDataFromJson() {
+ List students = null;
+ try (BufferedReader br = new BufferedReader(new FileReader("src/main/resources/student.json"))) {
+ StringBuilder jsonStr = new StringBuilder();
+ String aLine;
+ while ((aLine=br.readLine()) != null){
+ jsonStr.append(aLine);
+ }
+
+ students = new GsonBuilder().create().fromJson(jsonStr.toString(), new TypeToken>(){}.getType());
+
+ Map> studentMap = new HashMap<>();
+ for (int i = 0; i < students.size(); i++) {
+ String id = students.get(i).getId();
+ String lessonName = students.get(i).getLessonName();
+
+ if (!StudentUtil.checkIfStudentScoreDuplicate(studentMap, id, lessonName)){
+ students.remove(i--);
+ }
+ }
+
+ System.out.println("成功导入 " + students.size() + " 条学生数据");
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (Exception e) {
+ System.out.println("数据导入失败!");
+ e.printStackTrace();
+ }
+ return students;
+ }
+
+ /**
+ * 导出学生成绩信息到json文件中
+ * @param students 待导出成绩信息的学生数据的List集合
+ */
+ public static void exportDataToJson(List students) {
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/score.json"))) {
+ JsonArray jsonArray = new JsonArray();
+ int studentCnt = 0;
+ Map isWritten = new HashMap<>();
+ for (int i = 0; i < students.size(); i++) {
+ if (isWritten.containsKey(students.get(i).getId())) {
+ continue;
+ }
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("id", students.get(i).getId());
+ jsonObject.addProperty("name", students.get(i).getName());
+ jsonObject.addProperty("gender", students.get(i).getGender());
+
+ int lessonCnt = 0;
+ double allScore = 0;
+ for (Student stu : students) {
+ if (stu.getId().equals(students.get(i).getId())) {
+ lessonCnt++;
+ allScore += stu.getScore();
+ }
+ }
+ jsonObject.addProperty("total", allScore);
+ jsonObject.addProperty("average", allScore / lessonCnt);
+
+ jsonArray.add(jsonObject);
+ studentCnt++;
+
+ isWritten.put(students.get(i).getId(), true);
+ }
+
+ Gson gson = new GsonBuilder().setPrettyPrinting().create();
+ bw.write(gson.toJson(jsonArray));
+ System.out.println("成功导出 " + studentCnt + " 条学生成绩信息到 json 文件中");
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (Exception e) {
+ System.out.println("数据导出失败!");
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/main/java/cn/czyx007/week10/utils/StudentUtil.java b/src/main/java/cn/czyx007/week10/utils/StudentUtil.java
new file mode 100644
index 0000000..fc1a3ab
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/utils/StudentUtil.java
@@ -0,0 +1,104 @@
+package cn.czyx007.week10.utils;
+
+import cn.czyx007.week10.bean.Student;
+
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class StudentUtil {
+ /**
+ * 检查学生课程成绩是否重复
+ * @param studentMap 存储各学生的所有课程名称,用于辅助验证特定课程成绩是否重复。
+ * Key存储学生学号,Value存储学生所有课程名称的List集合
+ * @param id 学生学号
+ * @param lessonName 特定课程名称
+ * @return true:课程成绩未重复 false:课程成绩重复
+ */
+ public static boolean checkIfStudentScoreDuplicate(Map> studentMap, String id, String lessonName) {
+ if (!(studentMap.containsKey(id) && studentMap.get(id).contains(lessonName))) {
+ if (!studentMap.containsKey(id)) {
+ List lessons = new ArrayList<>();
+ lessons.add(lessonName);
+ studentMap.put(id, lessons);
+ } else {
+ studentMap.get(id).add(lessonName);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * 从键盘依次录入学生成绩信息
+ * @return 录入的所有学生成绩信息的List集合
+ */
+ public static List inputDataFromKeyBoard() {
+ List students = new ArrayList<>();
+
+ System.out.println("学生成绩信息录入格式:学号,姓名,性别,课程名称,成绩");
+ Scanner scanner = new Scanner(System.in);
+ Map> studentMap = new HashMap<>();
+ while (true) {
+ System.out.println("请依次输入学生成绩信息(输入end结束):");
+ String str = scanner.nextLine();
+ if ("end".equalsIgnoreCase(str)) {
+ break;
+ }
+ try {
+ String[] attributes = str.split("[,,]");
+ String id = attributes[0];
+ String name = attributes[1];
+ String gender = attributes[2];
+ String lessonName = attributes[3];
+ Double score = Double.parseDouble(attributes[4]);
+
+ if (StudentUtil.checkIfStudentScoreDuplicate(studentMap, id, lessonName)) {
+ students.add(new Student(id, name, gender, lessonName, score));
+ System.out.println("增加成功!");
+ } else {
+ System.out.println("该学生不能重复录入成绩信息,请重新输入!");
+ }
+ } catch (Exception e) {
+ System.out.println("输入出错!请检查输入格式!");
+ }
+ }
+ try {
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return students;
+ }
+
+ /**
+ * 根据学生学号查询其所有课程成绩
+ * @param id 待查询课程成绩的学生学号
+ * @param students 所有学生成绩信息的List集合
+ */
+ public static void selectAllScoreById(String id, List students) {
+ boolean flag = false;
+ if (students != null) {
+ for (Student student : students) {
+ if (student.getId().equals(id)) {
+ flag = true;
+ System.out.println(student.getLessonName() + " " + student.getScore());
+ }
+ }
+ }
+ if (!flag) {
+ System.out.println("不存在该学生!");
+ }
+
+ try {
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/main/java/cn/czyx007/week10/utils/TxtUtil.java b/src/main/java/cn/czyx007/week10/utils/TxtUtil.java
new file mode 100644
index 0000000..7fe80a4
--- /dev/null
+++ b/src/main/java/cn/czyx007/week10/utils/TxtUtil.java
@@ -0,0 +1,87 @@
+package cn.czyx007.week10.utils;
+
+import cn.czyx007.week10.bean.Student;
+
+import java.io.*;
+import java.util.*;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class TxtUtil {
+ /**
+ * 从txt文件中加载数据
+ * @return 所有不重复的学生数据的List集合
+ */
+ public static List loadDataFromTxt() {
+ List students = new ArrayList<>();
+
+ Map> studentMap = new HashMap<>();
+ try(BufferedReader br = new BufferedReader(new FileReader("src/main/resources/student.txt"))) {
+ int cnt = 0;
+ String aLine;
+ br.readLine();
+ while ((aLine=br.readLine()) != null){
+ String[] attributes = aLine.split("[ \t]");
+ String id = attributes[0];
+ String name = attributes[1];
+ String gender = attributes[2];
+ String lessonName = attributes[3];
+ Double score = Double.parseDouble(attributes[4]);
+
+ if(StudentUtil.checkIfStudentScoreDuplicate(studentMap, id, lessonName)){
+ students.add(new Student(id, name, gender, lessonName, score));
+ cnt++;
+ }
+ }
+
+ System.out.println("成功导入 " + cnt + " 条学生数据");
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (Exception e) {
+ System.out.println("数据导入失败!");
+ e.printStackTrace();
+ }
+ return students;
+ }
+
+ /**
+ * 导出学生成绩信息到txt文件中
+ * @param students 待导出成绩信息的学生数据的List集合
+ */
+ public static void exportDataToTxt(List students) {
+ try(BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/score.txt"))) {
+ bw.write("学号\t姓名\t性别\t总分\t平均分\n");
+
+ int studentCnt = 0;
+ Map isWritten = new HashMap<>();
+ for (int i = 0; i < students.size(); i++) {
+ if(isWritten.containsKey(students.get(i).getId())){
+ continue;
+ }
+
+ bw.write(students.get(i).getId() + "\t" + students.get(i).getName() + "\t" + students.get(i).getGender() + "\t");
+
+ int lessonCnt=0;
+ double allScore=0;
+ for (Student stu : students) {
+ if(stu.getId().equals(students.get(i).getId())){
+ lessonCnt++;
+ allScore += stu.getScore();
+ }
+ }
+ bw.write(allScore + "\t" + allScore/lessonCnt + "\n");
+ studentCnt++;
+
+ isWritten.put(students.get(i).getId(), true);
+ }
+ System.out.println("成功导出 " + studentCnt + " 条学生成绩信息到 txt 文件中");
+ System.out.print("按任意键返回主菜单");
+ System.in.read();
+ } catch (Exception e) {
+ System.out.println("数据导出失败!");
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/main/resources/score.json b/src/main/resources/score.json
new file mode 100644
index 0000000..d791ea3
--- /dev/null
+++ b/src/main/resources/score.json
@@ -0,0 +1,30 @@
+[
+ {
+ "id": "1001",
+ "name": "张三",
+ "gender": "男",
+ "total": 279.0,
+ "average": 93.0
+ },
+ {
+ "id": "1002",
+ "name": "李四",
+ "gender": "男",
+ "total": 98.0,
+ "average": 98.0
+ },
+ {
+ "id": "1003",
+ "name": "王五",
+ "gender": "女",
+ "total": 175.0,
+ "average": 87.5
+ },
+ {
+ "id": "1004",
+ "name": "赵六",
+ "gender": "女",
+ "total": 82.0,
+ "average": 82.0
+ }
+]
\ No newline at end of file
diff --git a/src/main/resources/score.txt b/src/main/resources/score.txt
new file mode 100644
index 0000000..b9db144
--- /dev/null
+++ b/src/main/resources/score.txt
@@ -0,0 +1,5 @@
+学号 姓名 性别 总分 平均分
+1001 张三 男 279.0 93.0
+1002 李四 男 98.0 98.0
+1003 王五 女 175.0 87.5
+1004 赵六 女 82.0 82.0
diff --git a/src/main/resources/score.xls b/src/main/resources/score.xls
new file mode 100644
index 0000000..63e6620
Binary files /dev/null and b/src/main/resources/score.xls differ
diff --git a/src/main/resources/student.json b/src/main/resources/student.json
new file mode 100644
index 0000000..c4271be
--- /dev/null
+++ b/src/main/resources/student.json
@@ -0,0 +1,58 @@
+[
+ {
+ "id": "1001",
+ "name": "张三",
+ "gender": "男",
+ "lessonName": "高数",
+ "score": 95
+ },
+ {
+ "id": "1001",
+ "name": "张三",
+ "gender": "男",
+ "lessonName": "高数",
+ "score": 55
+ },
+ {
+ "id": "1001",
+ "name": "张三",
+ "gender": "男",
+ "lessonName": "c语言",
+ "score": 94
+ },
+ {
+ "id": "1002",
+ "name": "李四",
+ "gender": "男",
+ "lessonName": "英语",
+ "score": 98
+ },
+ {
+ "id": "1003",
+ "name": "王五",
+ "gender": "女",
+ "lessonName": "高数",
+ "score": 90
+ },
+ {
+ "id": "1001",
+ "name": "张三",
+ "gender": "男",
+ "lessonName": "英语",
+ "score": 90
+ },
+ {
+ "id": "1003",
+ "name": "王五",
+ "gender": "女",
+ "lessonName": "英语",
+ "score": 85
+ },
+ {
+ "id": "1004",
+ "name": "赵六",
+ "gender": "女",
+ "lessonName": "高数",
+ "score": 82
+ }
+]
\ No newline at end of file
diff --git a/src/main/resources/student.txt b/src/main/resources/student.txt
new file mode 100644
index 0000000..58a5939
--- /dev/null
+++ b/src/main/resources/student.txt
@@ -0,0 +1,9 @@
+学号 姓名 性别 课程名称 成绩
+1001 张三 男 高数 95
+1001 张三 男 c语言 94
+1002 李四 男 英语 98
+1003 王五 女 高数 90
+1001 张三 男 英语 90
+1003 王五 女 英语 85
+1004 赵六 女 高数 82
+1001 张三 男 高数 55
diff --git a/src/main/resources/student.xls b/src/main/resources/student.xls
new file mode 100644
index 0000000..54a638d
Binary files /dev/null and b/src/main/resources/student.xls differ
diff --git a/src/main/resources/users.csv b/src/main/resources/users.csv
new file mode 100644
index 0000000..cb48fa5
--- /dev/null
+++ b/src/main/resources/users.csv
@@ -0,0 +1,2 @@
+admin 123
+zhangsan abc123
diff --git a/src/test/java/cn/czyx007/week10/test/ExcelUtilTest.java b/src/test/java/cn/czyx007/week10/test/ExcelUtilTest.java
new file mode 100644
index 0000000..9b357b8
--- /dev/null
+++ b/src/test/java/cn/czyx007/week10/test/ExcelUtilTest.java
@@ -0,0 +1,25 @@
+package cn.czyx007.week10.test;
+
+import cn.czyx007.week10.bean.Student;
+import cn.czyx007.week10.utils.ExcelUtil;
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class ExcelUtilTest {
+ @Test
+ public void testLoadDataFromExcel(){
+ List students = ExcelUtil.loadDataFromExcel();
+ students.forEach(System.out::println);
+ }
+
+ @Test
+ public void testExportDataToExcel(){
+ List students = ExcelUtil.loadDataFromExcel();
+ ExcelUtil.exportDataToExcel(students);
+ }
+}
diff --git a/src/test/java/cn/czyx007/week10/test/JsonUtilTest.java b/src/test/java/cn/czyx007/week10/test/JsonUtilTest.java
new file mode 100644
index 0000000..34ed4f9
--- /dev/null
+++ b/src/test/java/cn/czyx007/week10/test/JsonUtilTest.java
@@ -0,0 +1,25 @@
+package cn.czyx007.week10.test;
+
+import cn.czyx007.week10.bean.Student;
+import cn.czyx007.week10.utils.JsonUtil;
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class JsonUtilTest {
+ @Test
+ public void testLoadDataFromJson(){
+ List students = JsonUtil.loadDataFromJson();
+ students.forEach(System.out::println);
+ }
+
+ @Test
+ public void testExportDataToJson(){
+ List students = JsonUtil.loadDataFromJson();
+ JsonUtil.exportDataToJson(students);
+ }
+}
diff --git a/src/test/java/cn/czyx007/week10/test/StudentUtilTest.java b/src/test/java/cn/czyx007/week10/test/StudentUtilTest.java
new file mode 100644
index 0000000..62304ba
--- /dev/null
+++ b/src/test/java/cn/czyx007/week10/test/StudentUtilTest.java
@@ -0,0 +1,43 @@
+package cn.czyx007.week10.test;
+
+import cn.czyx007.week10.bean.Student;
+import cn.czyx007.week10.utils.StudentUtil;
+import cn.czyx007.week10.utils.TxtUtil;
+import org.junit.Test;
+
+import java.util.*;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class StudentUtilTest {
+ @Test
+ public void testCheckIfStudentScoreDuplicate() {
+ List students = new ArrayList<>();
+ students.add(new Student("1001", "张三", "男", "高数", 95.0));
+
+ Map> studentMap = new HashMap<>();
+ List lessons = new ArrayList<>();
+ lessons.add("高数");
+ lessons.add("英语");
+ studentMap.put("1001", lessons);
+
+ if (!StudentUtil.checkIfStudentScoreDuplicate(studentMap, "1001", "高数")) {
+ System.out.println("Duplicated!");
+ students.forEach(System.out::println);
+ }
+ }
+
+ @Test
+ public void testSelectAllScoreById(){
+ List students = TxtUtil.loadDataFromTxt();
+ StudentUtil.selectAllScoreById("1001", students);
+ }
+
+ //testInputDataFromKeyBoard
+ public static void main(String[] args) {
+ List students = StudentUtil.inputDataFromKeyBoard();
+ students.forEach(System.out::println);
+ }
+}
diff --git a/src/test/java/cn/czyx007/week10/test/TxtUtilTest.java b/src/test/java/cn/czyx007/week10/test/TxtUtilTest.java
new file mode 100644
index 0000000..cd767d0
--- /dev/null
+++ b/src/test/java/cn/czyx007/week10/test/TxtUtilTest.java
@@ -0,0 +1,25 @@
+package cn.czyx007.week10.test;
+
+import cn.czyx007.week10.bean.Student;
+import cn.czyx007.week10.utils.TxtUtil;
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class TxtUtilTest {
+ @Test
+ public void testLoadDataFromTxt(){
+ List students = TxtUtil.loadDataFromTxt();
+ students.forEach(System.out::println);
+ }
+
+ @Test
+ public void testExportDataToTxt(){
+ List students = TxtUtil.loadDataFromTxt();
+ TxtUtil.exportDataToTxt(students);
+ }
+}
diff --git a/src/test/java/cn/czyx007/week10/test/UserLoginTest.java b/src/test/java/cn/czyx007/week10/test/UserLoginTest.java
new file mode 100644
index 0000000..d487adb
--- /dev/null
+++ b/src/test/java/cn/czyx007/week10/test/UserLoginTest.java
@@ -0,0 +1,35 @@
+package cn.czyx007.week10.test;
+
+import cn.czyx007.week10.bean.User;
+import cn.czyx007.week10.service.UserLogin;
+import org.junit.Test;
+
+/**
+ * @author : 张宇轩
+ * @createTime : 2022/11/3
+ */
+public class UserLoginTest {
+ @Test
+ public void testLogin(){
+ if (UserLogin.login(new User("admin", "123"))) {
+ System.out.println("true");
+ } else System.out.println("false");
+ }
+
+ @Test
+ public void testCheckPassword(){
+ if (UserLogin.checkPassword(new User("admin", "123"), "123")){
+ System.out.println("true");
+ } else System.out.println("false");
+ }
+
+ @Test
+ public void testUpdateUserDataFile(){
+ UserLogin.updateUserDataFile(new User("admin", "123"));
+ }
+
+ //testChangePassword
+ public static void main(String[] args) {
+ UserLogin.changePassword(new User("admin", "123"));
+ }
+}