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.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import java.io.IOException ;
import java.util.* ;
/**
* 题目生成器类
*/
public class QuestionGenerator {
private User user ;
private String role ;
public QuestionGenerator ( User user , String role ) {
this . user = user ;
this . role = role ;
}
public void generateQuestions ( int questionCount ) throws IOException {
// 获取策略
QuestionStrategy strategy = QuestionStrategyFactory . getStrategy ( role ) ;
// 读出该用户所有历史题目
Set < String > existingQuestions = FileUtils . loadExistingQuestions ( user . getUsername ( ) ) ;
Set < String > newQuestionsSet = new HashSet < > ( ) ;
List < MathQuestion > questions = new ArrayList < > ( ) ;
System . out . println ( "以下为生成的题目列表:\n" ) ;
int i = 1 ;
int tryCount = 0 ;
while ( questions . size ( ) < questionCount ) {
String questionText = strategy . generateQuestion ( ) ;
tryCount + + ;
if ( existingQuestions . contains ( questionText ) | | newQuestionsSet . contains ( questionText ) ) {
// 如果题库太小可能死循环,这里设置最大尝试次数
if ( tryCount > questionCount * 100 ) {
System . out . println ( "题库不足,无法生成足够不重复的题目" ) ;
break ;
}
continue ;
}
newQuestionsSet . add ( questionText ) ;
MathQuestion question = new MathQuestion ( i , questionText ) ;
questions . add ( question ) ;
System . out . println ( "题号 " + i + ": " + questionText + "\n" ) ;
i + + ;
}
String fileName = FileUtils . generateFileName ( ) ;
FileUtils . saveQuestionsToFile ( user . getUsername ( ) , fileName , questions ) ;
}
}