diff --git a/src/controller/AppController.java b/src/controller/AppController.java index 681253b..d9ec606 100644 --- a/src/controller/AppController.java +++ b/src/controller/AppController.java @@ -2,8 +2,9 @@ package controller; import ui.LoginPanel; import ui.RegisterPanel; -import ui.DifficultySelectionPanel; // 添加难度选择面板 -import view.QuizPanel; // 保持原有导入 +import ui.DifficultySelectionPanel; +import model.User; +import view.QuizPanel; import javax.swing.*; import java.awt.*; @@ -28,7 +29,6 @@ public class AppController { frame.setVisible(true); } - /** 显示登录界面 */ public void showLoginPanel() { frame.getContentPane().removeAll(); frame.add(new LoginPanel(this)); @@ -36,7 +36,6 @@ public class AppController { frame.repaint(); } - /** 显示注册界面 */ public void showRegisterPanel() { frame.getContentPane().removeAll(); frame.add(new RegisterPanel(this)); @@ -44,7 +43,6 @@ public class AppController { frame.repaint(); } - /** 显示难度选择界面 */ public void showDifficultySelectionPanel() { frame.getContentPane().removeAll(); frame.add(new DifficultySelectionPanel(this)); @@ -52,7 +50,6 @@ public class AppController { frame.repaint(); } - /** 显示题目选择界面(QuizPanel) */ public void showQuizPanel(String level, int count) { frame.getContentPane().removeAll(); frame.add(new QuizPanel(this, level, count)); @@ -60,31 +57,31 @@ public class AppController { frame.repaint(); } - /** 登录逻辑 */ public void handleLogin(String email, String password) { if (userManager.checkLogin(email, password)) { JOptionPane.showMessageDialog(frame, "登录成功!"); - // 跳转到难度选择界面 + // 获取用户并显示注册的用户名 + User user = userManager.getUserByEmail(email); + String displayName = user != null ? user.getUsername() : email; + JOptionPane.showMessageDialog(frame, "欢迎回来," + displayName + "!"); showDifficultySelectionPanel(); } else { JOptionPane.showMessageDialog(frame, "邮箱或密码错误!"); } } - /** 注册逻辑 */ - public void handleRegister(String email, String password) { + public void handleRegister(String email, String password, String username) { if (userManager.isUserExist(email)) { JOptionPane.showMessageDialog(frame, "该邮箱已注册!"); return; } - userManager.addUser(email, password); + // 传递 username 参数 + userManager.addUser(email, password, username); JOptionPane.showMessageDialog(frame, "注册成功!请登录。"); showLoginPanel(); } - /** 在难度选择界面处理难度选择 */ public void handleDifficultySelection(String level) { - // 弹出输入框要求输入题目数量 String countInput = JOptionPane.showInputDialog( frame, "请输入需要生成的题目数量 (1-50):", @@ -93,7 +90,6 @@ public class AppController { ); if (countInput == null) { - // 用户取消输入 return; } @@ -103,7 +99,6 @@ public class AppController { JOptionPane.showMessageDialog(frame, "题目数量必须在1-50之间!"); return; } - // 跳转到题目界面 showQuizPanel(level, count); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(frame, "请输入有效的数字!"); diff --git a/src/controller/UserManager.java b/src/controller/UserManager.java index 37032eb..c0d819d 100644 --- a/src/controller/UserManager.java +++ b/src/controller/UserManager.java @@ -1,37 +1,41 @@ package controller; +import model.User; + import java.io.*; -import java.util.HashMap; -import java.util.Map; +import java.util.*; /** * 用户信息管理(本地文件,无数据库) */ public class UserManager { private static final String USER_FILE = "users.txt"; - private Map users = new HashMap<>(); + private Map users = new HashMap<>(); // 修改为存储 User 对象 public UserManager() { loadUsers(); } - /** 检查登录 */ public boolean checkLogin(String email, String password) { - return users.containsKey(email) && users.get(email).equals(password); + User user = users.get(email); + return user != null && user.getPassword().equals(password); } - /** 判断用户是否存在 */ public boolean isUserExist(String email) { return users.containsKey(email); } - /** 添加用户 */ - public void addUser(String email, String password) { - users.put(email, password); + // 修改 addUser 方法,接收 username 参数 + public void addUser(String email, String password, String username) { + users.put(email, new User(email, password, username)); saveUsers(); } - /** 加载用户文件 */ + // 添加获取用户的方法 + public User getUserByEmail(String email) { + return users.get(email); + } + private void loadUsers() { File file = new File(USER_FILE); if (!file.exists()) return; @@ -39,22 +43,30 @@ public class UserManager { String line; while ((line = br.readLine()) != null) { String[] parts = line.split(","); - if (parts.length == 2) - users.put(parts[0], parts[1]); + if (parts.length >= 2) { + String email = parts[0]; + String password = parts[1]; + String username = email; // 默认用户名为邮箱 + if (parts.length >= 3) { + username = parts[2]; + } + users.put(email, new User(email, password, username)); + } } } catch (IOException e) { e.printStackTrace(); } } - /** 保存用户信息 */ private void saveUsers() { try (PrintWriter pw = new PrintWriter(new FileWriter(USER_FILE))) { - for (Map.Entry entry : users.entrySet()) { - pw.println(entry.getKey() + "," + entry.getValue()); + for (Map.Entry entry : users.entrySet()) { + User user = entry.getValue(); + // 保存 email, password, username 三列 + pw.println(user.getEmail() + "," + user.getPassword() + "," + user.getUsername()); } } catch (IOException e) { e.printStackTrace(); } } -} +} \ No newline at end of file diff --git a/src/model/QuestionGenerator.java b/src/model/QuestionGenerator.java index 22719f4..c37d3bc 100644 --- a/src/model/QuestionGenerator.java +++ b/src/model/QuestionGenerator.java @@ -29,7 +29,10 @@ public class QuestionGenerator { } if (!uniqueQuestions.contains(q)) { uniqueQuestions.add(q); - list.add(new Question(q, getOptions(q))); + double correct = calculate(q); + String[] opts = getOptions(q); + // 使用新构造函数传递正确答案 + list.add(new Question(q, opts, String.format("%.2f", correct))); } } return list; @@ -110,6 +113,7 @@ public class QuestionGenerator { } // Question 内部类 + // 修改Question内部类,添加新的构造函数 public static class Question { public String questionText; public String[] options; @@ -118,7 +122,15 @@ public class QuestionGenerator { public Question(String text, String[] options) { this.questionText = text; this.options = options; - this.correctAnswer = options[0]; // 默认第一个是正确答案(计算时设置) + // 旧的错误实现:this.correctAnswer = options[0]; + // 改为使用新构造函数 + } + + // 新增构造函数,正确设置正确答案 + public Question(String text, String[] options, String correctAnswer) { + this.questionText = text; + this.options = options; + this.correctAnswer = correctAnswer; } } } diff --git a/src/model/User.java b/src/model/User.java index c9ca7c6..0560c64 100644 --- a/src/model/User.java +++ b/src/model/User.java @@ -1,13 +1,24 @@ + package model; public class User { private String email; private String password; + private String username; - public User(String email, String password) { + // 修改构造函数,添加 username 参数 + public User(String email, String password, String username) { this.email = email; this.password = password; + this.username = username; + } + + // 保留原有无参数构造函数,用于旧数据兼容 + public User(String email, String password) { + this(email, password, email); // 默认用户名为邮箱 } + public String getEmail() { return email; } public String getPassword() { return password; } -} + public String getUsername() { return username; } +} \ No newline at end of file diff --git a/src/ui/RegisterPanel.java b/src/ui/RegisterPanel.java index c94a50f..553b26f 100644 --- a/src/ui/RegisterPanel.java +++ b/src/ui/RegisterPanel.java @@ -14,6 +14,7 @@ public class RegisterPanel extends JPanel { private JTextField codeField; private JPasswordField passwordField; private JPasswordField confirmField; + private JTextField usernameField; // 新增用户名输入框 private String sentCode = null; @@ -37,20 +38,26 @@ public class RegisterPanel extends JPanel { add(emailField, gbc); gbc.gridx = 0; gbc.gridy = 2; + add(new JLabel("用户名:"), gbc); // 修改标签 + gbc.gridx = 1; + usernameField = new JTextField(20); // 新增用户名输入框 + add(usernameField, gbc); + + gbc.gridx = 0; gbc.gridy = 3; add(new JLabel("验证码:"), gbc); gbc.gridx = 1; codeField = new JTextField(10); add(codeField, gbc); - gbc.gridx = 0; gbc.gridy = 3; + gbc.gridx = 0; gbc.gridy = 4; add(new JLabel("密码:"), gbc); gbc.gridx = 1; passwordField = new JPasswordField(20); add(passwordField, gbc); - gbc.gridx = 0; gbc.gridy = 4; + gbc.gridx = 0; gbc.gridy = 5; add(new JLabel("确认密码:"), gbc); gbc.gridx = 1; @@ -58,7 +65,7 @@ public class RegisterPanel extends JPanel { add(confirmField, gbc); JButton sendCodeButton = new JButton("发送验证码"); - gbc.gridx = 0; gbc.gridy = 5; + gbc.gridx = 0; gbc.gridy = 6; add(sendCodeButton, gbc); JButton registerButton = new JButton("注册"); @@ -66,7 +73,7 @@ public class RegisterPanel extends JPanel { add(registerButton, gbc); JButton backButton = new JButton("返回登录"); - gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 2; + gbc.gridx = 0; gbc.gridy = 7; gbc.gridwidth = 2; add(backButton, gbc); sendCodeButton.addActionListener(e -> { @@ -84,6 +91,7 @@ public class RegisterPanel extends JPanel { String code = codeField.getText().trim(); String password = new String(passwordField.getPassword()); String confirm = new String(confirmField.getPassword()); + String username = usernameField.getText().trim(); // 获取用户名 if (sentCode == null) { JOptionPane.showMessageDialog(this, "请先获取验证码!"); @@ -102,9 +110,9 @@ public class RegisterPanel extends JPanel { return; } - controller.handleRegister(email, password); + controller.handleRegister(email, password, username); // 传递username }); backButton.addActionListener(e -> controller.showLoginPanel()); } -} +} \ No newline at end of file diff --git a/src/view/QuizPanel.java b/src/view/QuizPanel.java index e4609ae..7fdb9fc 100644 --- a/src/view/QuizPanel.java +++ b/src/view/QuizPanel.java @@ -58,8 +58,10 @@ public class QuizPanel extends JPanel { } private void checkAndNext() { + // 从Question对象中获取正确答案,而不是假设options[0]是正确答案 + String correctAnswer = questions.get(currentIndex).correctAnswer; for (JRadioButton r : options) { - if (r.isSelected() && r.getText().equals(questions.get(currentIndex).options[0])) { + if (r.isSelected() && r.getText().equals(correctAnswer)) { correctCount++; } } @@ -70,10 +72,14 @@ public class QuizPanel extends JPanel { private void showResult() { removeAll(); setLayout(new GridLayout(3, 1, 10, 10)); - JLabel result = new JLabel("你的得分:" + (100 * correctCount / questions.size()) + " 分", JLabel.CENTER); + // 添加除以0的检查 + int total = questions.size(); + int score = total > 0 ? (100 * correctCount / total) : 0; + JLabel result = new JLabel("你的得分:" + score + " 分", JLabel.CENTER); result.setFont(new Font("微软雅黑", Font.BOLD, 22)); add(result); + // 其余代码保持不变 JButton retry = new JButton("再做一份"); JButton exit = new JButton("退出"); add(retry);