You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.7 KiB

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;
}
}
}