优化历史题目读取,确保跳过空行,忽略题号

main
Teptao 7 months ago
parent 73a07cdccf
commit 8ee14a94cf

@ -26,7 +26,7 @@ public class Application {
// 创建数据访问层的实例
IUserRepository userRepository = new UserRepository();
this.fileService = new TextFilePersistence();
this.fileService = new FileService();
// 创建应用服务层的实例,并注入依赖
this.authService = new AuthService(userRepository);
// 获取会话管理器的单例实例
@ -78,7 +78,7 @@ public class Application {
*
*/
private void handleLoggedInState() throws IOException {
String currentLevel = sessionManager.getCurrentLevelName();
String currentLevel = sessionManager.getCurrentLevel();
System.out.printf(
"当前选择为%s出题。请输入生成题目数量10-30或输入 '切换为XX',或输入 '-1' 退出登录:%n",
currentLevel);

@ -12,13 +12,12 @@ import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
*
*
*/
public class TextFilePersistence implements IFileService {
public class FileService implements IFileService {
private static final DateTimeFormatter FILE_NAME_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
@ -67,9 +66,10 @@ public class TextFilePersistence implements IFileService {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String cleanedLine = line.replaceFirst("^\\d+\\.\\s*", "");
System.out.println(cleanedLine);
historySet.add(cleanedLine);
if (!line.trim().isEmpty()) {
String cleanedLine = line.replaceFirst("^\\d+\\.\\s*", "");
historySet.add(cleanedLine);
}
}
}
}

@ -3,40 +3,27 @@
*
*/
public enum Operator {
ADD('+', true),
SUBTRACT('-', false),
MULTIPLY('*', true),
DIVIDE('/', false);
ADD('+'),
SUBTRACT('-'),
MULTIPLY('*'),
DIVIDE('/');
private final char symbol;
private final boolean isCommutative; // 标记是否为可交换运算符(如加法、乘法)
/**
*
*
* @param symbol
* @param isCommutative
*/
Operator(char symbol, boolean isCommutative) {
Operator(char symbol) {
this.symbol = symbol;
this.isCommutative = isCommutative;
}
/**
*
*
* @return
*/
public char getSymbol() {
return symbol;
}
/**
*
*
* @return true
*/
public boolean isCommutative() {
return isCommutative;
}
}

@ -95,10 +95,9 @@ public final class SessionManager extends AbstractSessionManager {
/**
*
*
* @return "小学"
*/
public String getCurrentLevelName() {
public String getCurrentLevel() {
return currentLevel;
}
}
Loading…
Cancel
Save