From 0bfeaea308dfc0d6621a16bc525ec901a6de33a7 Mon Sep 17 00:00:00 2001 From: Arno <15528622+arno-1@user.noreply.gitee.com> Date: Tue, 7 Oct 2025 13:28:31 +0800 Subject: [PATCH 1/2] Second commit --- src/Base/Exam_result.java | 42 +++++++++++++++ src/Main.java | 25 +++++++++ src/Send_Email/Deal_i_code.java | 4 +- src/Send_Email/Send_email.java | 32 ++++-------- src/Service/Exam_service.java | 90 +++++++++++++++++++++++++++++++++ src/Service/Generate_paper.java | 22 ++++++-- src/Service/User_service.java | 67 +++++++++++++++++++++++- 7 files changed, 255 insertions(+), 27 deletions(-) create mode 100644 src/Base/Exam_result.java create mode 100644 src/Main.java create mode 100644 src/Service/Exam_service.java diff --git a/src/Base/Exam_result.java b/src/Base/Exam_result.java new file mode 100644 index 0000000..d41bb86 --- /dev/null +++ b/src/Base/Exam_result.java @@ -0,0 +1,42 @@ +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 wrong_questions; // 错题索引 + + public Exam_result( String examType, int total, + int correct, double score, long duration, + List 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 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); + } + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..6b568bb --- /dev/null +++ b/src/Main.java @@ -0,0 +1,25 @@ +import Base.Question; +import Send_Email.Deal_i_code; +import Send_Email.Send_email; +import Service.Generate_paper; + +import java.util.ArrayList; + +public class Main { + public static void main(String[] args){ + ArrayList q= Generate_paper.g_paper(12,"小学"); + for (int i=0;i<12;i++) { + System.out.println(q.get(i)); + for (int j = 0; j < 4; j++) { + System.out.print(q.get(i).getOptions(j) + " "); + } + System.out.println(); + System.out.println(); + } + /*String email="835981889@qq.com"; + String code= Deal_i_code.generate_code(email); + if (Send_email.judge_email_address(email).equals("邮箱格式正确")){ + Send_email.send_email(email,code); + }*/ + } +} diff --git a/src/Send_Email/Deal_i_code.java b/src/Send_Email/Deal_i_code.java index fd265cb..b3fdb84 100644 --- a/src/Send_Email/Deal_i_code.java +++ b/src/Send_Email/Deal_i_code.java @@ -48,6 +48,8 @@ public class Deal_i_code{ } public static void clean_all_codes(){ - codeMap.clear(); + if (!codeMap.isEmpty()) { + codeMap.clear(); + } } } diff --git a/src/Send_Email/Send_email.java b/src/Send_Email/Send_email.java index 3df6b77..5c63db7 100644 --- a/src/Send_Email/Send_email.java +++ b/src/Send_Email/Send_email.java @@ -9,61 +9,51 @@ import java.util.Properties; import java.util.regex.Pattern; public class Send_email { - public static boolean judge_email_address(String email) { + public static String judge_email_address(String email) { if (email == null || email.trim().isEmpty()) { - System.out.println("邮箱不能为空"); - return false; + return "邮箱不能为空"; } // 去除前后空格 email = email.trim(); // 检查长度 if (email.length() > 254) { // RFC标准规定邮箱最大长度 - System.out.println("邮箱地址过长"); - return false; + return "邮箱地址过长"; } // 检查是否包含@ if (!email.contains("@")) { - System.out.println("邮箱格式错误:缺少@符号"); - return false; + return "邮箱格式错误:缺少@符号"; } // 分割本地部分和域名部分 String[] parts = email.split("@"); if (parts.length != 2) { - System.out.println("邮箱格式错误:只能有一个@符号"); - return false; + return "邮箱格式错误:只能有一个@符号"; } String localPart = parts[0]; String domainPart = parts[1]; // 验证本地部分 if (localPart.isEmpty()) { - System.out.println("邮箱格式错误:@前必须有内容"); - return false; + return "邮箱格式错误:@前必须有内容"; } if (localPart.length() > 64) { - System.out.println("邮箱格式错误:@前内容过长"); - return false; + return "邮箱格式错误:@前内容过长"; } // 验证域名部分 if (domainPart.isEmpty()) { - System.out.println("邮箱格式错误:@后必须有内容"); - return false; + return "邮箱格式错误:@后必须有内容"; } if (!domainPart.contains(".")) { - System.out.println("邮箱格式错误:域名不完整"); - return false; + return "邮箱格式错误:域名不完整"; } // 验证顶级域名 String[] domainParts = domainPart.split("\\."); String topLevelDomain = domainParts[domainParts.length - 1]; if (topLevelDomain.length() < 2) { - System.out.println("邮箱格式错误:顶级域名太短"); - return false; + return "邮箱格式错误:顶级域名太短"; } String emailRegex = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; boolean matches = Pattern.matches(emailRegex, email); - System.out.println(matches ? "" : "邮箱格式错误"); - return matches; + return matches ? "邮箱格式正确" : "邮箱格式错误"; } public static boolean send_email(String toEmail, String Code) { diff --git a/src/Service/Exam_service.java b/src/Service/Exam_service.java new file mode 100644 index 0000000..f46903d --- /dev/null +++ b/src/Service/Exam_service.java @@ -0,0 +1,90 @@ +package Service; + +import Base.Exam_result; +import Base.Question; + +import java.util.*; + +public class Exam_service { + private String type; + private ArrayList paper; + private int now_index; + private Date start_time; + private Date end_time; + private Map user_answers; + + Exam_service(int num,String type){ + this.type=type; + paper=Generate_paper.g_paper(num,type); + now_index=0; + this.start_time = new Date(); + this.user_answers = new HashMap<>(); + } + + public ArrayList 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 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 Exam_result calculate_result(){ + int correct = 0; + List 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); + } +} diff --git a/src/Service/Generate_paper.java b/src/Service/Generate_paper.java index 989a051..9e16f9b 100644 --- a/src/Service/Generate_paper.java +++ b/src/Service/Generate_paper.java @@ -7,6 +7,7 @@ import Generator.Pri_g_ques; import Generator.Sen_g_ques; import java.util.ArrayList; +import java.util.Objects; public class Generate_paper { public static ArrayList g_paper(int num,String type) { @@ -30,10 +31,25 @@ public class Generate_paper { } } for (int i=0;i all,String ques){ + for (Question q:all){ + if (q.getContent().equals(ques)){ + return true; + } + } + return false; + } } diff --git a/src/Service/User_service.java b/src/Service/User_service.java index b012943..777644b 100644 --- a/src/Service/User_service.java +++ b/src/Service/User_service.java @@ -1,6 +1,9 @@ package Service; import Base.User; +import Send_Email.Deal_i_code; +import Send_Email.Send_email; + import java.io.*; import java.util.ArrayList; @@ -14,6 +17,54 @@ public class User_service { } } + public String register(String email){ + Deal_i_code.clean_all_codes(); + if (find_user(email)!=null){ + return "邮箱已被注册"; + } + String judge_result=Send_email.judge_email_address(email); + if (judge_result.equals("邮箱格式正确")){ + String code=Deal_i_code.generate_code(email); + if (!Send_email.send_email(email,code)){ + return "邮件发送失败"; + } + return "验证码已发送"; + } + else{ + return judge_result; + } + } + + public String check_register(String email,String input_code,String pwd){ + if (Deal_i_code.judge_code(email,input_code)){ + if (!User.check_password(pwd)){ + return "密码长6-20位,至少包含大小写字母和数字"; + } + User new_one=new User(email); + new_one.set_password(pwd); + users.add(new_one); + Deal_i_code.clean_all_codes(); + if (!save_users()){ + return "注册失败,请重试"; + } + return "注册成功"; + } + else { + return "验证码不存在或不正确"; + } + } + + public String login(String email,String pwd){ + User u=find_user(email); + if (u==null){ + return "请先注册"; + } + if (u.get_password().equals(pwd)){ + return "登陆成功"; + } + return "密码有误"; + } + public String change_pwd(String email, String oldPassword, String newPassword){ User user=find_user(email); if (user == null) { @@ -35,6 +86,18 @@ public class User_service { return "修改失败"; } + public String logout(String email){ + User user=find_user(email); + if (user!=null) { + if (users.remove(user)) { + if (!save_users()) { + return "注销成功"; + } + } + } + return "注销失败"; + } + public User find_user(String email){ for (User user : users) { if (user.get_email().equals(email)) { @@ -44,7 +107,7 @@ public class User_service { return null; } - public boolean save_users(){ + private boolean save_users(){ File baseDir = new File(base_dir); if (!baseDir.exists()) { if (!baseDir.mkdirs()){ @@ -65,7 +128,7 @@ public class User_service { } } - public boolean load_users(){ + private boolean load_users(){ users=new ArrayList<>(); String file_path=base_dir+"用户信息.txt"; File file=new File(file_path); From 760aa403487853fe8e30d53584d506c347c41005 Mon Sep 17 00:00:00 2001 From: Arno <15528622+arno-1@user.noreply.gitee.com> Date: Tue, 7 Oct 2025 21:46:34 +0800 Subject: [PATCH 2/2] Second commit --- src/Main.java | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 6b568bb..0000000 --- a/src/Main.java +++ /dev/null @@ -1,25 +0,0 @@ -import Base.Question; -import Send_Email.Deal_i_code; -import Send_Email.Send_email; -import Service.Generate_paper; - -import java.util.ArrayList; - -public class Main { - public static void main(String[] args){ - ArrayList q= Generate_paper.g_paper(12,"小学"); - for (int i=0;i<12;i++) { - System.out.println(q.get(i)); - for (int j = 0; j < 4; j++) { - System.out.print(q.get(i).getOptions(j) + " "); - } - System.out.println(); - System.out.println(); - } - /*String email="835981889@qq.com"; - String code= Deal_i_code.generate_code(email); - if (Send_email.judge_email_address(email).equals("邮箱格式正确")){ - Send_email.send_email(email,code); - }*/ - } -}