|
|
|
@ -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<Student> 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; // 无效输入
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|