Compare commits
11 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
f4ba30bca1 | 5 months ago |
|
|
5f2a62139f | 5 months ago |
|
|
f60bffb305 | 5 months ago |
|
|
0b3d1716b6 | 5 months ago |
|
|
1e223f1c54 | 5 months ago |
|
|
23e8359e3a | 5 months ago |
|
|
d4ed7e9341 | 5 months ago |
|
|
33f6372d98 | 5 months ago |
|
|
760aa40348 | 5 months ago |
|
|
0bfeaea308 | 5 months ago |
|
|
6f60465022 | 5 months ago |
@ -1,13 +1,86 @@
|
||||
package Base;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Email_settings {
|
||||
public static final String SMTP_HOST = "smtp.qq.com";
|
||||
// 邮箱服务器端口
|
||||
public static final String SMTP_PORT = "587";
|
||||
// 发件人邮箱
|
||||
public static final String FROM_EMAIL = "835981889@qq.com";
|
||||
// 发件人邮箱授权码
|
||||
public static final String EMAIL_PASSWORD = "fpqfprqznbvdbcdf";
|
||||
// 是否启用SSL
|
||||
public static final boolean SSL_ENABLE = true;
|
||||
private static final String CONFIG_FILE = "config/email.properties";
|
||||
private static Properties properties;
|
||||
private static long lastModified = 0;
|
||||
|
||||
static {
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
private static void loadConfig() {
|
||||
properties = new Properties();
|
||||
setDefaultProperties();
|
||||
File configFile = new File(CONFIG_FILE);
|
||||
if (configFile.exists()) {
|
||||
try (FileInputStream input = new FileInputStream(configFile)) {
|
||||
properties.load(input);
|
||||
lastModified = configFile.lastModified();
|
||||
} catch (IOException e) {
|
||||
System.err.println("加载邮箱配置文件失败,使用默认配置: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
createDefaultConfigFile();
|
||||
}
|
||||
}
|
||||
|
||||
private static void setDefaultProperties() {
|
||||
properties.setProperty("smtp.host", "smtp.qq.com");
|
||||
properties.setProperty("smtp.port", "587");
|
||||
properties.setProperty("from.email", "835981889@qq.com");
|
||||
properties.setProperty("email.password", "fpqfprqznbvdbcdf");
|
||||
properties.setProperty("ssl.enable", "true");
|
||||
}
|
||||
|
||||
private static void createDefaultConfigFile() {
|
||||
File configDir = new File("config");
|
||||
if (!configDir.exists()) {
|
||||
configDir.mkdirs();
|
||||
}
|
||||
|
||||
try (FileOutputStream output = new FileOutputStream(CONFIG_FILE)) {
|
||||
properties.store(output, "Settings");
|
||||
} catch (IOException e) {
|
||||
System.err.println("创建默认配置文件失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkForUpdates() {
|
||||
File configFile = new File(CONFIG_FILE);
|
||||
if (configFile.exists() && configFile.lastModified() > lastModified) {
|
||||
loadConfig();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getSmtpHost() {
|
||||
checkForUpdates();
|
||||
return properties.getProperty("smtp.host");
|
||||
}
|
||||
|
||||
public static String getSmtpPort() {
|
||||
checkForUpdates();
|
||||
return properties.getProperty("smtp.port");
|
||||
}
|
||||
|
||||
public static String getFromEmail() {
|
||||
checkForUpdates();
|
||||
return properties.getProperty("from.email");
|
||||
}
|
||||
|
||||
public static String getEmailPassword() {
|
||||
checkForUpdates();
|
||||
return properties.getProperty("email.password");
|
||||
}
|
||||
|
||||
public static boolean isSslEnable() {
|
||||
checkForUpdates();
|
||||
return Boolean.parseBoolean(properties.getProperty("ssl.enable"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
package Base;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class Exam_result {
|
||||
private String exam_type;
|
||||
private int total_questions;
|
||||
private int correct_answers;
|
||||
private double score;
|
||||
private long duration; // 考试时长(秒)
|
||||
private List<Integer> wrong_questions; // 错题索引
|
||||
|
||||
public Exam_result( String examType, int total,
|
||||
int correct, double score, long duration,
|
||||
List<Integer> wrong) {
|
||||
this.exam_type = examType;
|
||||
this.total_questions = total;
|
||||
this.correct_answers = correct;
|
||||
this.score = Math.round(score * 100.0) / 100.0;
|
||||
this.duration = duration;
|
||||
this.wrong_questions = wrong;
|
||||
}
|
||||
|
||||
public String getExamType() { return exam_type; }
|
||||
public int getTotalQuestions() { return total_questions; }
|
||||
public int getCorrectAnswers() { return correct_answers; }
|
||||
public double getScore() { return score; }
|
||||
public long getDuration() { return duration; }
|
||||
public List<Integer> getWrongQuestions() { return wrong_questions; }
|
||||
|
||||
public String get_time() {
|
||||
long minutes = duration / 60;
|
||||
long seconds = duration % 60;
|
||||
return String.format("%d分%d秒", minutes, seconds);
|
||||
}
|
||||
|
||||
public String getCorrectRate() {
|
||||
return String.format("%.1f%%", (double) correct_answers / total_questions * 100);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package Service;
|
||||
|
||||
import Base.Question;
|
||||
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class Deal_file {
|
||||
private static final String BASE_DIR="试卷/";
|
||||
|
||||
public Deal_file() {
|
||||
File baseDir = new File(BASE_DIR);
|
||||
if (!baseDir.exists()) {
|
||||
if (!baseDir.mkdirs())
|
||||
System.out.println("目录创建失败!");
|
||||
}
|
||||
}
|
||||
|
||||
public void savePaper(ArrayList<Question> paper, String username) {
|
||||
String userDirPath = BASE_DIR + username + "/";
|
||||
File userDir = new File(userDirPath);
|
||||
if (!userDir.exists()) {
|
||||
if (!userDir.mkdirs()) {
|
||||
System.out.println("目录创建失败!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
|
||||
String filePath = userDirPath + timestamp + ".txt";
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
|
||||
for (int i = 0; i < paper.size(); i++) {
|
||||
writer.println(paper.get(i).toString());
|
||||
if (i < paper.size() - 1) {
|
||||
writer.println();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("保存文件出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
package Service;
|
||||
|
||||
import Base.Exam_result;
|
||||
import Base.Question;
|
||||
import Generator.Generate_paper;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Exam_service {
|
||||
private String id;
|
||||
private String type;
|
||||
private ArrayList<Question> paper;
|
||||
private int now_index;
|
||||
private Date start_time;
|
||||
private Date end_time;
|
||||
private Map<Integer, Integer> user_answers;
|
||||
|
||||
public Exam_service(int num,String type,String id){
|
||||
this.id=id;
|
||||
this.type=type;
|
||||
paper= Generate_paper.g_paper(num,type,id);
|
||||
now_index=0;
|
||||
this.start_time = new Date();
|
||||
this.user_answers = new HashMap<>();
|
||||
}
|
||||
|
||||
public ArrayList<Question> get_paper() { return paper; }
|
||||
public int get_now_index() { return now_index; }
|
||||
public Date get_start_time() { return start_time; }
|
||||
public Date get_end_time() { return end_time; }
|
||||
public Map<Integer, Integer> get_user_answers() {return user_answers;}
|
||||
|
||||
public void set_now_index(int i){
|
||||
if (i==1 && now_index<paper.size()-1){
|
||||
now_index+=i;
|
||||
}
|
||||
else if (i==-1 && now_index>0){
|
||||
now_index+=i;
|
||||
}
|
||||
else if (i==0){
|
||||
now_index=0;
|
||||
}
|
||||
}
|
||||
|
||||
public Question get_now_question() {
|
||||
if (now_index < paper.size()) {
|
||||
return paper.get(now_index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean next_one(int answer_index) {
|
||||
if (now_index < paper.size()) {
|
||||
user_answers.put(now_index, answer_index);
|
||||
|
||||
if (now_index < paper.size() - 1) {
|
||||
now_index++;
|
||||
return true;
|
||||
} else {
|
||||
end_time = new Date();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean pre_one() {
|
||||
if (now_index > 0) {
|
||||
now_index--;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean change_answer(int index,int choice){
|
||||
if (user_answers.containsKey(index)){
|
||||
user_answers.put(index,choice);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Integer get_user_answer(int question_index) {
|
||||
return user_answers.get(question_index);
|
||||
}
|
||||
|
||||
public boolean check_finished(){
|
||||
for (int i=0;i< paper.size();i++){
|
||||
if (user_answers.get(i)==-1){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Exam_result calculate_result(){
|
||||
int correct = 0;
|
||||
List<Integer> wrong = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < paper.size(); i++) {
|
||||
Integer userAnswer = user_answers.get(i);
|
||||
if (userAnswer != null && paper.get(i).getOptions(userAnswer).equals(paper.get(i).getAnswer())) {
|
||||
correct++;
|
||||
} else {
|
||||
wrong.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
double score = (double) correct / paper.size() * 100;
|
||||
long duration = (end_time.getTime() - start_time.getTime()) / 1000; // 秒
|
||||
|
||||
return new Exam_result(type, paper.size(), correct,
|
||||
score, duration, wrong);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue