微改结构

main
lensfrex 3 years ago
parent 3cf490dc74
commit 7b9dc8df43
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 34
      src/main/java/me/lensfrex/manager/data/manager/ManagerTool.java
  2. 61
      src/main/java/me/lensfrex/manager/data/manager/StudentManager.java

@ -1,4 +1,6 @@
package me.lensfrex.manager.data;
package me.lensfrex.manager.data.manager;
import me.lensfrex.manager.data.Student;
import java.util.ArrayList;
import java.util.HashMap;
@ -10,7 +12,7 @@ import java.util.Map;
public class ManagerTool {
private final ArrayList<Student> students;
public ManagerTool(ArrayList<Student> studentData) {
protected ManagerTool(ArrayList<Student> studentData) {
this.students = studentData;
}
@ -21,7 +23,7 @@ public class ManagerTool {
* @param name 学生姓名
* @param score 学生成绩map中key为科目名称value为相应的成绩
*/
public void addStudent(long id, String name, Map<String, Integer> score) {
protected void addStudent(long id, String name, Map<String, Integer> score) {
Student student = new Student(id, name, score);
students.add(student);
}
@ -31,7 +33,7 @@ public class ManagerTool {
*
* @param id 学生idlong格式
*/
public void removeStudent(long id) {
protected void removeStudent(long id) {
students.remove(getStudent(id));
}
@ -40,7 +42,7 @@ public class ManagerTool {
*
* @param name 学生姓名long格式
*/
public void removeStudent(String name) {
protected void removeStudent(String name) {
students.remove(getStudent(name));
}
@ -50,7 +52,7 @@ public class ManagerTool {
* @param name 学生姓名
* @return 学生的信息若无该学生则返回null
*/
public Student getStudent(String name) {
protected Student getStudent(String name) {
for (Student student : students) {
if (student.getName().equals(name)) {
return student;
@ -65,7 +67,7 @@ public class ManagerTool {
* @param id 学生学号
* @return 学生信息若无该学生则返回null
*/
public Student getStudent(long id) {
protected Student getStudent(long id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
@ -74,9 +76,9 @@ public class ManagerTool {
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;
protected static final int SORT_COMPARE_BY_NAME = 0;
protected static final int SORT_COMPARE_BY_ID = 1;
protected static final int SORT_COMPARE_BY_GENERAL_SCORE = 2;
/**
* 对学生列表进行排序
@ -85,7 +87,7 @@ public class ManagerTool {
* 可根据名字字典顺序升序0学生学号升序1以及总成绩降序2进行排序
* @return 排序后的学生列表
*/
public ArrayList<Student> getSortedStudentList(int compareMethod) {
protected ArrayList<Student> getSortedStudentList(int compareMethod) {
switch (compareMethod) {
case SORT_COMPARE_BY_NAME:
students.sort(Student.compareByName);
@ -108,7 +110,7 @@ public class ManagerTool {
* @param subject 欲获取的科目信息
* @return 包含成绩信息的HashMapkey为学生姓名value为该科成绩数据
*/
public HashMap<String, Integer> getSubjectScores(String subject) {
protected HashMap<String, Integer> getSubjectScores(String subject) {
HashMap<String, Integer> scores = new HashMap<>();
for (Student student : students) {
scores.put(student.getName(), student.getScore().get(subject));
@ -121,7 +123,7 @@ public class ManagerTool {
*
* @return 包含全班所有同学成绩的HashMapkey为姓名value为总成绩数据
*/
public HashMap<String, Integer> getAllStudentScores() {
protected HashMap<String, Integer> getAllStudentScores() {
HashMap<String, Integer> result = new HashMap<>();
int scoreSum = 0;
@ -140,7 +142,7 @@ public class ManagerTool {
*
* @return 包含有排名信息的HashMapkey为姓名value为相应的排名
*/
public HashMap<String, Integer> generateRanking() {
protected HashMap<String, Integer> generateRanking() {
HashMap<String, Integer> ranking = new HashMap<>();
this.getSortedStudentList(SORT_COMPARE_BY_GENERAL_SCORE);
@ -156,7 +158,7 @@ public class ManagerTool {
*
* @return 包含所有科目名称的Arraylist若无学生信息则返回null
*/
public ArrayList<String> getAllSubjects() {
protected ArrayList<String> getAllSubjects() {
if (students.isEmpty()) {
return null;
}
@ -168,7 +170,7 @@ public class ManagerTool {
*
* @return 学生总人数
*/
public int getStudentAmount() {
protected int getStudentAmount() {
return students.size();
}

@ -1,4 +1,6 @@
package me.lensfrex.manager.data;
package me.lensfrex.manager.data.manager;
import me.lensfrex.manager.data.Student;
import java.util.ArrayList;
import java.util.HashMap;
@ -79,7 +81,7 @@ public class StudentManager {
return scores;
}
/**-
/**
* 获取每门科目的平均成绩
*
* @return 包含每门课总成绩的HashMapkey为科目名称value为该科目的平均成绩
@ -95,6 +97,14 @@ public class StudentManager {
return scores;
}
/**
* 获取所有科目
* @return 包含有科目名称的ArrayList
*/
public ArrayList<String> getSubjects() {
return managerTool.getAllSubjects();
}
/**
* 按学生总分由高到低生成名次表
*
@ -115,7 +125,6 @@ public class StudentManager {
return stringBuilder.toString();
}
/**
* 按学生学号由小到大生成成绩表
*
@ -123,21 +132,14 @@ public class StudentManager {
*/
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();
ArrayList<Student> sortStudentList = managerTool.getSortedStudentList(ManagerTool.SORT_COMPARE_BY_ID);
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');
.append(student.getGeneralScore()).append(" | ")
.append(listScore(student)).append('\n');
}
return stringBuilder.toString();
@ -149,25 +151,36 @@ public class StudentManager {
* @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();
StringBuilder stringBuilder = new StringBuilder("Name | Id | General Score | Subject1: score Subject2: score ...\n");
ArrayList<Student> sortStudentList = managerTool.getSortedStudentList(ManagerTool.SORT_COMPARE_BY_NAME);
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');
.append(student.getGeneralScore()).append(" | ")
.append(listScore(student)).append('\n');
}
return stringBuilder.toString();
}
/**
* 列出某一学生的成绩
*
* @param student 学生
* @return 成绩字符串
*/
private String listScore(Student student) {
StringBuilder stringBuilder = new StringBuilder();
ArrayList<String> subjects = managerTool.getAllSubjects();
for (String subject : subjects) {
stringBuilder
.append(subject).append(": ")
.append(student.getScore().get(subject)).append(' ');
}
return stringBuilder.toString();
}
}
Loading…
Cancel
Save