YZY 4 months ago
parent b0405cf978
commit f4cdcf4894

@ -78,6 +78,10 @@
<recent name="E:\项目实战整理\1. JAVA实战项目\2022-01-17 学校社团管理系统\项目代码\server\src\main\resources" /> <recent name="E:\项目实战整理\1. JAVA实战项目\2022-01-17 学校社团管理系统\项目代码\server\src\main\resources" />
<recent name="E:\项目实战整理\1. JAVA实战项目\2022-01-17 学校社团管理系统\项目代码\src\main\java" /> <recent name="E:\项目实战整理\1. JAVA实战项目\2022-01-17 学校社团管理系统\项目代码\src\main\java" />
</key> </key>
<key name="MoveFile.RECENT_KEYS">
<recent name="D:\BaiduNetdiskDownload\springboot和vue社团管理\server\src\main\java\self\cases\teams" />
<recent name="D:\BaiduNetdiskDownload\springboot和vue社团管理\server\src\main" />
</key>
</component> </component>
<component name="RunManager"> <component name="RunManager">
<configuration name="Application" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true"> <configuration name="Application" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
@ -140,6 +144,7 @@
<workItem from="1744097186572" duration="686000" /> <workItem from="1744097186572" duration="686000" />
<workItem from="1744187732630" duration="3771000" /> <workItem from="1744187732630" duration="3771000" />
<workItem from="1744197535410" duration="1873000" /> <workItem from="1744197535410" duration="1873000" />
<workItem from="1744199899989" duration="1551000" />
</task> </task>
<servers /> <servers />
</component> </component>

