2022/11/04 20:00 上传第9次作业题1
This commit is contained in:
commit
049ee30434
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
14
.idea/compiler.xml
generated
Normal file
14
.idea/compiler.xml
generated
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile default="true" name="Default" enabled="true" />
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="week10-work1" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
20
.idea/jarRepositories.xml
generated
Normal file
20
.idea/jarRepositories.xml
generated
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://maven.aliyun.com/repository/public" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
40
pom.xml
Normal file
40
pom.xml
Normal file
@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cn.czyx007.week10</groupId>
|
||||
<artifactId>week10-work1</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.jexcelapi</groupId>
|
||||
<artifactId>jxl</artifactId>
|
||||
<version>2.6.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.20</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
38
src/main/java/cn/czyx007/week10/Driver.java
Normal file
38
src/main/java/cn/czyx007/week10/Driver.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
39
src/main/java/cn/czyx007/week10/bean/Student.java
Normal file
39
src/main/java/cn/czyx007/week10/bean/Student.java
Normal file
@ -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;
|
||||
}
|
24
src/main/java/cn/czyx007/week10/bean/User.java
Normal file
24
src/main/java/cn/czyx007/week10/bean/User.java
Normal file
@ -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;
|
||||
}
|
110
src/main/java/cn/czyx007/week10/service/MainMenu.java
Normal file
110
src/main/java/cn/czyx007/week10/service/MainMenu.java
Normal file
@ -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<Student> 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;
|
||||
}
|
||||
}
|
104
src/main/java/cn/czyx007/week10/service/UserLogin.java
Normal file
104
src/main/java/cn/czyx007/week10/service/UserLogin.java
Normal file
@ -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<User> 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();
|
||||
}
|
||||
}
|
||||
}
|
125
src/main/java/cn/czyx007/week10/utils/ExcelUtil.java
Normal file
125
src/main/java/cn/czyx007/week10/utils/ExcelUtil.java
Normal file
@ -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<Student> loadDataFromExcel() {
|
||||
List<Student> students = new ArrayList<>();
|
||||
|
||||
Map<String, List<String>> 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<Student> 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<String, Boolean> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
101
src/main/java/cn/czyx007/week10/utils/JsonUtil.java
Normal file
101
src/main/java/cn/czyx007/week10/utils/JsonUtil.java
Normal file
@ -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<Student> loadDataFromJson() {
|
||||
List<Student> 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<ArrayList<Student>>(){}.getType());
|
||||
|
||||
Map<String, List<String>> 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<Student> students) {
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/score.json"))) {
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
int studentCnt = 0;
|
||||
Map<String, Boolean> 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();
|
||||
}
|
||||
}
|
||||
}
|
104
src/main/java/cn/czyx007/week10/utils/StudentUtil.java
Normal file
104
src/main/java/cn/czyx007/week10/utils/StudentUtil.java
Normal file
@ -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<String, List<String>> studentMap, String id, String lessonName) {
|
||||
if (!(studentMap.containsKey(id) && studentMap.get(id).contains(lessonName))) {
|
||||
if (!studentMap.containsKey(id)) {
|
||||
List<String> 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<Student> inputDataFromKeyBoard() {
|
||||
List<Student> students = new ArrayList<>();
|
||||
|
||||
System.out.println("学生成绩信息录入格式:学号,姓名,性别,课程名称,成绩");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
Map<String, List<String>> 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<Student> 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();
|
||||
}
|
||||
}
|
||||
}
|
87
src/main/java/cn/czyx007/week10/utils/TxtUtil.java
Normal file
87
src/main/java/cn/czyx007/week10/utils/TxtUtil.java
Normal file
@ -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<Student> loadDataFromTxt() {
|
||||
List<Student> students = new ArrayList<>();
|
||||
|
||||
Map<String, List<String>> 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<Student> students) {
|
||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/score.txt"))) {
|
||||
bw.write("学号\t姓名\t性别\t总分\t平均分\n");
|
||||
|
||||
int studentCnt = 0;
|
||||
Map<String, Boolean> 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();
|
||||
}
|
||||
}
|
||||
}
|
30
src/main/resources/score.json
Normal file
30
src/main/resources/score.json
Normal file
@ -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
|
||||
}
|
||||
]
|
5
src/main/resources/score.txt
Normal file
5
src/main/resources/score.txt
Normal file
@ -0,0 +1,5 @@
|
||||
学号 姓名 性别 总分 平均分
|
||||
1001 张三 男 279.0 93.0
|
||||
1002 李四 男 98.0 98.0
|
||||
1003 王五 女 175.0 87.5
|
||||
1004 赵六 女 82.0 82.0
|
BIN
src/main/resources/score.xls
Normal file
BIN
src/main/resources/score.xls
Normal file
Binary file not shown.
58
src/main/resources/student.json
Normal file
58
src/main/resources/student.json
Normal file
@ -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
|
||||
}
|
||||
]
|
9
src/main/resources/student.txt
Normal file
9
src/main/resources/student.txt
Normal file
@ -0,0 +1,9 @@
|
||||
学号 姓名 性别 课程名称 成绩
|
||||
1001 张三 男 高数 95
|
||||
1001 张三 男 c语言 94
|
||||
1002 李四 男 英语 98
|
||||
1003 王五 女 高数 90
|
||||
1001 张三 男 英语 90
|
||||
1003 王五 女 英语 85
|
||||
1004 赵六 女 高数 82
|
||||
1001 张三 男 高数 55
|
BIN
src/main/resources/student.xls
Normal file
BIN
src/main/resources/student.xls
Normal file
Binary file not shown.
2
src/main/resources/users.csv
Normal file
2
src/main/resources/users.csv
Normal file
@ -0,0 +1,2 @@
|
||||
admin 123
|
||||
zhangsan abc123
|
|
25
src/test/java/cn/czyx007/week10/test/ExcelUtilTest.java
Normal file
25
src/test/java/cn/czyx007/week10/test/ExcelUtilTest.java
Normal file
@ -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<Student> students = ExcelUtil.loadDataFromExcel();
|
||||
students.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportDataToExcel(){
|
||||
List<Student> students = ExcelUtil.loadDataFromExcel();
|
||||
ExcelUtil.exportDataToExcel(students);
|
||||
}
|
||||
}
|
25
src/test/java/cn/czyx007/week10/test/JsonUtilTest.java
Normal file
25
src/test/java/cn/czyx007/week10/test/JsonUtilTest.java
Normal file
@ -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<Student> students = JsonUtil.loadDataFromJson();
|
||||
students.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportDataToJson(){
|
||||
List<Student> students = JsonUtil.loadDataFromJson();
|
||||
JsonUtil.exportDataToJson(students);
|
||||
}
|
||||
}
|
43
src/test/java/cn/czyx007/week10/test/StudentUtilTest.java
Normal file
43
src/test/java/cn/czyx007/week10/test/StudentUtilTest.java
Normal file
@ -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<Student> students = new ArrayList<>();
|
||||
students.add(new Student("1001", "张三", "男", "高数", 95.0));
|
||||
|
||||
Map<String, List<String>> studentMap = new HashMap<>();
|
||||
List<String> 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<Student> students = TxtUtil.loadDataFromTxt();
|
||||
StudentUtil.selectAllScoreById("1001", students);
|
||||
}
|
||||
|
||||
//testInputDataFromKeyBoard
|
||||
public static void main(String[] args) {
|
||||
List<Student> students = StudentUtil.inputDataFromKeyBoard();
|
||||
students.forEach(System.out::println);
|
||||
}
|
||||
}
|
25
src/test/java/cn/czyx007/week10/test/TxtUtilTest.java
Normal file
25
src/test/java/cn/czyx007/week10/test/TxtUtilTest.java
Normal file
@ -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<Student> students = TxtUtil.loadDataFromTxt();
|
||||
students.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportDataToTxt(){
|
||||
List<Student> students = TxtUtil.loadDataFromTxt();
|
||||
TxtUtil.exportDataToTxt(students);
|
||||
}
|
||||
}
|
35
src/test/java/cn/czyx007/week10/test/UserLoginTest.java
Normal file
35
src/test/java/cn/czyx007/week10/test/UserLoginTest.java
Normal file
@ -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"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user