import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
class ScoreException extends Exception {
public ScoreException(String message) {
super(message);
}
}
class Student {
private String stuId;
private String name;
private int javaScore;
private int pythonScore;
private int webScore;
public Student(String stuId, String name, int javaScore, int pythonScore, int webScore) {
this.stuId = stuId;
this.name = name;
this.javaScore = javaScore;
this.pythonScore = pythonScore;
this.webScore = webScore;
}
public String getStuId() { return stuId; }
public void setStuId(String stuId) { this.stuId = stuId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getJavaScore() { return javaScore; }
public void setJavaScore(int javaScore) { this.javaScore = javaScore; }
public int getPythonScore() { return pythonScore; }
public void setPythonScore(int pythonScore) { this.pythonScore = pythonScore; }
public int getWebScore() { return webScore; }
public void setWebScore(int webScore) { this.webScore = webScore; }
public int calculateTotalScore() {
return javaScore + pythonScore + webScore;
}
public double calculateAverageScore() {
return calculateTotalScore() / 3.0;
}
@Override
public String toString() {
return "学号:" + stuId + ", 姓名:" + name +
", Java:" + javaScore + ", Python:" + pythonScore + ", Web:" + webScore;
}
}
interface StudentManager {
void addStudent(Student student) throws ScoreException;
void deleteStudent(String stuId);
void updateStudent(Student student) throws ScoreException;
Student queryStudent(String stuId);
List<Student> getAllStudents();
void saveDataToFile(String filePath) throws ScoreException;
void loadDataFromFile(String filePath) throws ScoreException;
}
class ScoreManager implements StudentManager {
private List<Student> studentList = new ArrayList<>();
@Override
public void addStudent(Student student) throws ScoreException {
if (studentList.stream().anyMatch(s -> s.getStuId().equals(student.getStuId()))) {
throw new ScoreException("学号重复异常:学号 " + student.getStuId() + " 已存在");
}
if (!student.getName().matches("^[\u4e00-\u9fa5]{2,4}$")) {
throw new ScoreException("姓名格式异常:姓名必须为 2-4 个汉字");
}
if (student.getJavaScore() < 0 || student.getJavaScore() > 100 ||
student.getPythonScore() < 0 || student.getPythonScore() > 100 ||
student.getWebScore() < 0 || student.getWebScore() > 100) {
throw new ScoreException("成绩输入范围异常:成绩必须在 0-100 之间");
}
studentList.add(student);
}
@Override
public void deleteStudent(String stuId) {
studentList.removeIf(s -> s.getStuId().equals(stuId));
}
@Override
public void updateStudent(Student student) throws ScoreException {
deleteStudent(student.getStuId());
addStudent(student);
}
@Override
public Student queryStudent(String stuId) {
return studentList.stream()
.filter(s -> s.getStuId().equals(stuId))
.findFirst()
.orElse(null);
}
@Override
public List<Student> getAllStudents() {
return studentList;
}
@Override
public void saveDataToFile(String filePath) throws ScoreException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (Student student : studentList) {
writer.write(student.getStuId() + "," +
student.getName() + "," +
student.getJavaScore() + "," +
student.getPythonScore() + "," +
student.getWebScore());
writer.newLine();
}
} catch (IOException e) {
throw new ScoreException("文件操作异常:" + e.getMessage());
}
}
@Override
public void loadDataFromFile(String filePath) throws ScoreException {
studentList.clear();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length != 5) {
throw new ScoreException("文件格式异常:数据行格式错误");
}
Student student = new Student(
parts[0], parts[1],
Integer.parseInt(parts[2]),
Integer.parseInt(parts[3]),
Integer.parseInt(parts[4])
);
addStudent(student);
}
} catch (IOException | NumberFormatException e) {
throw new ScoreException("文件操作异常:" + e.getMessage());
}
}
public double[] calculateClassAverageScores() {
if (studentList.isEmpty()) return new double[0];
int javaTotal = 0, pythonTotal = 0, webTotal = 0;
for (Student student : studentList) {
javaTotal += student.getJavaScore();
pythonTotal += student.getPythonScore();
webTotal += student.getWebScore();
}
int count = studentList.size();
return new double[]{
(double) javaTotal / count,
(double) pythonTotal / count,
(double) webTotal / count
};
}
public List<Student> sortByTotalScoreDesc() {
return studentList.stream()
.sorted(Comparator.comparingInt(Student::calculateTotalScore).reversed())
.collect(Collectors.toList());
}
}
public class StudentGradeManagementSystem {
private static ScoreManager scoreManager = new ScoreManager();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
showMenu();
}
private static void showMenu() {
while (true) {
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. 保存数据");
System.out.println("8. 加载数据");
System.out.println("0. 退出");
System.out.print("请选择操作:");
try {
int choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1: addStudent(); break;
case 2: deleteStudent(); break;
case 3: updateStudent(); break;
case 4: queryStudent(); break;
case 5: showAllStudents(); break;
case 6: analyzeScores(); break;
case 7: saveData(); break;
case 8: loadData(); break;
case 0: System.out.println("退出系统!"); return;
default: System.out.println("无效选择!");
}
} catch (NumberFormatException e) {
System.out.println("输入格式错误!请输入数字。");
} catch (ScoreException e) {
System.out.println("操作失败:" + e.getMessage());
}
}
}
private static void addStudent() throws ScoreException {
System.out.print("请输入学号:");
String stuId = scanner.nextLine();
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入Java成绩:");
int java = Integer.parseInt(scanner.nextLine());
System.out.print("请输入Python成绩:");
int python = Integer.parseInt(scanner.nextLine());
System.out.print("请输入Web成绩:");
int web = Integer.parseInt(scanner.nextLine());
scoreManager.addStudent(new Student(stuId, name, java, python, web));
System.out.println("添加成功!");
}
private static void deleteStudent() {
System.out.print("请输入要删除的学号:");
String stuId = scanner.nextLine();
scoreManager.deleteStudent(stuId);
System.out.println("删除成功!");
}
private static void updateStudent() throws ScoreException {
System.out.print("请输入要修改的学号:");
String stuId = scanner.nextLine();
Student student = scoreManager.queryStudent(stuId);
if (student == null) {
System.out.println("未找到该学生!");
return;
}
System.out.print("请输入新姓名(当前:" + student.getName() + "):");
String name = scanner.nextLine();
System.out.print("请输入新Java成绩(当前:" + student.getJavaScore() + "):");
int java = Integer.parseInt(scanner.nextLine());
System.out.print("请输入新Python成绩(当前:" + student.getPythonScore() + "):");
int python = Integer.parseInt(scanner.nextLine());
System.out.print("请输入新Web成绩(当前:" + student.getWebScore() + "):");
int web = Integer.parseInt(scanner.nextLine());
scoreManager.updateStudent(new Student(stuId, name, java, python, web));
System.out.println("修改成功!");
}
private static void queryStudent() {
System.out.print("请输入要查询的学号:");
String stuId = scanner.nextLine();
Student student = scoreManager.queryStudent(stuId);
if (student == null) {
System.out.println("未找到该学生!");
} else {
System.out.println("查询结果:" + student);
}
}
private static void showAllStudents() {
List<Student> students = scoreManager.getAllStudents();
if (students.isEmpty()) {
System.out.println("暂无学生数据!");
} else {
System.out.println("所有学生信息:");
for (Student student : students) {
System.out.println(student);
}
}
}
private static void analyzeScores() {
List<Student> students = scoreManager.getAllStudents();
if (students.isEmpty()) {
System.out.println("暂无学生数据!");
return;
}
double[] averages = scoreManager.calculateClassAverageScores();
System.out.printf("全班平均分:Java=%.2f, Python=%.2f, Web=%.2f%n",
averages[0], averages[1], averages[2]);
System.out.println("\n总分排名:");
List<Student> sortedStudents = scoreManager.sortByTotalScoreDesc();
for (int i = 0; i < sortedStudents.size(); i++) {
Student s = sortedStudents.get(i);
System.out.printf("%d. %s,总分=%d%n", i + 1, s.getName(), s.calculateTotalScore());
}
}
private static void saveData() throws ScoreException {
scoreManager.saveDataToFile("students.txt");
System.out.println("数据已保存到 students.txt");
}
private static void loadData() throws ScoreException {
scoreManager.loadDataFromFile("students.txt");
System.out.println("数据加载成功!");
}
}