进一步完成

Student:
- 内部新增getGeneralScore()方法
StudentManager:
- 按学生总分由高到低生成名次表
- 按学生学号由小到大生成成绩表
- 按学生学号由小到大生成成绩表

(有点困了...写得有点乱了... ( ̄﹏ ̄;))
main
lensfrex 3 years ago
parent b6c5b80435
commit 3cf490dc74
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 27
      src/main/java/me/lensfrex/manager/data/ManagerTool.java
  2. 20
      src/main/java/me/lensfrex/manager/data/Student.java
  3. 82
      src/main/java/me/lensfrex/manager/data/StudentManager.java
  4. 2
      src/main/resources/me.lensfrex.manager/studentInformationExample.txt

@ -4,6 +4,9 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* 对Student类的操作的底层封装有最基础的操作
*/
public class ManagerTool { public class ManagerTool {
private final ArrayList<Student> students; private final ArrayList<Student> students;
@ -23,6 +26,24 @@ public class ManagerTool {
students.add(student); students.add(student);
} }
/**
* 删除一个学生按ID
*
* @param id 学生idlong格式
*/
public void removeStudent(long id) {
students.remove(getStudent(id));
}
/**
* 删除一个学生按姓名
*
* @param name 学生姓名long格式
*/
public void removeStudent(String name) {
students.remove(getStudent(name));
}
/** /**
* 按名字查找学生 * 按名字查找学生
* *
@ -60,7 +81,8 @@ public class ManagerTool {
/** /**
* 对学生列表进行排序 * 对学生列表进行排序
* *
* @param compareMethod 排序方式可根据名字字典顺序升序0学生学号升序1以及总成绩降序2进行排序 * @param compareMethod 排序方式
* 可根据名字字典顺序升序0学生学号升序1以及总成绩降序2进行排序
* @return 排序后的学生列表 * @return 排序后的学生列表
*/ */
public ArrayList<Student> getSortedStudentList(int compareMethod) { public ArrayList<Student> getSortedStudentList(int compareMethod) {
@ -99,7 +121,7 @@ public class ManagerTool {
* *
* @return 包含全班所有同学成绩的HashMapkey为姓名value为总成绩数据 * @return 包含全班所有同学成绩的HashMapkey为姓名value为总成绩数据
*/ */
public HashMap<String, Integer> getStudentScores() { public HashMap<String, Integer> getAllStudentScores() {
HashMap<String, Integer> result = new HashMap<>(); HashMap<String, Integer> result = new HashMap<>();
int scoreSum = 0; int scoreSum = 0;
@ -128,6 +150,7 @@ public class ManagerTool {
return ranking; return ranking;
} }
/** /**
* 获取所有科目名称若无学生信息则返回null * 获取所有科目名称若无学生信息则返回null
* *

@ -41,18 +41,16 @@ public class Student implements Serializable {
this.score = score; this.score = score;
} }
public static Comparator<Student> compareByName = (s1, s2) -> s1.getName().compareTo(s2.getName()); public int getGeneralScore() {
public static Comparator<Student> compareById = (s1, s2) -> (int) (s1.id - s2.id); int generalScore = 0;
public static Comparator<Student> compareByGeneralScore = (s1, s2) -> { for (int score : getScore().values()) {
int student1GeneralScore = 0, student2GeneralScore = 0; generalScore += score;
for (int score : s1.getScore().values()) {
student1GeneralScore += score;
} }
for (int score : s2.getScore().values()) { return generalScore;
student2GeneralScore += score; }
}
return student1GeneralScore - student2GeneralScore; 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) -> s1.getGeneralScore() - s2.getGeneralScore();
} }

@ -3,10 +3,12 @@ package me.lensfrex.manager.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
/**
* 对Student类的操作的上层封装有较为多的操作
*/
public class StudentManager { public class StudentManager {
private final ArrayList<Student> students = new ArrayList<>(); private final ArrayList<Student> students = new ArrayList<>();
private final ManagerTool managerTool = new ManagerTool(students); private final ManagerTool managerTool = new ManagerTool(students);
private ArrayList<String> subjects = new ArrayList<>();
/** /**
* 增添学生输入的字符串格式应为 [id] [name] ...[subjectName,score] * 增添学生输入的字符串格式应为 [id] [name] ...[subjectName,score]
@ -77,7 +79,7 @@ public class StudentManager {
return scores; return scores;
} }
/** /**-
* 获取每门科目的平均成绩 * 获取每门科目的平均成绩
* *
* @return 包含每门课总成绩的HashMapkey为科目名称value为该科目的平均成绩 * @return 包含每门课总成绩的HashMapkey为科目名称value为该科目的平均成绩
@ -92,4 +94,80 @@ public class StudentManager {
return scores; return scores;
} }
/**
* 按学生总分由高到低生成名次表
*
* @return 名次表字符串
*/
public String generateRankingTable() {
StringBuilder stringBuilder = new StringBuilder("Ranking | Name | General Score\n");
ArrayList<Student> sortStudentList = managerTool.getSortedStudentList(ManagerTool.SORT_COMPARE_BY_GENERAL_SCORE);
for (int i = 0; i < sortStudentList.size(); i++) {
stringBuilder
.append(i + 1).append(" | ")
.append(sortStudentList.get(i).getName()).append(" | ")
.append(students.get(i).getGeneralScore())
.append('\n');
}
return stringBuilder.toString();
}
/**
* 按学生学号由小到大生成成绩表
*
* @return 成绩表字符串
*/
public String generateScoreTableById() {
StringBuilder stringBuilder = new StringBuilder("ID | Name | General Score | Subject1: score Subject2: score ...\n");
ArrayList<Student> sortStudentList = managerTool.getSortedStudentList(ManagerTool.SORT_COMPARE_BY_GENERAL_SCORE);
ArrayList<String> subjects = managerTool.getAllSubjects();
for (Student student : sortStudentList) {
stringBuilder
.append(student.getId()).append(" | ")
.append(student.getName()).append(" | ")
.append(student.getGeneralScore()).append(" | ");
for (String subject : subjects) {
stringBuilder
.append(subject).append(": ")
.append(student.getScore().get(subject)).append(' ');
}
stringBuilder.append('\n');
}
return stringBuilder.toString();
}
/**
* 按学生学号由小到大生成成绩表
*
* @return 成绩表字符串
*/
public String generateScoreTableByName() {
StringBuilder stringBuilder = new StringBuilder("Name | Id | General Score | Subject1/score Subject2: score ...\n");
ArrayList<Student> sortStudentList = managerTool.getSortedStudentList(ManagerTool.SORT_COMPARE_BY_GENERAL_SCORE);
ArrayList<String> subjects = managerTool.getAllSubjects();
for (Student student : sortStudentList) {
stringBuilder
.append(student.getName()).append(" | ")
.append(student.getId()).append(" | ")
.append(student.getGeneralScore()).append(" | ");
for (String subject : subjects) {
stringBuilder
.append(subject).append(": ")
.append(student.getScore().get(subject)).append(' ');
}
stringBuilder.append('\n');
}
return stringBuilder.toString();
}
} }

@ -1,4 +1,4 @@
- Please enter the student information following this format: - Please enter the student information following this format:
[id] [name] ...[subjectName,score] [id] [name] ...[subjectName,score]
- Example: - Example:
23333333 Lag English,100 Math,99 History,98 23333333 LagSeeing English,100 Math,99 History,98
Loading…
Cancel
Save