首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Java 命令行學生成績管理系統設計指南

     編輯:程式設計綜合問答

系統架構

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  菜單交互層  │ ←→  │  業務邏輯層  │ ←→  │  數據持久層  │
│  (Scanner)  │     │  (Service)  │     │  (File/DB)  │
└─────────────┘     └─────────────┘     └─────────────┘

核心類設計

// 實體
class Student {
    String id, name;
    Map<String, Double> scores; // 科目→分數
    // getters/setters, toString()
}

// DAO 接口
interface StudentDao {
    void save(Student s);
    Student findByName(String name);
    Student findById(String id);
    List<Student> findAll();
    void update(Student s);
    void delete(String id);
}

// 文件實現(CSV 格式)
class FileStudentDao implements StudentDao {
    private final Path file = Paths.get("students.csv");
    // 實現 CRUD,每行:id,name,math,english,physics...
}

// 業務服務
class StudentService {
    private final StudentDao dao;
    // 封裝排序、統計邏輯
    List<Student> rankBySubject(String subject) { ... }
    List<Student> rankById(String subject) { ... }
}

持久化細節

  • 文本文件:簡單、可視、適合數據量 < 1 萬條。用 Files.readAllLines/Files.write + CSV 解析(推薦 OpenCSV 或手寫 split(","))。
  • 序列化:ObjectOutputStream 直接存對象圖,但版本升級不兼容,不推薦長期項目。
  • 嵌入式資料庫:H2 / SQLite,零配置、支持 SQL 排序統計,**強烈推薦**作為進階版本。

菜單交互樣例

while (true) {
    System.out.println("""
        1. 添加學生
        2. 按姓名查詢
        3. 科目成績排名
        4. 學號成績排名
        5. 退出
        請選擇:""");
    int cmd = scanner.nextInt();
    switch (cmd) {
        case 1 -> addStudent();
        case 2 -> queryByName();
        case 3 -> rankBySubject();
        case 4 -> rankById();
        case 5 -> System.exit(0);
    }
}

進階擴展點

  • 引入 ServiceLoader 實現 DAO 插件化(文件/H2/MySQL 熱插拔)。
  • 用 TablePrinter 美化控制台表格輸出。
  • 加入單元測試(JUnit + Mockito)保證業務邏輯正確性。

「先跑通文件版,再遷移到 H2」是最平滑的學習曲線。核心難點在排序邏輯與文件並發,建議早期引入 ReentrantReadWriteLock 保護文件讀寫。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved