|
|
import account.Account;
|
|
|
import account.AccountService;
|
|
|
import account.SimpleAccountService;
|
|
|
import persistence.FileSaver;
|
|
|
import persistence.QuestionChecker;
|
|
|
import question.Question;
|
|
|
import question.QuestionGenerator;
|
|
|
import question.PrimaryQuestionGenerator;
|
|
|
import question.MiddleQuestionGenerator;
|
|
|
import question.HighQuestionGenerator;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
public class Main {
|
|
|
private static final AccountService accountService = new SimpleAccountService();
|
|
|
private static final Scanner scanner = new Scanner(System.in);
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
while (true) {
|
|
|
System.out.println("欢迎来到题目生成系统!");
|
|
|
System.out.println("请输入用户名与密码(空格分隔):");
|
|
|
String[] parts = scanner.nextLine().split(" ");
|
|
|
|
|
|
if (parts.length != 2) {
|
|
|
System.out.println("输入格式错误!请重新输入!");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
Account account = accountService.login(parts[0], parts[1]);
|
|
|
if (account == null) {
|
|
|
System.out.println("登录失败!用户名或密码错误!");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
generator(account.getUserType(), account.getUserName());//登录成功 进入出题循环系统
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 出题循环 出题类型选择器
|
|
|
* @param type
|
|
|
* @param userName
|
|
|
*/
|
|
|
private static void generator(String type, String userName) {
|
|
|
while (true) {
|
|
|
System.out.println("当前类型为 " + type + " ,请选择是否切换类型(1-切换,0-保持):");
|
|
|
String switchInput = scanner.nextLine().trim();
|
|
|
|
|
|
if (switchInput.equals("1")) {
|
|
|
String newType = switchType(type);//出题类型切换器
|
|
|
System.out.println("当前选择为 " + newType + " 出题");
|
|
|
//切换新类型出题
|
|
|
if (!questionLoop(newType, userName)) {
|
|
|
return;
|
|
|
}
|
|
|
} else if (switchInput.equals("0")) {
|
|
|
System.out.println("当前选择为 " + type + " 出题");
|
|
|
//使用原类型出题
|
|
|
if (!questionLoop(type, userName)) {
|
|
|
return;
|
|
|
}
|
|
|
} else {
|
|
|
System.out.println("输入错误,请输入0或1!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 出题类型切换
|
|
|
* @param userType
|
|
|
* @return 新类型
|
|
|
*/
|
|
|
private static String switchType(String userType) {
|
|
|
while (true){
|
|
|
System.out.println("请输入新的类型(小学/初中/高中):");
|
|
|
String newType = scanner.nextLine().trim();
|
|
|
if (!newType.equals("小学") && !newType.equals("初中") && !newType.equals("高中")) {
|
|
|
System.out.println("输入错误!请重新输入!");
|
|
|
continue;
|
|
|
}
|
|
|
return newType;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成题目
|
|
|
* @param userType
|
|
|
* @param username
|
|
|
* @return 是否继续出题
|
|
|
*/
|
|
|
private static boolean questionLoop(String userType, String username) {
|
|
|
while (true) {
|
|
|
System.out.println("请输入生成题目数量(数量限制:10-30,-1返回主页【重新登录】,-2继续出题,-3退出系统):");
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
|
if (input.equals("-1")) {
|
|
|
return false;
|
|
|
} else if (input.equals("-2")) {
|
|
|
return true;
|
|
|
} else if (input.equals("-3")) {
|
|
|
System.exit(0);
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
int count = Integer.parseInt(input);
|
|
|
if (count < 10 || count > 30) {
|
|
|
System.out.println("题目数量必须在10-30之间!");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
generateAndSaveQuestions(userType, username, count);
|
|
|
} catch (NumberFormatException e) {
|
|
|
System.out.println("请输入有效数字!");//防止用户生成非数字时程序崩溃
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成并保存题目
|
|
|
* @param userType
|
|
|
* @param username
|
|
|
* @param count
|
|
|
*/
|
|
|
private static void generateAndSaveQuestions(String userType, String username, int count) {
|
|
|
QuestionGenerator generator = getGenerator(userType);//匹配生成器类型
|
|
|
List<Question> questions = new ArrayList<>();
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
Question q = generator.generateQuestion();//进行相应类型出题
|
|
|
if (!QuestionChecker.isDuplicate(username, q.getQuestion())) {
|
|
|
questions.add(q);//进行查重处理 未重复则加入到题目队列中
|
|
|
} else {
|
|
|
i--;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
FileSaver.saveQuestions(username, questions);//保存题目到文件夹
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("题目保存失败!");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据用户类型匹配对应题目生成器
|
|
|
* @param userType
|
|
|
* @return 相应生成器
|
|
|
*/
|
|
|
private static QuestionGenerator getGenerator(String userType) {
|
|
|
switch (userType) {
|
|
|
case "小学": return new PrimaryQuestionGenerator();
|
|
|
case "初中": return new MiddleQuestionGenerator();
|
|
|
case "高中": return new HighQuestionGenerator();
|
|
|
default: return new PrimaryQuestionGenerator();
|
|
|
}
|
|
|
}
|
|
|
} |