Compare commits
No commits in common. 'main' and 'jinzhibo_branch' have entirely different histories.
main
...
jinzhibo_b
@ -1,3 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: MathLearningApp
|
||||
Class-Path: .
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,32 +0,0 @@
|
||||
import service.QuestionService;
|
||||
import model.Question;
|
||||
import service.OptionsResult;
|
||||
import java.util.List;
|
||||
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== 测试题目生成 ===\n");
|
||||
|
||||
QuestionService questionService = new QuestionService();
|
||||
List<Question> questions = questionService.generateQuestions("高中", 30);
|
||||
|
||||
for (int i = 0; i < questions.size(); i++) {
|
||||
Question question = questions.get(i);
|
||||
System.out.println((i + 1) + ". " + question.getContent());
|
||||
|
||||
// 通过 OptionsResult 获取选项列表
|
||||
OptionsResult optionsResult = question.getOptions();
|
||||
List<String> options = optionsResult.getOptions();
|
||||
|
||||
System.out.print(" 选项: ");
|
||||
for (int j = 0; j < options.size(); j++) {
|
||||
char optionChar = (char) ('A' + j);
|
||||
System.out.print(optionChar + "." + options.get(j) + " ");
|
||||
}
|
||||
|
||||
char correctChar = (char) ('A' + optionsResult.getCorrectIndex());
|
||||
System.out.println(" [答案:" + correctChar + "]");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Exam {
|
||||
private List<Question> questions;
|
||||
private String difficulty;
|
||||
|
||||
public Exam(List<Question> questions, String difficulty) {
|
||||
this.questions = questions;
|
||||
this.difficulty = difficulty;
|
||||
}
|
||||
|
||||
public List<Question> getQuestions() {
|
||||
return questions;
|
||||
}
|
||||
|
||||
public String getDifficulty() {
|
||||
return difficulty;
|
||||
}
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
package model;
|
||||
|
||||
import java.util.List;
|
||||
import service.OptionsResult;
|
||||
|
||||
public class Question {
|
||||
private String content;
|
||||
private OptionsResult options;
|
||||
private int correctPos;
|
||||
private String difficulty;
|
||||
|
||||
public Question(String content, OptionsResult options, int correctPos, String difficulty) {
|
||||
this.content = content;
|
||||
this.options = options;
|
||||
this.correctPos = correctPos;
|
||||
this.difficulty = difficulty;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public OptionsResult getOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
public int getCorrectAnswer() {
|
||||
return correctPos;
|
||||
}
|
||||
|
||||
public String getDifficulty() {
|
||||
return difficulty;
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
package model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class User implements Serializable {
|
||||
private String email;
|
||||
private String password;
|
||||
private String registrationCode;
|
||||
private boolean isRegistered;
|
||||
|
||||
public User(String email, String registrationCode) {
|
||||
this.email = email;
|
||||
this.registrationCode = registrationCode;
|
||||
this.isRegistered = false;
|
||||
this.password = null;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getRegistrationCode() {
|
||||
return registrationCode;
|
||||
}
|
||||
|
||||
public boolean isRegistered() {
|
||||
return isRegistered;
|
||||
}
|
||||
|
||||
public void setRegistered(boolean registered) {
|
||||
isRegistered = registered;
|
||||
}
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
package service;
|
||||
|
||||
import model.Question;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class AbstractQuestionGenerator implements QuestionGenerator {
|
||||
protected String difficulty;
|
||||
protected Random random = new Random();
|
||||
|
||||
public AbstractQuestionGenerator(String difficulty) {
|
||||
this.difficulty = difficulty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OptionsResult generateOptions(int correct) {
|
||||
List<String> options = new ArrayList<>();
|
||||
Set<Integer> used = new HashSet<>();
|
||||
used.add(correct);
|
||||
|
||||
// 先生成3个错误选项
|
||||
while (options.size() < 3) {
|
||||
int wrong = correct + (int)(Math.random() * 10) - 5;
|
||||
if (!used.contains(wrong) && wrong != correct) {
|
||||
options.add(String.valueOf(wrong));
|
||||
used.add(wrong);
|
||||
}
|
||||
}
|
||||
// 在随机位置插入正确答案
|
||||
int correctPosition = (int)(Math.random() * 4); // 0-3的随机位置
|
||||
options.add(correctPosition, String.valueOf(correct));
|
||||
|
||||
return new OptionsResult(options, correctPosition);
|
||||
}
|
||||
|
||||
public String getDifficulty() {
|
||||
return difficulty;
|
||||
}
|
||||
|
||||
protected int getRandomOperand() {
|
||||
return random.nextInt(20)+1;
|
||||
}
|
||||
|
||||
protected String getRandomOperator(String[] operators) {
|
||||
return operators[random.nextInt(operators.length)];
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OptionsResult {
|
||||
private List<String> options;
|
||||
private int correctIndex;
|
||||
|
||||
public OptionsResult(List<String> options, int correctIndex) {
|
||||
this.options = options;
|
||||
this.correctIndex = correctIndex;
|
||||
}
|
||||
|
||||
public List<String> getOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
public int getCorrectIndex() {
|
||||
return correctIndex;
|
||||
}
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
package service;
|
||||
|
||||
import model.Question;
|
||||
import java.util.List;
|
||||
|
||||
public interface QuestionGenerator {
|
||||
Question generateQuestion();
|
||||
OptionsResult generateOptions(int correctAnswer);
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
package service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class QuestionGeneratorFactory {
|
||||
private static final Map<String, QuestionGenerator> generators = new HashMap<>();
|
||||
|
||||
static {
|
||||
generators.put("小学", new PrimaryQuestionGenerator());
|
||||
generators.put("初中", new MiddleSchoolQuestionGenerator());
|
||||
generators.put("高中", new HighSchoolQuestionGenerator());
|
||||
}
|
||||
|
||||
public static QuestionGenerator getGenerator(String difficulty) {
|
||||
return generators.getOrDefault(difficulty, generators.get("小学"));
|
||||
}
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package service;
|
||||
|
||||
import model.Question;
|
||||
import java.util.*;
|
||||
|
||||
public class QuestionService {
|
||||
/*生成试卷的题目部分*/
|
||||
public List<Question> generateQuestions(String difficulty, int count) {
|
||||
if(count < 10 || count > 30) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Question> questions = new ArrayList<>();
|
||||
Set<String> usedQuestions = new HashSet<>();
|
||||
QuestionGenerator generator = QuestionGeneratorFactory.getGenerator(difficulty);
|
||||
|
||||
while (questions.size() < count) {
|
||||
Question q = generator.generateQuestion();
|
||||
if (usedQuestions.add(q.getContent())) {
|
||||
questions.add(q);
|
||||
}
|
||||
}
|
||||
return questions;
|
||||
}
|
||||
}
|
||||
@ -1,61 +0,0 @@
|
||||
package service;
|
||||
|
||||
import model.User;
|
||||
import utils.FileStorage;
|
||||
import utils.PasswordValidator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class UserService {
|
||||
private Map<String, User> users = new HashMap<>();
|
||||
private FileStorage fileStorage = new FileStorage();
|
||||
|
||||
public UserService() {
|
||||
users = fileStorage.loadUsers(); //加载所有已有的用户
|
||||
}
|
||||
|
||||
/*注册用户第一阶段-生成验证码*/
|
||||
public String registerUser(String email) {
|
||||
if (users.containsKey(email) && users.get(email).isRegistered()){
|
||||
return "registered"; //注册过的用户不能再注册
|
||||
//return null;
|
||||
}
|
||||
if (users.containsKey(email)) {
|
||||
return "recheck your email"; //已发过验证码也不再发了
|
||||
//return null;
|
||||
}
|
||||
String code = String.valueOf(100000 + (int)(Math.random() * 900000)); //生成6位注册码
|
||||
users.put(email, new User(email, code));
|
||||
fileStorage.saveUsers(users);
|
||||
return code;
|
||||
}
|
||||
|
||||
/*注册用户第二阶段-完成注册*/
|
||||
public boolean completeRegistration(String email, String code, String password) {
|
||||
User user = users.get(email);
|
||||
if (user != null && user.getRegistrationCode().equals(code) && PasswordValidator.isValid(password)) {
|
||||
user.setPassword(password);
|
||||
user.setRegistered(true);
|
||||
fileStorage.saveUsers(users);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*用户登录*/
|
||||
public boolean login(String email, String password) {
|
||||
User user = users.get(email);
|
||||
return user != null && user.isRegistered() && user.getPassword().equals(password);
|
||||
}
|
||||
|
||||
/*修改密码*/
|
||||
public boolean changePassword(String email, String oldPassword, String newPassword) {
|
||||
User user = users.get(email);
|
||||
if (user != null && user.getPassword().equals(oldPassword) && PasswordValidator.isValid(newPassword)) {
|
||||
user.setPassword(newPassword);
|
||||
fileStorage.saveUsers(users);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package utils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class EmailValidator {
|
||||
private static final Pattern EMAIL_PATTERN = Pattern.compile("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
|
||||
|
||||
/*只检查邮箱的格式是否正确,但不保证这个邮箱存在*/
|
||||
public static boolean isValid(String email) {
|
||||
return email != null && EMAIL_PATTERN.matcher(email).matches();
|
||||
}
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package utils;
|
||||
|
||||
import model.User;
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FileStorage {
|
||||
/*完全擦除原来的文件并重新写入*/
|
||||
public void saveUsers(Map<String, User> users) {
|
||||
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users.dat"))) {
|
||||
oos.writeObject(users);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/*从文件中载入*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, User> loadUsers() {
|
||||
File file = new File("users.dat");
|
||||
if (!file.exists()) return new HashMap<>();
|
||||
|
||||
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users.dat"))) {
|
||||
return (Map<String, User>) ois.readObject();
|
||||
} catch (Exception e) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
package utils;
|
||||
|
||||
public class PasswordValidator {
|
||||
public static boolean isValid(String password) {
|
||||
if (password == null || password.length() < 6 || password.length() > 10) {
|
||||
return false;
|
||||
}
|
||||
boolean hasUpper = false;
|
||||
boolean hasLower = false;
|
||||
boolean hasDigit = false;
|
||||
for (char c : password.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) hasUpper = true;
|
||||
if (Character.isLowerCase(c)) hasLower = true;
|
||||
if (Character.isDigit(c)) hasDigit = true;
|
||||
}
|
||||
return hasUpper && hasLower && hasDigit;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue