抛弃sqlite转向自造轮子

先这样吧...
目前已实现基本逻辑:
- 添加一个学生
- 按名字查找学生
- 按学号查找学生
- 对学生列表进行排序
- 获取全班某课程的成绩信息
- 获取全班所有同学的总成绩信息
- 从文件中读取学生数据
希望没什么bug
main
lensfrex 3 years ago
parent bea60a3774
commit 3e3c93bb48
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 102
      src/main/java/me/lensfrex/manager/data/ManagerTool.java
  2. 24
      src/main/java/me/lensfrex/manager/data/Student.java
  3. 3
      src/main/java/me/lensfrex/manager/data/StudentManager.java
  4. 43
      src/main/java/me/lensfrex/manager/utils/DataLoader.java

@ -1,6 +1,8 @@
package me.lensfrex.manager.data; package me.lensfrex.manager.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class ManagerTool { public class ManagerTool {
@ -10,8 +12,108 @@ public class ManagerTool {
this.studentData = studentData; this.studentData = studentData;
} }
/**
* 添加一个学生
*
* @param id 学生idlong格式
* @param name 学生姓名
* @param score 学生成绩map中key为科目名称value为相应的成绩
*/
public void addStudent(long id, String name, Map<String, Integer> score) { public void addStudent(long id, String name, Map<String, Integer> score) {
Student student = new Student(id, name, score);
studentData.add(student);
}
/**
* 按名字查找学生
*
* @param name 学生姓名
* @return 学生的信息若无该学生则返回null
*/
public Student getStudent(String name) {
for (Student student : studentData) {
if (student.getName().equals(name)) {
return student;
}
}
return null;
}
/**
* 按学号查找学生
*
* @param id 学生学号
* @return 学生信息若无该学生则返回null
*/
public Student getStudent(long id) {
for (Student student : studentData) {
if (student.getId() == id) {
return student;
}
}
return null;
}
public static final int SORT_COMPARE_BY_NAME = 0;
public static final int SORT_COMPARE_BY_ID = 1;
public static final int SORT_COMPARE_BY_GENERAL_SCORE = 2;
/**
* 对学生列表进行排序
*
* @param compareMethod 排序方式可根据名字字典顺序升序0学生学号升序1以及总成绩降序2进行排序
* @return 排序后的学生列表
*/
public ArrayList<Student> getSortedStudentList(int compareMethod) {
ArrayList<Student> sortedStudentList = new ArrayList<>(studentData);
switch (compareMethod) {
case SORT_COMPARE_BY_NAME:
sortedStudentList.sort(Student.compareByName);
break;
case SORT_COMPARE_BY_ID:
sortedStudentList.sort(Student.compareById);
break;
case SORT_COMPARE_BY_GENERAL_SCORE:
sortedStudentList.sort(Student.compareByGeneralScore);
break;
default:
break;
}
return sortedStudentList;
}
/**
* 获取全班某课程的成绩信息
*
* @param subject 欲获取的科目信息
* @return 包含成绩信息的HashMapkey为学生姓名value为该科成绩数据
*/
public HashMap<String, Integer> getSubjectScores(String subject) {
HashMap<String, Integer> scores = new HashMap<>();
for (Student student : studentData) {
scores.put(student.getName(), student.getScore().get(subject));
}
return scores;
} }
/**
* 获取全班所有同学的总成绩信息
*
* @return 包含全班所有同学成绩的HashMapkey为姓名value为总成绩数据
*/
public HashMap<String, Integer> getStudentScores() {
HashMap<String, Integer> result = new HashMap<>();
int scoreSum = 0;
for (Student student : studentData) {
for (int score : student.getScore().values()) {
scoreSum += score;
}
result.put(student.getName(), scoreSum);
scoreSum = 0;
}
return result;
}
} }

@ -1,10 +1,13 @@
package me.lensfrex.manager.data; package me.lensfrex.manager.data;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Map; import java.util.Map;
public class Student { public class Student implements Serializable {
private String name; private String name;
//private String id; //private String id;
// 改用long是因为方便排序 :)
private long id; private long id;
private Map<String, Integer> score; private Map<String, Integer> score;
@ -32,9 +35,24 @@ public class Student {
this.score = score; this.score = score;
} }
public Student(String name, long id, Map<String, Integer> score) { public Student(long id, String name, Map<String, Integer> score) {
this.name = name;
this.id = id; this.id = id;
this.name = name;
this.score = score; this.score = score;
} }
public static Comparator<Student> compareByName = (s1, s2) -> s1.getName().compareTo(s2.getName());
public static Comparator<Student> compareById = (s1, s2) -> (int) (s1.id - s2.id);
public static Comparator<Student> compareByGeneralScore = (s1, s2) -> {
int student1GeneralScore = 0, student2GeneralScore = 0;
for (int score : s1.getScore().values()) {
student1GeneralScore += score;
}
for (int score : s2.getScore().values()) {
student2GeneralScore += score;
}
return student1GeneralScore - student2GeneralScore;
};
} }

@ -5,6 +5,7 @@ import java.util.ArrayList;
public class StudentManager { public class StudentManager {
private ArrayList<Student> studentData = new ArrayList<>(); private ArrayList<Student> studentData = new ArrayList<>();
public void addStudent() {
}
} }

@ -0,0 +1,43 @@
package me.lensfrex.manager.utils;
import me.lensfrex.manager.data.Student;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
/**
* 参考自以前的项目
* https://github.com/lensferno/dogename/blob/main/Dogename/src/main/java/me/lensferno/dogename/data/History.java
*/
public class DataLoader {
private static final String DATA_FILE = "files/students.data";
/**\
* 从默认路径files/student.data加载学生数据若文件不存在或读取过程中发生错误则新建一个空的ArrayList
* @return 加载读取的结果若文件不存在或读取过程中发生错误则返回一个空的ArrayList
*/
public ArrayList<Student> load() {
File historyFile = new File(DATA_FILE);
try {
if (!historyFile.exists()) {
historyFile.getParentFile().mkdirs();
historyFile.createNewFile();
return new ArrayList<>();
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(historyFile));
ois.close();
return (ArrayList<Student>) ois.readObject();
} catch (EOFException e) {
System.out.println("Data file is empty.");
return new ArrayList<>();
} catch (Exception e) {
System.out.println("Failed to load data file:" + e);
e.printStackTrace();
return new ArrayList<>();
}
}
}
Loading…
Cancel
Save