@ -1,5 +1,40 @@
|
||||
|
||||
import util.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
final private static Scanner scanner = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
|
||||
menu Menu=new menu();
|
||||
while (true) {
|
||||
// 登录
|
||||
System.out.println("1.登录");
|
||||
System.out.println("2.退出");
|
||||
System.out.println("3.修改密码");
|
||||
System.out.println("4.创建账户");
|
||||
|
||||
int num=scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
if(num==1){
|
||||
Menu.Mymenu();
|
||||
}
|
||||
else if (num==2){
|
||||
System.exit(0);
|
||||
}
|
||||
else if(num==3){
|
||||
ModifyPassword.modimenu();
|
||||
}
|
||||
else if(num==4){
|
||||
CreateUser.createuser();
|
||||
}
|
||||
else{
|
||||
System.out.println("输出错误,请重新输入");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
package generator;
|
||||
|
||||
import util.ExpressionUtils;
|
||||
import java.util.*;
|
||||
|
||||
public class HighGenerator implements ProblemGenerator {
|
||||
@Override
|
||||
public List<String> generateProblems(int count) {
|
||||
List<String> problems = new ArrayList<>();
|
||||
for (int i = 1; i <= count; i++) {
|
||||
problems.add(ExpressionUtils.generateHighExpr());
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package generator;
|
||||
|
||||
import util.ExpressionUtils;
|
||||
import java.util.*;
|
||||
|
||||
public class MiddleGenerator implements ProblemGenerator {
|
||||
@Override
|
||||
public List<String> generateProblems(int count) {
|
||||
List<String> problems = new ArrayList<>();
|
||||
for (int i = 1; i <= count; i++) {
|
||||
problems.add(ExpressionUtils.generateMiddleExpr());
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
package generator;
|
||||
|
||||
public class Praimary {
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package generator;
|
||||
|
||||
import util.ExpressionUtils;
|
||||
import java.util.*;
|
||||
|
||||
public class PrimaryGenerator implements ProblemGenerator {
|
||||
@Override
|
||||
public List<String> generateProblems(int count) {
|
||||
List<String> problems = new ArrayList<>(count);
|
||||
for (int i = 1; i <= count; i++) {
|
||||
problems.add( ExpressionUtils.generatePrimaryExpr());
|
||||
}
|
||||
return problems;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package generator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ProblemGenerator {
|
||||
List<String> generateProblems(int count);
|
||||
}
|
||||
|
||||
@ -0,0 +1,63 @@
|
||||
package util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
// 获取用户文件夹路径
|
||||
public static String getUserFolder(String username) {
|
||||
String folder = "exams/" + username; // 存放在项目目录下 exams/
|
||||
File dir = new File(folder);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
return folder;
|
||||
}
|
||||
|
||||
// 读取历史题目(用于查重)
|
||||
public static Set<String> loadHistory(String username) {
|
||||
Set<String> history = new HashSet<>();
|
||||
String folder = getUserFolder(username);
|
||||
|
||||
File dir = new File(folder);
|
||||
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
if (!line.trim().isEmpty()) {
|
||||
history.add(line.trim());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
||||
// 保存新题目
|
||||
public static void saveProblems(String username, List<String> problems) {
|
||||
String folder = getUserFolder(username);
|
||||
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
|
||||
String filename = folder + "/" + timestamp + ".txt";
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
|
||||
int index=1;
|
||||
for (String problem : problems) {
|
||||
bw.write(index+"."+problem);
|
||||
bw.newLine();
|
||||
bw.newLine();
|
||||
index++;
|
||||
}
|
||||
System.out.println("卷子已保存到: " + filename);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,133 @@
|
||||
package util;
|
||||
import auth.*;
|
||||
import generator.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static util.ModifyClass.modifyclass;
|
||||
|
||||
public class menu {
|
||||
static Scanner scanner = new Scanner(System.in);
|
||||
User currentUser = null;
|
||||
ProblemGenerator generator = null;
|
||||
|
||||
public void Mymenu() {
|
||||
while (currentUser==null) {
|
||||
|
||||
System.out.println("请输入用户名和密码(用空格隔开):");
|
||||
String input = scanner.nextLine();
|
||||
String [] parts=input.split(" ");
|
||||
if (parts.length != 2) {
|
||||
System.out.println("格式错误,请重新输入。");
|
||||
continue;
|
||||
}
|
||||
String username = parts[0];
|
||||
String password = parts[1];
|
||||
UserManager userManager = new UserManager();
|
||||
currentUser = userManager.login(username, password);
|
||||
if (currentUser == null) {
|
||||
System.out.println("请输入正确的用户名、密码");
|
||||
} else {
|
||||
System.out.println("当前选择为 " + currentUser.getType() + " 出题");
|
||||
int tmp = menu1(currentUser, generator);
|
||||
if(tmp == 1) {
|
||||
currentUser = null;
|
||||
generator=null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int menu1(User currentUser, ProblemGenerator generator) {
|
||||
int count1 = 0;
|
||||
while (true) {
|
||||
System.out.println("1.生成试卷");
|
||||
System.out.println("2.修改难度");
|
||||
System.out.println("3.退出当前登录");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
if (choice == 1) {
|
||||
int temp= generateExam(currentUser);
|
||||
if(temp== 1) {
|
||||
count1=1;
|
||||
break;
|
||||
}
|
||||
|
||||
} else if (choice == 2) {
|
||||
currentUser = switchDifficulty(currentUser);
|
||||
} else if (choice == 3) {
|
||||
//count=1代表currentuser要退出
|
||||
count1 = 1;
|
||||
break; // 退出
|
||||
}
|
||||
else{
|
||||
System.out.println("输出错误,请重新输入");
|
||||
}
|
||||
}
|
||||
return count1;
|
||||
}
|
||||
|
||||
private static int generateExam(User currentUser) {
|
||||
System.out.println("准备生成 " + currentUser.getType() +
|
||||
" 数学题目,请输入生成题目数量(输入 -1 退出当前用户):");
|
||||
ProblemGenerator generator = getGenerator(currentUser.getType());
|
||||
int count = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
if (count == -1){
|
||||
System.out.println("退出成功");
|
||||
return 1;
|
||||
}
|
||||
if (count < 10 || count > 30) {
|
||||
System.out.println("题目数量必须在 10 到 30 之间");
|
||||
return 2;
|
||||
}
|
||||
|
||||
Set<String> history = FileUtils.loadHistory(currentUser.getUsername());
|
||||
List<String> problems = new ArrayList<>();
|
||||
while (problems.size() < count) {
|
||||
List<String> batch = generator.generateProblems(1);
|
||||
String problem = batch.get(0);
|
||||
if (!history.contains(problem)) {
|
||||
problems.add(problem);
|
||||
history.add(problem);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i <count; i++) {
|
||||
String p = problems.get(i);
|
||||
System.out.println((i+1)+"."+p);
|
||||
}
|
||||
|
||||
FileUtils.saveProblems(currentUser.getUsername(), problems);
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static User switchDifficulty(User currentUser) {
|
||||
System.out.println("正在修改难度中,请输入 切换为小学/初中/高中");
|
||||
String input1 = scanner.next();
|
||||
if (input1.startsWith("切换为")) {
|
||||
String target = input1.replace("切换为", "").trim();
|
||||
if (target.equals("小学") || target.equals("初中") || target.equals("高中")) {
|
||||
currentUser = new User(currentUser.getUsername(),
|
||||
currentUser.getPassword(), target);
|
||||
modifyclass("user.txt", currentUser.getUsername(), target);
|
||||
System.out.println("系统提示:修改难度完成。难度为 " + target + " 难度");
|
||||
} else {
|
||||
System.out.println("请输入小学、初中和高中三个选项中的一个");
|
||||
}
|
||||
} else {
|
||||
System.out.println("输入错误请重试");
|
||||
}
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
public static ProblemGenerator getGenerator(String type) {
|
||||
switch (type) {
|
||||
case "小学": return new PrimaryGenerator();
|
||||
case "初中": return new MiddleGenerator();
|
||||
case "高中": return new HighGenerator();
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue