|
|
// 学生控制器 - Controller层
|
|
|
import java.util.List;
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
public class StudentController {
|
|
|
private StudentModel model;
|
|
|
private StudentView view;
|
|
|
private Scanner scanner;
|
|
|
|
|
|
public StudentController(StudentModel model, StudentView view) {
|
|
|
this.model = model;
|
|
|
this.view = view;
|
|
|
this.scanner = new Scanner(System.in);
|
|
|
}
|
|
|
|
|
|
// 启动控制器
|
|
|
public void start() {
|
|
|
boolean running = true;
|
|
|
while (running) {
|
|
|
view.displayMenu();
|
|
|
String choice = scanner.nextLine();
|
|
|
|
|
|
switch (choice) {
|
|
|
case "1":
|
|
|
addStudent();
|
|
|
break;
|
|
|
case "2":
|
|
|
deleteStudent();
|
|
|
break;
|
|
|
case "3":
|
|
|
updateStudent();
|
|
|
break;
|
|
|
case "4":
|
|
|
findStudent();
|
|
|
break;
|
|
|
case "5":
|
|
|
displayAllStudents();
|
|
|
break;
|
|
|
case "6":
|
|
|
running = false;
|
|
|
view.displayMessage("感谢使用学生管理系统!");
|
|
|
break;
|
|
|
default:
|
|
|
view.displayMessage("无效的选择,请重新输入!");
|
|
|
}
|
|
|
}
|
|
|
scanner.close();
|
|
|
}
|
|
|
|
|
|
// 添加学生
|
|
|
private void addStudent() {
|
|
|
System.out.print("请输入学生ID: ");
|
|
|
String id = scanner.nextLine();
|
|
|
System.out.print("请输入学生姓名: ");
|
|
|
String name = scanner.nextLine();
|
|
|
System.out.print("请输入学生年龄: ");
|
|
|
String age = scanner.nextLine();
|
|
|
System.out.print("请输入学生课程: ");
|
|
|
String course = scanner.nextLine();
|
|
|
|
|
|
Student student = new Student(id, name, age, course);
|
|
|
if (model.addStudent(student)) {
|
|
|
view.displayMessage("学生添加成功!");
|
|
|
} else {
|
|
|
view.displayMessage("学生ID已存在,添加失败!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 删除学生
|
|
|
private void deleteStudent() {
|
|
|
System.out.print("请输入要删除的学生ID: ");
|
|
|
String id = scanner.nextLine();
|
|
|
if (model.deleteStudent(id)) {
|
|
|
view.displayMessage("学生删除成功!");
|
|
|
} else {
|
|
|
view.displayMessage("未找到该学生,删除失败!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新学生信息
|
|
|
private void updateStudent() {
|
|
|
System.out.print("请输入要修改的学生ID: ");
|
|
|
String id = scanner.nextLine();
|
|
|
Student existingStudent = model.findStudent(id);
|
|
|
|
|
|
if (existingStudent == null) {
|
|
|
view.displayMessage("未找到该学生,无法修改!");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
System.out.println("当前学生信息: " + existingStudent);
|
|
|
System.out.print("请输入新姓名 (留空不修改): ");
|
|
|
String name = scanner.nextLine();
|
|
|
System.out.print("请输入新年龄 (留空不修改): ");
|
|
|
String age = scanner.nextLine();
|
|
|
System.out.print("请输入新课程 (留空不修改): ");
|
|
|
String course = scanner.nextLine();
|
|
|
|
|
|
// 保留原值如果用户留空
|
|
|
name = name.isEmpty() ? existingStudent.getName() : name;
|
|
|
age = age.isEmpty() ? existingStudent.getAge() : age;
|
|
|
course = course.isEmpty() ? existingStudent.getCourse() : course;
|
|
|
|
|
|
Student updatedStudent = new Student(id, name, age, course);
|
|
|
if (model.updateStudent(updatedStudent)) {
|
|
|
view.displayMessage("学生信息更新成功!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 查找学生
|
|
|
private void findStudent() {
|
|
|
System.out.print("请输入要查询的学生ID: ");
|
|
|
String id = scanner.nextLine();
|
|
|
Student student = model.findStudent(id);
|
|
|
view.displayStudent(student);
|
|
|
}
|
|
|
|
|
|
// 显示所有学生
|
|
|
private void displayAllStudents() {
|
|
|
List<Student> students = model.getAllStudents();
|
|
|
view.displayAllStudents(students);
|
|
|
}
|
|
|
} |