ADD file via upload

main
pc9pizjb6 4 months ago
parent 2e2cbcef8f
commit 543af56cc2

@ -0,0 +1,196 @@
package com.studentmanagement;
import com.studentmanagement.controller.StudentController;
import com.studentmanagement.model.Student;
import com.studentmanagement.util.InputUtil;
import com.studentmanagement.view.StudentConsoleView;
import com.studentmanagement.view.StudentView;
/**
*
* MVC
*/
public class MainApp {
public static void main(String[] args) {
// 初始化MVC组件
StudentView studentView = new StudentConsoleView();
StudentController studentController = new StudentController(studentView);
// 启动应用
runApplication(studentController, (StudentConsoleView) studentView);
}
/**
*
* @param controller
* @param view
*/
private static void runApplication(StudentController controller, StudentConsoleView view) {
boolean running = true;
while (running) {
// 显示主菜单
view.displayMainMenu();
// 获取用户选择
int choice = InputUtil.getIntInput("");
// 根据选择执行相应操作
switch (choice) {
case 1:
addStudent(controller);
break;
case 2:
controller.displayAllStudents();
break;
case 3:
findStudentById(controller);
break;
case 4:
updateStudent(controller);
break;
case 5:
deleteStudent(controller);
break;
case 6:
filterStudentsByCourse(controller);
break;
case 7:
filterStudentsByGradeRange(controller);
break;
case 8:
calculateAverageGradeByCourse(controller);
break;
case 9:
controller.calculateAverageAge();
break;
case 0:
running = false;
System.out.println("\n感谢使用学生管理系统再见");
break;
default:
System.out.println("无效的选择,请重新输入");
break;
}
// 如果不是退出,等待用户确认继续
if (running && choice != 0) {
InputUtil.waitForEnter();
}
}
}
/**
*
* @param controller
*/
private static void addStudent(StudentController controller) {
String id = InputUtil.getStringInput("请输入学生ID: ");
String name = InputUtil.getStringInput("请输入学生姓名: ");
int age = InputUtil.getIntInput("请输入学生年龄: ");
String gender = InputUtil.getStringInput("请输入学生性别: ");
Student student = new Student(id, name, age, gender);
// 添加课程成绩
boolean addMoreCourses = true;
while (addMoreCourses) {
String course = InputUtil.getStringInput("请输入课程名称 (输入空字符串结束): ");
if (course.isEmpty()) {
addMoreCourses = false;
} else {
double grade = InputUtil.getDoubleInput("请输入该课程成绩: ");
student.addCourseGrade(course, grade);
}
}
controller.addStudent(student);
}
/**
* ID
* @param controller
*/
private static void findStudentById(StudentController controller) {
String id = InputUtil.getStringInput("请输入要查找的学生ID: ");
controller.findStudentById(id);
}
/**
*
* @param controller
*/
private static void updateStudent(StudentController controller) {
String id = InputUtil.getStringInput("请输入要更新的学生ID: ");
// 先查找学生是否存在
try {
// 这里我们直接创建一个新的学生对象来更新
String name = InputUtil.getStringInput("请输入新的学生姓名: ");
int age = InputUtil.getIntInput("请输入新的学生年龄: ");
String gender = InputUtil.getStringInput("请输入新的学生性别: ");
Student updatedStudent = new Student(id, name, age, gender);
// 添加课程成绩
boolean addCourses = InputUtil.getConfirmation("是否添加/更新课程成绩");
if (addCourses) {
boolean addMoreCourses = true;
while (addMoreCourses) {
String course = InputUtil.getStringInput("请输入课程名称 (输入空字符串结束): ");
if (course.isEmpty()) {
addMoreCourses = false;
} else {
double grade = InputUtil.getDoubleInput("请输入该课程成绩: ");
updatedStudent.addCourseGrade(course, grade);
}
}
}
controller.updateStudent(updatedStudent);
} catch (Exception e) {
System.out.println("更新失败: " + e.getMessage());
}
}
/**
*
* @param controller
*/
private static void deleteStudent(StudentController controller) {
String id = InputUtil.getStringInput("请输入要删除的学生ID: ");
if (InputUtil.getConfirmation("确定要删除该学生吗?")) {
controller.deleteStudent(id);
} else {
System.out.println("删除操作已取消");
}
}
/**
*
* @param controller
*/
private static void filterStudentsByCourse(StudentController controller) {
String course = InputUtil.getStringInput("请输入课程名称: ");
controller.filterStudentsByCourse(course);
}
/**
*
* @param controller
*/
private static void filterStudentsByGradeRange(StudentController controller) {
double minGrade = InputUtil.getDoubleInput("请输入最低成绩: ");
double maxGrade = InputUtil.getDoubleInput("请输入最高成绩: ");
controller.filterStudentsByGradeRange(minGrade, maxGrade);
}
/**
*
* @param controller
*/
private static void calculateAverageGradeByCourse(StudentController controller) {
String course = InputUtil.getStringInput("请输入课程名称: ");
controller.calculateAverageGradeByCourse(course);
}
}
Loading…
Cancel
Save