Compare commits
5 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
6e8637eab1 | 4 months ago |
|
|
c0bb5f7399 | 4 months ago |
|
|
155be7ca07 | 4 months ago |
|
|
6dce4233dc | 4 months ago |
|
|
d0bc8c9496 | 4 months ago |
@ -0,0 +1,25 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class AuthManager {
|
||||
private static final List<User> USERS = Arrays.asList(
|
||||
new User("张三1", "123", UserType.PRIMARY),
|
||||
new User("张三2", "123", UserType.PRIMARY),
|
||||
new User("张三3", "123", UserType.PRIMARY),
|
||||
new User("李四1", "123", UserType.JUNIOR),
|
||||
new User("李四2", "123", UserType.JUNIOR),
|
||||
new User("李四3", "123", UserType.JUNIOR),
|
||||
new User("王五1", "123", UserType.SENIOR),
|
||||
new User("王五2", "123", UserType.SENIOR),
|
||||
new User("王五3", "123", UserType.SENIOR)
|
||||
);
|
||||
|
||||
public User authenticate(String username, String password) {
|
||||
for (User user : USERS) {
|
||||
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DuplicateChecker {
|
||||
|
||||
public boolean isDuplicate(String username, String questionContent) {
|
||||
String userDir = "questions/" + username + "/";
|
||||
File dir = new File(userDir);
|
||||
|
||||
if (!dir.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
|
||||
if (files == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (File file : files) {
|
||||
try {
|
||||
List<String> lines = Files.readAllLines(Paths.get(file.getAbsolutePath()));
|
||||
for (String line : lines) {
|
||||
if (line.trim().isEmpty()) continue;
|
||||
// 提取题目内容(去掉题号)
|
||||
String content = line.substring(line.indexOf('.') + 1).trim();
|
||||
if (content.equals(questionContent)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("读取文件失败: " + file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<String> getUserQuestions(String username) {
|
||||
List<String> questions = new ArrayList<>();
|
||||
String userDir = "questions/" + username + "/";
|
||||
File dir = new File(userDir);
|
||||
|
||||
if (!dir.exists()) {
|
||||
return questions;
|
||||
}
|
||||
|
||||
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
|
||||
if (files == null) {
|
||||
return questions;
|
||||
}
|
||||
|
||||
for (File file : files) {
|
||||
try {
|
||||
List<String> lines = Files.readAllLines(Paths.get(file.getAbsolutePath()));
|
||||
for (String line : lines) {
|
||||
if (line.trim().isEmpty()) continue;
|
||||
String content = line.substring(line.indexOf('.') + 1).trim();
|
||||
questions.add(content);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("读取文件失败: " + file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class FileService {
|
||||
|
||||
public String saveQuestions(String username, List<Question> questions) throws IOException {
|
||||
// 创建用户目录
|
||||
String userDir = "questions/" + username + "/";
|
||||
File dir = new File(userDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
|
||||
String filename = userDir + timestamp + ".txt";
|
||||
|
||||
// 写入文件
|
||||
try (FileWriter writer = new FileWriter(filename)) {
|
||||
for (int i = 0; i < questions.size(); i++) {
|
||||
writer.write(questions.get(i).toString());
|
||||
writer.write("\n\n"); // 题目之间空一行
|
||||
}
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class JuniorQuestionGenerator implements QuestionGenerator {
|
||||
private Random random = new Random();
|
||||
private final String[] OPERATORS = {"+", "-", "*", "/"};
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
int operandCount = random.nextInt(3) + 2; // 2-4个操作数
|
||||
StringBuilder question = new StringBuilder();
|
||||
|
||||
// 确保至少有一个平方或开根号
|
||||
boolean hasSpecialOperator = false;
|
||||
int specialOperatorPosition = random.nextInt(operandCount);
|
||||
|
||||
for (int i = 0; i < operandCount; i++) {
|
||||
if (i > 0) {
|
||||
question.append(" ").append(OPERATORS[random.nextInt(OPERATORS.length)]).append(" ");
|
||||
}
|
||||
|
||||
// 在特定位置添加平方或开根号
|
||||
if (i == specialOperatorPosition || !hasSpecialOperator) {
|
||||
if (random.nextBoolean()) {
|
||||
// 平方
|
||||
int num = random.nextInt(20) + 1; // 1-20的平方
|
||||
question.append(num).append("²");
|
||||
hasSpecialOperator = true;
|
||||
} else {
|
||||
// 开根号
|
||||
int num = random.nextInt(100) + 1; // 1-100开根号
|
||||
question.append("√").append(num);
|
||||
hasSpecialOperator = true;
|
||||
}
|
||||
} else {
|
||||
question.append(random.nextInt(100) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return question.toString() + " = ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidQuestion(String question) {
|
||||
return (question.contains("²") || question.contains("√")) &&
|
||||
question.contains("=");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class PrimaryQuestionGenerator implements QuestionGenerator {
|
||||
private Random random = new Random();
|
||||
private final String[] OPERATORS = {"+", "-", "*", "/"};
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
int operandCount = random.nextInt(3) + 2; // 2-4个操作数
|
||||
StringBuilder question = new StringBuilder();
|
||||
|
||||
// 随机决定是否添加括号
|
||||
boolean hasParentheses = operandCount >= 3 && random.nextBoolean();
|
||||
int parenthesesPosition = hasParentheses ? random.nextInt(operandCount - 1) : -1;
|
||||
|
||||
for (int i = 0; i < operandCount; i++) {
|
||||
if (i > 0) {
|
||||
question.append(" ").append(OPERATORS[random.nextInt(OPERATORS.length)]).append(" ");
|
||||
}
|
||||
|
||||
if (hasParentheses && i == parenthesesPosition) {
|
||||
question.append("(");
|
||||
}
|
||||
|
||||
question.append(random.nextInt(100) + 1); // 1-100
|
||||
|
||||
if (hasParentheses && i == parenthesesPosition + 1) {
|
||||
question.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
return question.toString() + " = ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidQuestion(String question) {
|
||||
return question.matches(".*[+\\-*/].*") &&
|
||||
!question.matches(".*[√²sincostan].*") &&
|
||||
question.contains("=");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
public class Question {
|
||||
private int number;
|
||||
private String content;
|
||||
private String userType;
|
||||
|
||||
public Question(int number, String content, String userType) {
|
||||
this.number = number;
|
||||
this.content = content;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public int getNumber() { return number; }
|
||||
public String getContent() { return content; }
|
||||
public String getUserType() { return userType; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return number + ". " + content;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
public interface QuestionGenerator {
|
||||
String generateQuestion();
|
||||
boolean isValidQuestion(String question);
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
public class QuestionGeneratorFactory {
|
||||
public static QuestionGenerator createGenerator(UserType userType) {
|
||||
switch (userType) {
|
||||
case PRIMARY:
|
||||
return new PrimaryQuestionGenerator();
|
||||
case JUNIOR:
|
||||
return new JuniorQuestionGenerator();
|
||||
case SENIOR:
|
||||
return new SeniorQuestionGenerator();
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的题目类型: " + userType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class SeniorQuestionGenerator implements QuestionGenerator {
|
||||
private Random random = new Random();
|
||||
private final String[] OPERATORS = {"+", "-", "*", "/"};
|
||||
private final String[] TRIG_FUNCTIONS = {"sin", "cos", "tan"};
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
int operandCount = random.nextInt(3) + 2; // 2-4个操作数
|
||||
StringBuilder question = new StringBuilder();
|
||||
|
||||
// 确保至少有一个三角函数
|
||||
boolean hasTrigFunction = false;
|
||||
int trigFunctionPosition = random.nextInt(operandCount);
|
||||
|
||||
for (int i = 0; i < operandCount; i++) {
|
||||
if (i > 0) {
|
||||
question.append(" ").append(OPERATORS[random.nextInt(OPERATORS.length)]).append(" ");
|
||||
}
|
||||
|
||||
// 在特定位置添加三角函数
|
||||
if (i == trigFunctionPosition || !hasTrigFunction) {
|
||||
String trigFunction = TRIG_FUNCTIONS[random.nextInt(TRIG_FUNCTIONS.length)];
|
||||
int angle = random.nextInt(360); // 0-359度
|
||||
question.append(trigFunction).append("(").append(angle).append("°)");
|
||||
hasTrigFunction = true;
|
||||
} else {
|
||||
question.append(random.nextInt(100) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return question.toString() + " = ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidQuestion(String question) {
|
||||
return (question.contains("sin") || question.contains("cos") || question.contains("tan")) &&
|
||||
question.contains("=");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
public class User {
|
||||
private String username;
|
||||
private String password;
|
||||
private UserType userType;
|
||||
|
||||
public User(String username, String password, UserType userType) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public String getUsername() { return username; }
|
||||
public String getPassword() { return password; }
|
||||
public UserType getUserType() { return userType; }
|
||||
|
||||
public void setUserType(UserType userType) {
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return username + " (" + userType.getDisplayName() + ")";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
public enum UserType {
|
||||
PRIMARY("小学"),
|
||||
JUNIOR("初中"),
|
||||
SENIOR("高中");
|
||||
|
||||
private final String displayName;
|
||||
|
||||
UserType(String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public static UserType fromDisplayName(String displayName) {
|
||||
for (UserType type : values()) {
|
||||
if (type.displayName.equals(displayName)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue