|
|
|
|
@ -22,7 +22,7 @@ import java.util.Set;
|
|
|
|
|
public class MathQuestionGenerator {
|
|
|
|
|
|
|
|
|
|
// 常量定义
|
|
|
|
|
private static final int MIN_OPERAND_COUNT = 1;
|
|
|
|
|
private static final int DEFAULT_MIN_OPERAND_COUNT = 1; // 默认最少操作数(用于直接实现QuestionGenerator接口的情况)
|
|
|
|
|
private static final int MAX_OPERAND_COUNT = 5;
|
|
|
|
|
private static final int MIN_OPERAND_VALUE = 1;
|
|
|
|
|
private static final int MAX_OPERAND_VALUE = 100;
|
|
|
|
|
@ -80,17 +80,26 @@ public class MathQuestionGenerator {
|
|
|
|
|
*/
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
showWelcomeMessage();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 【人工修复】修复while循环警告,简化循环逻辑
|
|
|
|
|
// 修改前:使用shouldContinue变量但设置后立即break,IDEA提示条件始终为true
|
|
|
|
|
// 修改后:直接使用while(true)配合break,逻辑更简洁清晰
|
|
|
|
|
while (true) {
|
|
|
|
|
if (!login()) {
|
|
|
|
|
Boolean loginResult = login();
|
|
|
|
|
if (loginResult == null) {
|
|
|
|
|
// 用户选择退出
|
|
|
|
|
break;
|
|
|
|
|
} else if (!loginResult) {
|
|
|
|
|
// 登录失败,继续循环
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("登录成功!欢迎 " + currentUser.getUsername() + " 老师");
|
|
|
|
|
|
|
|
|
|
System.out.println("登录成功!欢迎 " + currentUser.username() + " 老师");
|
|
|
|
|
operateAfterLogin();
|
|
|
|
|
// 登录成功后可以选择是否继续,这里保持继续循环
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示欢迎信息
|
|
|
|
|
*/
|
|
|
|
|
@ -108,17 +117,25 @@ public class MathQuestionGenerator {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户登录验证
|
|
|
|
|
* @return 登录是否成功
|
|
|
|
|
* @return 登录是否成功,null表示用户选择退出
|
|
|
|
|
*/
|
|
|
|
|
private static boolean login() {
|
|
|
|
|
private static Boolean login() {
|
|
|
|
|
System.out.println("=== 用户登录 ===");
|
|
|
|
|
System.out.println("请输入用户名和密码(用空格隔开):");
|
|
|
|
|
System.out.println("请输入用户名和密码(用空格隔开),或输入 'exit' 退出程序:");
|
|
|
|
|
System.out.print("> ");
|
|
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
|
|
|
|
|
// 【人工修改】添加退出选项,提升用户体验
|
|
|
|
|
// 修改前:只能通过用户名密码登录,没有退出选项
|
|
|
|
|
// 修改后:支持输入 'exit' 退出程序,提供更好的用户控制
|
|
|
|
|
if (input.equalsIgnoreCase("exit")) {
|
|
|
|
|
System.out.println("感谢使用数学题目生成系统,再见!");
|
|
|
|
|
return null; // 表示用户选择退出
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] parts = input.split("\\s+");
|
|
|
|
|
if (parts.length != 2) {
|
|
|
|
|
System.out.println("请输入正确的用户名、密码");
|
|
|
|
|
System.out.println("请输入正确的用户名、密码,或输入 'exit' 退出");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -126,11 +143,11 @@ public class MathQuestionGenerator {
|
|
|
|
|
String password = parts[1];
|
|
|
|
|
|
|
|
|
|
User user = USERS.get(username);
|
|
|
|
|
if (user != null && user.getPassword().equals(password)) {
|
|
|
|
|
if (user != null && user.password().equals(password)) {
|
|
|
|
|
currentUser = user;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("请输入正确的用户名、密码");
|
|
|
|
|
System.out.println("请输入正确的用户名、密码,或输入 'exit' 退出");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -139,24 +156,29 @@ public class MathQuestionGenerator {
|
|
|
|
|
* 登录后的操作处理
|
|
|
|
|
*/
|
|
|
|
|
private static void operateAfterLogin() {
|
|
|
|
|
String currentGrade = currentUser.getGrade();
|
|
|
|
|
String currentGrade = currentUser.grade();
|
|
|
|
|
showOperationMenu(currentGrade);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
boolean shouldContinue = true;
|
|
|
|
|
while (shouldContinue) {
|
|
|
|
|
String input = getUserInput();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (handleExitCommand(input)) {
|
|
|
|
|
break;
|
|
|
|
|
shouldContinue = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (input.startsWith("切换为")) {
|
|
|
|
|
if (handleSwitchCommand(input)) {
|
|
|
|
|
currentGrade = currentUser.getGrade();
|
|
|
|
|
currentGrade = currentUser.grade();
|
|
|
|
|
// 【人工修改】切换成功后不立即显示菜单,等待用户按回车键
|
|
|
|
|
// 修改前:切换成功后立即显示操作菜单,界面切换突兀
|
|
|
|
|
// 修改后:切换成功后等待用户按回车键,然后显示操作菜单,界面切换更自然
|
|
|
|
|
showOperationMenu(currentGrade);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleQuestionGeneration(input, currentGrade);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -195,12 +217,59 @@ public class MathQuestionGenerator {
|
|
|
|
|
try {
|
|
|
|
|
int count = Integer.parseInt(input);
|
|
|
|
|
if (count < MIN_QUESTION_COUNT || count > MAX_QUESTION_COUNT) {
|
|
|
|
|
System.out.println("题目数量应在" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + "之间");
|
|
|
|
|
handleInvalidCount(currentGrade);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
generateQuestions(count, currentGrade);
|
|
|
|
|
showOperationMenu(currentGrade);
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("请输入有效的数字(" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + ")或切换命令");
|
|
|
|
|
handleInvalidInput(currentGrade);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理无效的题目数量
|
|
|
|
|
* @param currentGrade 当前年级类型
|
|
|
|
|
*/
|
|
|
|
|
private static void handleInvalidCount(String currentGrade) {
|
|
|
|
|
System.out.println("题目数量应在" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + "之间");
|
|
|
|
|
// 【人工修改】输入错误后要求按回车键继续,提升用户体验
|
|
|
|
|
// 修改前:输入错误后直接显示操作菜单,界面切换突兀
|
|
|
|
|
// 修改后:输入错误后要求按回车键,让用户有时间阅读错误信息,界面切换更自然
|
|
|
|
|
System.out.println("按回车键继续...");
|
|
|
|
|
System.out.flush();
|
|
|
|
|
waitForEnter();
|
|
|
|
|
showOperationMenu(currentGrade);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理无效的输入
|
|
|
|
|
* @param currentGrade 当前年级类型
|
|
|
|
|
*/
|
|
|
|
|
private static void handleInvalidInput(String currentGrade) {
|
|
|
|
|
System.out.println("请输入有效的数字(" + MIN_QUESTION_COUNT + "-" + MAX_QUESTION_COUNT + ")或切换命令");
|
|
|
|
|
// 【人工修改】输入错误后要求按回车键继续,提升用户体验
|
|
|
|
|
// 修改前:输入错误后直接显示操作菜单,界面切换突兀
|
|
|
|
|
// 修改后:输入错误后要求按回车键,让用户有时间阅读错误信息,界面切换更自然
|
|
|
|
|
System.out.println("按回车键继续...");
|
|
|
|
|
System.out.flush();
|
|
|
|
|
waitForEnter();
|
|
|
|
|
showOperationMenu(currentGrade);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待用户按回车键
|
|
|
|
|
*/
|
|
|
|
|
private static void waitForEnter() {
|
|
|
|
|
try {
|
|
|
|
|
// 【人工修复】修复InputStream.read()结果被忽略的警告
|
|
|
|
|
// 修改前:System.in.read()的结果被忽略,IDEA显示警告
|
|
|
|
|
// 修改后:将读取结果赋值给变量,消除警告
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
int readResult = System.in.read();
|
|
|
|
|
// readResult用于等待用户按回车键,不需要使用具体值
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 忽略读取错误
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -232,15 +301,49 @@ public class MathQuestionGenerator {
|
|
|
|
|
String targetGrade = command.substring(3).trim();
|
|
|
|
|
|
|
|
|
|
if (targetGrade.equals("小学") || targetGrade.equals("初中") || targetGrade.equals("高中")) {
|
|
|
|
|
currentUser = new User(currentUser.getUsername(), currentUser.getPassword(), targetGrade);
|
|
|
|
|
currentUser = new User(currentUser.username(), currentUser.password(), targetGrade);
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("✓ 切换成功!当前为" + targetGrade + "出题模式");
|
|
|
|
|
// 【人工修改】切换成功后要求按回车键继续,提升用户体验
|
|
|
|
|
// 修改前:切换成功后直接显示操作菜单,界面切换突兀
|
|
|
|
|
// 修改后:切换成功后要求按回车键,让用户有时间阅读信息,界面切换更自然
|
|
|
|
|
System.out.println("按回车键继续...");
|
|
|
|
|
System.out.flush();
|
|
|
|
|
try {
|
|
|
|
|
// 【人工修复】修复InputStream.read()结果被忽略的警告
|
|
|
|
|
// 修改前:System.in.read()的结果被忽略,IDEA显示警告
|
|
|
|
|
// 修改后:将读取结果赋值给变量,消除警告
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
int readResult = System.in.read();
|
|
|
|
|
// 【人工修复】修复空if语句体警告
|
|
|
|
|
// 修改前:if (readResult == -1) { } 空语句体,IDEA显示警告
|
|
|
|
|
// 修改后:删除空的if语句,因为IOException已经被catch块处理
|
|
|
|
|
// readResult用于等待用户按回车键,不需要检查具体值
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 忽略读取错误
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println();
|
|
|
|
|
System.out.println("✗ 请输入小学、初中和高中三个选项中的一个");
|
|
|
|
|
// 【人工修改】切换失败后要求按回车键继续,提升用户体验
|
|
|
|
|
// 修改前:切换失败后直接显示操作菜单,界面切换突兀
|
|
|
|
|
// 修改后:切换失败后要求按回车键,让用户有时间阅读错误信息,界面切换更自然
|
|
|
|
|
System.out.println("按回车键继续...");
|
|
|
|
|
System.out.flush();
|
|
|
|
|
try {
|
|
|
|
|
// 【人工修复】修复InputStream.read()结果被忽略的警告
|
|
|
|
|
// 修改前:System.in.read()的结果被忽略,IDEA显示警告
|
|
|
|
|
// 修改后:将读取结果赋值给变量,消除警告
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
int readResult = System.in.read();
|
|
|
|
|
// 【人工修复】修复空if语句体警告
|
|
|
|
|
// 修改前:if (readResult == -1) { } 空语句体,IDEA显示警告
|
|
|
|
|
// 修改后:删除空的if语句,因为IOException已经被catch块处理
|
|
|
|
|
// readResult用于等待用户按回车键,不需要检查具体值
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 忽略读取错误
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -262,10 +365,50 @@ public class MathQuestionGenerator {
|
|
|
|
|
* @return 用户文件夹
|
|
|
|
|
*/
|
|
|
|
|
private static File createUserDirectory() {
|
|
|
|
|
File userDir = new File(currentUser.getUsername());
|
|
|
|
|
// 【人工修改】创建paper文件夹结构:paper->用户名->年级
|
|
|
|
|
// 修改前:File userDir = new File(currentUser.getUsername());
|
|
|
|
|
// 修改后:创建三级文件夹结构,试卷集中管理
|
|
|
|
|
File paperDir = new File("paper");
|
|
|
|
|
if (!paperDir.exists()) {
|
|
|
|
|
// 【人工修复】修复File.mkdir()结果被忽略的警告
|
|
|
|
|
// 修改前:paperDir.mkdir()的结果被忽略,IDEA显示警告
|
|
|
|
|
// 修改后:检查mkdir()返回值,确保文件夹创建成功
|
|
|
|
|
boolean created = paperDir.mkdir();
|
|
|
|
|
if (!created) {
|
|
|
|
|
System.err.println("警告:无法创建paper文件夹");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File userDir = new File(paperDir, currentUser.username());
|
|
|
|
|
if (!userDir.exists()) {
|
|
|
|
|
userDir.mkdir();
|
|
|
|
|
// 【人工修复】修复File.mkdir()结果被忽略的警告
|
|
|
|
|
// 修改前:userDir.mkdir()的结果被忽略,IDEA显示警告
|
|
|
|
|
// 修改后:检查mkdir()返回值,确保文件夹创建成功
|
|
|
|
|
boolean created = userDir.mkdir();
|
|
|
|
|
if (!created) {
|
|
|
|
|
System.err.println("警告:无法创建用户文件夹:" + currentUser.username());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建年级子文件夹
|
|
|
|
|
File[] gradeDirs = {
|
|
|
|
|
new File(userDir, "小学"),
|
|
|
|
|
new File(userDir, "初中"),
|
|
|
|
|
new File(userDir, "高中")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (File gradeDir : gradeDirs) {
|
|
|
|
|
if (!gradeDir.exists()) {
|
|
|
|
|
// 【人工修复】修复File.mkdir()结果被忽略的警告
|
|
|
|
|
// 修改前:gradeDir.mkdir()的结果被忽略,IDEA显示警告
|
|
|
|
|
// 修改后:检查mkdir()返回值,确保文件夹创建成功
|
|
|
|
|
boolean created = gradeDir.mkdir();
|
|
|
|
|
if (!created) {
|
|
|
|
|
System.err.println("警告:无法创建年级文件夹:" + gradeDir.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return userDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -301,26 +444,44 @@ public class MathQuestionGenerator {
|
|
|
|
|
private static String generateSingleQuestion(QuestionGenerator generator, Random random,
|
|
|
|
|
Set<String> historyQuestions, List<String> newQuestions,
|
|
|
|
|
int questionNumber) {
|
|
|
|
|
String question;
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
int operandCount = random.nextInt(MAX_OPERAND_COUNT - MIN_OPERAND_COUNT + 1) + MIN_OPERAND_COUNT;
|
|
|
|
|
int[] operands = new int[operandCount];
|
|
|
|
|
for (int i = 0; i < operandCount; i++) {
|
|
|
|
|
operands[i] = random.nextInt(MAX_OPERAND_VALUE - MIN_OPERAND_VALUE + 1) + MIN_OPERAND_VALUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
question = generator.generateQuestion(operands, random);
|
|
|
|
|
attempts++;
|
|
|
|
|
if (attempts > MAX_GENERATION_ATTEMPTS) {
|
|
|
|
|
question = "备用题目:" + questionNumber;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while (historyQuestions.contains(question) || newQuestions.contains(question));
|
|
|
|
|
String question;
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
// 【人工修改】根据年级设置不同的最少操作数
|
|
|
|
|
// 修改前:int operandCount = random.nextInt(5) + 1; // 可能生成单操作数
|
|
|
|
|
// 修改后:根据年级设置最少操作数,小学最少2个,其他年级最少1个
|
|
|
|
|
int minOperandCount = getMinOperandCountForGrade(generator);
|
|
|
|
|
int operandCount = random.nextInt(MAX_OPERAND_COUNT - minOperandCount + 1) + minOperandCount;
|
|
|
|
|
int[] operands = new int[operandCount];
|
|
|
|
|
for (int i = 0; i < operandCount; i++) {
|
|
|
|
|
operands[i] = random.nextInt(MAX_OPERAND_VALUE - MIN_OPERAND_VALUE + 1) + MIN_OPERAND_VALUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
question = generator.generateQuestion(operands, random);
|
|
|
|
|
attempts++;
|
|
|
|
|
if (attempts > MAX_GENERATION_ATTEMPTS) {
|
|
|
|
|
question = "备用题目:" + questionNumber;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while (historyQuestions.contains(question) || newQuestions.contains(question));
|
|
|
|
|
|
|
|
|
|
return question;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据年级获取最少操作数
|
|
|
|
|
* @param generator 题目生成器
|
|
|
|
|
* @return 最少操作数
|
|
|
|
|
*/
|
|
|
|
|
private static int getMinOperandCountForGrade(QuestionGenerator generator) {
|
|
|
|
|
// 直接调用生成器的getMinOperandCount方法,利用多态特性
|
|
|
|
|
if (generator instanceof AbstractQuestionGenerator) {
|
|
|
|
|
return ((AbstractQuestionGenerator) generator).getMinOperandCount();
|
|
|
|
|
} else {
|
|
|
|
|
return DEFAULT_MIN_OPERAND_COUNT; // 默认最少1个操作数
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载历史题目用于去重
|
|
|
|
|
@ -330,7 +491,15 @@ public class MathQuestionGenerator {
|
|
|
|
|
private static Set<String> loadHistoryQuestions(File userDir) {
|
|
|
|
|
Set<String> historyQuestions = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
File[] files = userDir.listFiles((dir, name) -> name.endsWith(".txt"));
|
|
|
|
|
// 【人工修改】从对应的年级文件夹加载历史题目
|
|
|
|
|
String currentGrade = currentUser.grade();
|
|
|
|
|
File gradeDir = new File(userDir, currentGrade);
|
|
|
|
|
|
|
|
|
|
if (!gradeDir.exists()) {
|
|
|
|
|
return historyQuestions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File[] files = gradeDir.listFiles((dir, name) -> name.endsWith(".txt"));
|
|
|
|
|
if (files != null) {
|
|
|
|
|
for (File file : files) {
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
|
|
|
|
@ -369,9 +538,13 @@ public class MathQuestionGenerator {
|
|
|
|
|
* @return 输出文件
|
|
|
|
|
*/
|
|
|
|
|
private static File createOutputFile(File userDir) {
|
|
|
|
|
// 【人工修改】将文件保存到对应的年级文件夹
|
|
|
|
|
String currentGrade = currentUser.grade();
|
|
|
|
|
File gradeDir = new File(userDir, currentGrade);
|
|
|
|
|
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
|
|
|
|
String filename = dateFormat.format(new Date()) + ".txt";
|
|
|
|
|
return new File(userDir, filename);
|
|
|
|
|
return new File(gradeDir, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -403,51 +576,32 @@ public class MathQuestionGenerator {
|
|
|
|
|
System.out.println("✓ 题目生成完成!");
|
|
|
|
|
System.out.println("✓ 已生成 " + count + " 道" + grade + "数学题目");
|
|
|
|
|
System.out.println("✓ 文件已保存到:" + outputFile.getName());
|
|
|
|
|
// 【人工修改】生成题目后要求按回车键继续,提升用户体验
|
|
|
|
|
// 修改前:生成题目后直接显示操作菜单,界面切换突兀
|
|
|
|
|
// 修改后:生成题目后要求按回车键,让用户有时间阅读生成结果,界面切换更自然
|
|
|
|
|
System.out.println("按回车键继续...");
|
|
|
|
|
System.out.flush();
|
|
|
|
|
try {
|
|
|
|
|
// 【人工修复】修复InputStream.read()结果被忽略的警告
|
|
|
|
|
// 修改前:System.in.read()的结果被忽略,IDEA显示警告
|
|
|
|
|
// 修改后:将读取结果赋值给变量,消除警告
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
int readResult = System.in.read();
|
|
|
|
|
// readResult用于等待用户按回车键,不需要使用具体值
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 忽略读取错误
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户类,存储用户信息
|
|
|
|
|
* 【人工修复】将User类改为记录类,消除IDEA警告
|
|
|
|
|
* 修改前:使用传统类定义,IDEA提示"类可以是记录类"
|
|
|
|
|
* 修改后:使用Java 14+的记录类语法,代码更简洁
|
|
|
|
|
*/
|
|
|
|
|
static class User {
|
|
|
|
|
private final String username;
|
|
|
|
|
private final String password;
|
|
|
|
|
private final String grade;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
* @param password 密码
|
|
|
|
|
* @param grade 年级类型
|
|
|
|
|
*/
|
|
|
|
|
public User(String username, String password, String grade) {
|
|
|
|
|
this.username = username;
|
|
|
|
|
this.password = password;
|
|
|
|
|
this.grade = grade;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户名
|
|
|
|
|
* @return 用户名
|
|
|
|
|
*/
|
|
|
|
|
public String getUsername() {
|
|
|
|
|
return username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取密码
|
|
|
|
|
* @return 密码
|
|
|
|
|
*/
|
|
|
|
|
public String getPassword() {
|
|
|
|
|
return password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取年级类型
|
|
|
|
|
* @return 年级类型
|
|
|
|
|
*/
|
|
|
|
|
public String getGrade() {
|
|
|
|
|
return grade;
|
|
|
|
|
}
|
|
|
|
|
record User(String username, String password, String grade) {
|
|
|
|
|
// 记录类自动生成构造函数、getter方法、equals、hashCode和toString
|
|
|
|
|
// 无需手动编写这些方法,代码更简洁
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|