You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MVC/StudentConsoleView.java

114 lines
3.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.studentmanagement.view;
import com.studentmanagement.model.Student;
import java.util.List;
import java.util.Map;
/**
* 学生控制台视图类
* 实现StudentView接口提供基于控制台的学生信息显示
*/
public class StudentConsoleView implements StudentView {
@Override
public void displayStudentList(List<Student> students) {
System.out.println("\n===== 学生列表 =====");
if (students.isEmpty()) {
System.out.println("当前没有学生记录");
} else {
System.out.printf("%-10s %-10s %-5s %-5s %-10s\n", "学号", "姓名", "年龄", "性别", "平均成绩");
System.out.println("------------------------------------------");
for (Student student : students) {
System.out.printf("%-10s %-10s %-5d %-5s %-10.2f\n",
student.getId(),
student.getName(),
student.getAge(),
student.getGender(),
student.calculateAverageGrade());
}
}
displaySeparator();
}
@Override
public void displayStudentDetails(Student student) {
System.out.println("\n===== 学生详细信息 =====");
System.out.println("学号: " + student.getId());
System.out.println("姓名: " + student.getName());
System.out.println("年龄: " + student.getAge());
System.out.println("性别: " + student.getGender());
System.out.println("课程成绩:");
if (student.getCourseGrades().isEmpty()) {
System.out.println(" 暂无成绩记录");
} else {
for (Map.Entry<String, Double> entry : student.getCourseGrades().entrySet()) {
System.out.printf(" %-10s: %.2f\n", entry.getKey(), entry.getValue());
}
System.out.println("平均成绩: " + String.format("%.2f", student.calculateAverageGrade()));
}
displaySeparator();
}
@Override
public void displayFilteredStudents(List<Student> students, String filterCriteria) {
System.out.println("\n===== 筛选结果 =====");
System.out.println("筛选条件: " + filterCriteria);
displayStudentList(students);
}
@Override
public void displaySuccessMessage(String message) {
System.out.println("[成功] " + message);
}
@Override
public void displayErrorMessage(String message) {
System.out.println("[错误] " + message);
}
@Override
public void displayStatistics(String statistics) {
System.out.println("\n===== 统计信息 =====");
System.out.println(statistics);
displaySeparator();
}
@Override
public void displayMainMenu() {
displaySeparator();
System.out.println("学生管理系统");
displaySeparator();
System.out.println("1. 添加学生");
System.out.println("2. 查看所有学生");
System.out.println("3. 根据ID查找学生");
System.out.println("4. 更新学生信息");
System.out.println("5. 删除学生");
System.out.println("6. 根据课程筛选学生");
System.out.println("7. 根据成绩范围筛选学生");
System.out.println("8. 计算课程平均成绩");
System.out.println("9. 计算学生平均年龄");
System.out.println(0 + ". 退出系统");
displaySeparator();
System.out.print("请选择操作: ");
}
/**
* 显示分隔线
*/
private void displaySeparator() {
System.out.println("------------------------------------------");
}
/**
* 显示等待提示
*/
public void waitForEnter() {
System.out.println("\n按Enter键继续...");
try {
System.in.read();
} catch (Exception e) {
// 忽略异常
}
}
}