|
|
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<Student> 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("学生删除失败");
|
|
|
}
|
|
|
}
|
|
|
} |