@ -0,0 +1,351 @@
package self.cases.teams;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
// 学生类
class Student implements Serializable {
private String id;
private String name;
private int age;
private String className;
private Date enrollDate;
private Map<String, Double> scores;
public Student(String id, String name, int age, String className) {
this.id = id;
this.name = name;
this.age = age;
this.className = className;
this.enrollDate = new Date();
this.scores = new HashMap<>();
}
// Getter和Setter方法
public String getId() { return id; }
public String getName() { return name; }
public int getAge() { return age; }
public String getClassName() { return className; }
public Date getEnrollDate() { return enrollDate; }
public Map<String, Double> getScores() { return scores; }
public void updateScore(String course, double score) {
scores.put(course, score);
}
public double calculateAverage() {
return scores.values().stream()
.mapToDouble(Double::doubleValue)
.average()
.orElse(0.0);
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return String.format("学号: %s\n姓名: %s\n年龄: %d\n班级: %s\n入学日期: %s",
id, name, age, className, sdf.format(enrollDate));
}
}
// 课程管理类
class CourseManager {
private static final List<String> AVAILABLE_COURSES = Arrays.asList(
"数学", "语文", "英语", "物理", "化学"
);
public static boolean isValidCourse(String course) {
return AVAILABLE_COURSES.contains(course);
}
public static void showAvailableCourses() {
System.out.println("可选课程列表:");
AVAILABLE_COURSES.forEach(c -> System.out.print(c + " "));
System.out.println();
}
}
// 文件工具类
class FileUtil {
private static final String DATA_FILE = "students.dat";
public static void saveData(List<Student> students) {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(DATA_FILE))) {
oos.writeObject(students);
} catch (IOException e) {
System.err.println("数据保存失败: " + e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static List<Student> loadData() {
File file = new File(DATA_FILE);
if (!file.exists()) return new ArrayList<>();
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(DATA_FILE))) {
return (List<Student>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
System.err.println("数据加载失败: " + e.getMessage());
return new ArrayList<>();
}
}
}
// 输入工具类
class InputUtil {
private static final Scanner scanner = new Scanner(System.in);
public static int getIntInput(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("输入无效,请输入整数!");
}
}
}
public static String getStringInput(String prompt) {
System.out.print(prompt);
return scanner.nextLine().trim();
}
public static double getDoubleInput(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Double.parseDouble(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("输入无效,请输入数字!");
}
}
}
}
// 主系统类
class StudentManagementSystem {
private List<Student> students;
private boolean isRunning = true;
public StudentManagementSystem() {
students = FileUtil.loadData();
}
public void start() {
while (isRunning) {
showMainMenu();
int choice = InputUtil.getIntInput("请选择操作:");
handleMainMenu(choice);
}
}
private void showMainMenu() {
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("6. 保存数据");
System.out.println("7. 退出系统");
}
private void handleMainMenu(int choice) {
switch (choice) {
// case 1 -> addStudent();
// case 2 -> searchStudent();
// case 3 -> inputScore();
// case 4 -> showStatistics();
// case 5 -> deleteStudent();
// case 6 -> saveData();
// case 7 -> exitSystem();
// default -> System.out.println("无效选项!");
}
}
private void addStudent() {
String id = InputUtil.getStringInput("请输入学号:");
if (isStudentExist(id)) {
System.out.println("该学号已存在!");
return;
}
String name = InputUtil.getStringInput("请输入姓名:");
int age = InputUtil.getIntInput("请输入年龄:");
String className = InputUtil.getStringInput("请输入班级:");
Student student = new Student(id, name, age, className);
students.add(student);
System.out.println("学生添加成功!");
}
private void searchStudent() {
System.out.println("\n==== 查询选项 ====");
System.out.println("1. 按学号查询");
System.out.println("2. 按姓名查询");
System.out.println("3. 显示全部");
int choice = InputUtil.getIntInput("请选择查询方式:");
switch (choice) {
// case 1 -> searchById();
// case 2 -> searchByName();
// case 3 -> showAllStudents();
// default -> System.out.println("无效选项!");
}
}
private void searchById() {
String id = InputUtil.getStringInput("请输入学号:");
students.stream()
.filter(s -> s.getId().equals(id))
.findFirst();
}
private void searchByName() {
String name = InputUtil.getStringInput("请输入姓名:");
List<Student> result = null;
try {
students.stream()
.filter(s -> s.getName().equalsIgnoreCase(name))
.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (result.isEmpty()) {
System.out.println("找不到该学生");
} else {
result.forEach(System.out::println);
}
}
private void showAllStudents() {
if (students.isEmpty()) {
System.out.println("当前没有学生记录");
return;
}
students.forEach(s -> {
System.out.println(s);
System.out.println("--------------------");
});
}
private void inputScore() {
String id = InputUtil.getStringInput("请输入学号:");
Student student = findStudentById(id);
if (student == null) {
System.out.println("学生不存在!");
return;
}
CourseManager.showAvailableCourses();
String course = InputUtil.getStringInput("请输入课程名称:");
if (!CourseManager.isValidCourse(course)) {
System.out.println("无效课程!");
return;
}
double score = InputUtil.getDoubleInput("请输入成绩:");
student.updateScore(course, score);
System.out.println("成绩录入成功!");
}
private void showStatistics() {
System.out.println("\n==== 统计信息 ====");
System.out.println("1. 班级人数统计");
System.out.println("2. 课程平均分");
System.out.println("3. 学生个人成绩");
int choice = InputUtil.getIntInput("请选择统计类型:");
switch (choice) {
// case 1 -> showClassStatistics();
// case 2 -> showCourseStatistics();
// case 3 -> showStudentScores();
// default -> System.out.println("无效选项!");
}
}
private void showClassStatistics() {
Map<String, Long> classCount = students.stream()
.collect(Collectors.groupingBy(
Student::getClassName,
Collectors.counting()));
classCount.forEach((className, count) ->
System.out.printf("班级 %s: %d 人\n", className, count));
}
private void showCourseStatistics() {
Map<String, Double> courseAverages = new HashMap<>();
students.forEach(student ->
student.getScores().forEach((course, score) ->
courseAverages.merge(course, score, Double::sum)));
courseAverages.forEach((course, total) -> {
long count = students.stream()
.filter(s -> s.getScores().containsKey(course))
.count();
double average = count > 0 ? total / count : 0;
System.out.printf("%s 平均分: %.2f\n", course, average);
});
}
private void showStudentScores() {
String id = InputUtil.getStringInput("请输入学号:");
Student student = findStudentById(id);
if (student == null) {
System.out.println("学生不存在!");
return;
}
System.out.println(student);
if (student.getScores().isEmpty()) {
System.out.println("暂无成绩记录");
return;
}
student.getScores().forEach((course, score) ->
System.out.printf("%s: %.2f\n", course, score));
System.out.printf("平均分: %.2f\n", student.calculateAverage());
}
private void deleteStudent() {
String id = InputUtil.getStringInput("请输入要删除的学号:");
boolean removed = students.removeIf(s -> s.getId().equals(id));
System.out.println(removed ? "删除成功!" : "学生不存在!");
}
private void saveData() {
FileUtil.saveData(students);
System.out.println("数据已保存!");
}
private void exitSystem() {
isRunning = false;
System.out.println("正在退出系统...");
saveData();
System.out.println("谢谢使用!");
}
private boolean isStudentExist(String id) {
return students.stream()
.anyMatch(s -> s.getId().equals(id));
}
private Student findStudentById(String id) {
return students.stream()
.filter(s -> s.getId().equals(id))
.findFirst()
.orElse(null);
}
public static void main(String[] args) {
new StudentManagementSystem().start();
}
}
Loading…
Cancel
Save