commit 37bf2553100da3d5b7535408dd742557d8ffc683 Author: KN321136 <1280568951@qq.com> Date: Sun Nov 2 20:48:01 2025 +0800 333 diff --git a/ConsoleStudentView.java b/ConsoleStudentView.java new file mode 100644 index 0000000..0224de7 --- /dev/null +++ b/ConsoleStudentView.java @@ -0,0 +1,101 @@ +package com.student.view; + +import com.student.model.Student; + +import java.util.List; +import java.util.Scanner; + +/** + * 控制台学生视图实现类,负责在命令行界面展示学生信息和获取用户输入 + */ +public class ConsoleStudentView implements StudentView { + private final Scanner scanner; + + /** + * 构造函数,初始化Scanner对象 + */ + public ConsoleStudentView() { + this.scanner = new Scanner(System.in); + } + + @Override + public void displayStudent(Student student) { + if (student != null) { + System.out.println("\n===== 学生信息 ====="); + System.out.println("ID: " + student.getId()); + System.out.println("姓名: " + student.getName()); + System.out.println("年龄: " + student.getAge()); + System.out.println("课程: " + student.getCourse()); + System.out.println("==================\n"); + } else { + System.out.println("未找到该学生信息"); + } + } + + @Override + public void displayAllStudents(List students) { + if (students.isEmpty()) { + System.out.println("当前没有学生信息"); + return; + } + + System.out.println("\n===== 所有学生信息 ====="); + System.out.printf("%-10s %-10s %-5s %-15s\n", "ID", "姓名", "年龄", "课程"); + System.out.println("----------------------------------------"); + for (Student student : students) { + System.out.printf("%-10s %-10s %-5d %-15s\n", + student.getId(), + student.getName(), + student.getAge(), + student.getCourse()); + } + System.out.println("========================================\n"); + } + + @Override + public void displayMessage(String message) { + System.out.println(message); + } + + @Override + public String getStudentIdInput() { + System.out.print("请输入学生ID: "); + return scanner.nextLine(); + } + + @Override + public Student getStudentInput() { + System.out.println("请输入学生信息:"); + System.out.print("ID: "); + String id = scanner.nextLine(); + System.out.print("姓名: "); + String name = scanner.nextLine(); + System.out.print("年龄: "); + int age = Integer.parseInt(scanner.nextLine()); + System.out.print("课程: "); + String course = scanner.nextLine(); + return new Student(id, name, age, course); + } + + @Override + public void displayMenu() { + System.out.println("\n===== 学生管理系统 ====="); + System.out.println("1. 添加学生"); + System.out.println("2. 查看所有学生"); + System.out.println("3. 查找学生"); + System.out.println("4. 更新学生信息"); + System.out.println("5. 删除学生"); + System.out.println("0. 退出系统"); + System.out.println("======================="); + } + + @Override + public int getMenuChoice() { + System.out.print("请选择操作 (0-5): "); + try { + return Integer.parseInt(scanner.nextLine()); + } catch (NumberFormatException e) { + return -1; // 无效输入 + } + } +} \ No newline at end of file diff --git a/ExceptionHandler.java b/ExceptionHandler.java new file mode 100644 index 0000000..b91218e --- /dev/null +++ b/ExceptionHandler.java @@ -0,0 +1,90 @@ +package com.student.utils; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * 异常处理工具类,用于统一处理系统中的异常情况 + * 提供异常信息格式化和日志记录功能 + */ +public class ExceptionHandler { + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + /** + * 处理异常并返回格式化的异常信息 + * @param e 异常对象 + * @return 格式化的异常信息 + */ + public static String handleException(Exception e) { + StringBuilder message = new StringBuilder(); + message.append("[错误] ") + .append(getCurrentTime()) + .append(" - ") + .append(e.getClass().getSimpleName()) + .append(": ") + .append(e.getMessage()) + .append("\n"); + + // 添加异常堆栈信息 + message.append("异常堆栈:\n") + .append(getStackTraceAsString(e)); + + // 记录到控制台 + System.err.println(message.toString()); + + // 这里可以扩展为写入日志文件 + // logToFile(message.toString()); + + return e.getMessage(); // 返回简洁的错误信息给用户 + } + + /** + * 获取当前时间的格式化字符串 + * @return 当前时间字符串 + */ + private static String getCurrentTime() { + return LocalDateTime.now().format(formatter); + } + + /** + * 将异常堆栈转换为字符串 + * @param e 异常对象 + * @return 异常堆栈字符串 + */ + private static String getStackTraceAsString(Exception e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + pw.close(); + return sw.toString(); + } + + /** + * 验证学生ID格式 + * @param id 学生ID + * @return 是否有效 + */ + public static boolean isValidStudentId(String id) { + return id != null && !id.trim().isEmpty(); + } + + /** + * 验证学生姓名格式 + * @param name 学生姓名 + * @return 是否有效 + */ + public static boolean isValidStudentName(String name) { + return name != null && !name.trim().isEmpty() && name.length() <= 20; + } + + /** + * 验证学生年龄 + * @param age 学生年龄 + * @return 是否有效 + */ + public static boolean isValidStudentAge(int age) { + return age >= 1 && age <= 150; + } +} \ No newline at end of file diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..47925c4 --- /dev/null +++ b/Main.java @@ -0,0 +1,35 @@ +package com.student; + +import com.student.controller.StudentController; +import com.student.model.StudentRepository; +import com.student.view.ConsoleStudentView; +import com.student.view.StudentView; + +/** + * 学生管理系统主入口类 + * 负责初始化系统组件并启动应用程序 + */ +public class Main { + /** + * 主方法,应用程序的入口点 + * @param args 命令行参数 + */ + public static void main(String[] args) { + // 创建模型层组件 + StudentRepository repository = StudentRepository.getInstance(); + + // 创建视图层组件 + StudentView view = new ConsoleStudentView(); + + // 创建控制器,连接模型和视图 + StudentController controller = new StudentController(repository, view); + + // 显示欢迎信息 + view.displayMessage("=================================="); + view.displayMessage(" 欢迎使用学生管理系统"); + view.displayMessage("=================================="); + + // 启动控制器,开始处理用户交互 + controller.start(); + } +} \ No newline at end of file diff --git a/Student.java b/Student.java new file mode 100644 index 0000000..fdb2779 --- /dev/null +++ b/Student.java @@ -0,0 +1,68 @@ +package com.student.model; + +/** + * 学生实体类,代表系统中的数据模型 + */ +public class Student { + private String id; // 学生ID + private String name; // 学生姓名 + private int age; // 学生年龄 + private String course; // 学生课程 + + /** + * 构造函数 + * @param id 学生ID + * @param name 学生姓名 + * @param age 学生年龄 + * @param course 学生课程 + */ + public Student(String id, String name, int age, String course) { + this.id = id; + this.name = name; + this.age = age; + this.course = course; + } + + // Getter和Setter方法 + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getCourse() { + return course; + } + + public void setCourse(String course) { + this.course = course; + } + + @Override + public String toString() { + return "Student{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + ", course='" + course + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/StudentController.java b/StudentController.java new file mode 100644 index 0000000..22c81a6 --- /dev/null +++ b/StudentController.java @@ -0,0 +1,138 @@ +package com.student.controller; + +import com.student.model.Student; +import com.student.model.StudentRepository; +import com.student.view.StudentView; + +import java.util.List; + +/** + * 学生控制器类,负责处理业务逻辑,连接模型和视图 + * 实现MVC架构中的控制器角色 + */ +public class StudentController { + private final StudentRepository repository; + private final StudentView view; + + /** + * 构造函数 + * @param repository 学生数据仓库 + * @param view 学生视图 + */ + public StudentController(StudentRepository repository, StudentView view) { + this.repository = repository; + this.view = view; + } + + /** + * 启动控制器,处理用户交互 + */ + public void start() { + boolean running = true; + while (running) { + view.displayMenu(); + int choice = view.getMenuChoice(); + + switch (choice) { + case 1: + addStudent(); + break; + case 2: + displayAllStudents(); + break; + case 3: + findStudent(); + break; + case 4: + updateStudent(); + break; + case 5: + deleteStudent(); + break; + case 0: + running = false; + view.displayMessage("感谢使用学生管理系统,再见!"); + break; + default: + view.displayMessage("无效的选择,请重新输入"); + } + } + } + + /** + * 添加学生 + */ + private void addStudent() { + Student student = view.getStudentInput(); + // 检查学生ID是否已存在 + if (repository.getStudentById(student.getId()) != null) { + view.displayMessage("该学生ID已存在,添加失败"); + return; + } + repository.addStudent(student); + view.displayMessage("学生添加成功"); + } + + /** + * 显示所有学生 + */ + private void displayAllStudents() { + List students = repository.getAllStudents(); + view.displayAllStudents(students); + } + + /** + * 查找学生 + */ + private void findStudent() { + String id = view.getStudentIdInput(); + Student student = repository.getStudentById(id); + view.displayStudent(student); + } + + /** + * 更新学生信息 + */ + private void updateStudent() { + String id = view.getStudentIdInput(); + Student existingStudent = repository.getStudentById(id); + + if (existingStudent == null) { + view.displayMessage("未找到该学生,更新失败"); + return; + } + + // 显示当前学生信息 + view.displayStudent(existingStudent); + view.displayMessage("请输入更新后的学生信息:"); + + // 获取更新后的信息 + Student updatedStudent = view.getStudentInput(); + // 确保ID保持一致 + updatedStudent.setId(id); + + if (repository.updateStudent(updatedStudent)) { + view.displayMessage("学生信息更新成功"); + } else { + view.displayMessage("学生信息更新失败"); + } + } + + /** + * 删除学生 + */ + private void deleteStudent() { + String id = view.getStudentIdInput(); + + if (repository.getStudentById(id) == null) { + view.displayMessage("未找到该学生,删除失败"); + return; + } + + if (repository.deleteStudent(id)) { + view.displayMessage("学生删除成功"); + } else { + view.displayMessage("学生删除失败"); + } + } +} \ No newline at end of file diff --git a/StudentRepository.java b/StudentRepository.java new file mode 100644 index 0000000..8fd2921 --- /dev/null +++ b/StudentRepository.java @@ -0,0 +1,97 @@ +package com.student.model; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 学生数据仓库类,负责学生数据的存储、检索、更新和删除 + * 使用单例模式确保系统中只有一个数据仓库实例 + */ +public class StudentRepository { + // 使用Map存储学生数据,以ID为键 + private final Map students; + // 单例实例 + private static volatile StudentRepository instance; + + /** + * 私有构造函数,防止外部实例化 + */ + private StudentRepository() { + students = new HashMap<>(); + // 初始化一些示例数据 + addSampleData(); + } + + /** + * 获取单例实例 + * @return StudentRepository实例 + */ + public static StudentRepository getInstance() { + if (instance == null) { + synchronized (StudentRepository.class) { + if (instance == null) { + instance = new StudentRepository(); + } + } + } + return instance; + } + + /** + * 添加示例数据 + */ + private void addSampleData() { + addStudent(new Student("1", "张三", 20, "计算机科学")); + addStudent(new Student("2", "李四", 21, "软件工程")); + addStudent(new Student("3", "王五", 19, "数据结构")); + } + + /** + * 添加学生 + * @param student 学生对象 + */ + public void addStudent(Student student) { + students.put(student.getId(), student); + } + + /** + * 根据ID获取学生 + * @param id 学生ID + * @return 学生对象,如果不存在则返回null + */ + public Student getStudentById(String id) { + return students.get(id); + } + + /** + * 获取所有学生 + * @return 学生列表 + */ + public List getAllStudents() { + return new ArrayList<>(students.values()); + } + + /** + * 更新学生信息 + * @param student 更新后的学生对象 + * @return 是否更新成功 + */ + public boolean updateStudent(Student student) { + if (students.containsKey(student.getId())) { + students.put(student.getId(), student); + return true; + } + return false; + } + + /** + * 删除学生 + * @param id 学生ID + * @return 是否删除成功 + */ + public boolean deleteStudent(String id) { + return students.remove(id) != null; + } +} \ No newline at end of file diff --git a/StudentView.java b/StudentView.java new file mode 100644 index 0000000..11180f3 --- /dev/null +++ b/StudentView.java @@ -0,0 +1,52 @@ +package com.student.view; + +import com.student.model.Student; + +import java.util.List; + +/** + * 学生视图接口,定义了学生信息展示的基本方法 + * 遵循接口隔离原则,确保视图只负责数据展示 + */ +public interface StudentView { + /** + * 显示学生信息 + * @param student 学生对象 + */ + void displayStudent(Student student); + + /** + * 显示所有学生信息 + * @param students 学生列表 + */ + void displayAllStudents(List students); + + /** + * 显示消息 + * @param message 消息内容 + */ + void displayMessage(String message); + + /** + * 获取用户输入的学生ID + * @return 学生ID + */ + String getStudentIdInput(); + + /** + * 获取用户输入的学生信息 + * @return 学生对象 + */ + Student getStudentInput(); + + /** + * 显示菜单选项 + */ + void displayMenu(); + + /** + * 获取用户选择的菜单选项 + * @return 菜单选项 + */ + int getMenuChoice(); +} \ No newline at end of file