进一步完成管理类

StudentManager:
- 录入每个学生的学号、姓名和各科考试成绩
- 计算每门课程的总分和平均分
ManagerTool:
- 获取学生总人数
- 获取所有科目名称
- 生成排名信息(按总分)
main
lensfrex 3 years ago
parent 3e3c93bb48
commit ad3c333aab
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 63
      src/main/java/me/lensfrex/manager/data/ManagerTool.java
  2. 88
      src/main/java/me/lensfrex/manager/data/StudentManager.java
  3. 2
      src/main/resources/me.lensfrex.manager/studentInformationExample.txt

@ -1,15 +1,14 @@
package me.lensfrex.manager.data;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class ManagerTool {
private final ArrayList<Student> studentData;
private final ArrayList<Student> students;
public ManagerTool(ArrayList<Student> studentData) {
this.studentData = studentData;
this.students = studentData;
}
/**
@ -21,7 +20,7 @@ public class ManagerTool {
*/
public void addStudent(long id, String name, Map<String, Integer> score) {
Student student = new Student(id, name, score);
studentData.add(student);
students.add(student);
}
/**
@ -31,12 +30,11 @@ public class ManagerTool {
* @return 学生的信息若无该学生则返回null
*/
public Student getStudent(String name) {
for (Student student : studentData) {
for (Student student : students) {
if (student.getName().equals(name)) {
return student;
}
}
return null;
}
@ -47,12 +45,11 @@ public class ManagerTool {
* @return 学生信息若无该学生则返回null
*/
public Student getStudent(long id) {
for (Student student : studentData) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
@ -67,21 +64,20 @@ public class ManagerTool {
* @return 排序后的学生列表
*/
public ArrayList<Student> getSortedStudentList(int compareMethod) {
ArrayList<Student> sortedStudentList = new ArrayList<>(studentData);
switch (compareMethod) {
case SORT_COMPARE_BY_NAME:
sortedStudentList.sort(Student.compareByName);
students.sort(Student.compareByName);
break;
case SORT_COMPARE_BY_ID:
sortedStudentList.sort(Student.compareById);
students.sort(Student.compareById);
break;
case SORT_COMPARE_BY_GENERAL_SCORE:
sortedStudentList.sort(Student.compareByGeneralScore);
students.sort(Student.compareByGeneralScore);
break;
default:
break;
}
return sortedStudentList;
return students;
}
/**
@ -92,7 +88,7 @@ public class ManagerTool {
*/
public HashMap<String, Integer> getSubjectScores(String subject) {
HashMap<String, Integer> scores = new HashMap<>();
for (Student student : studentData) {
for (Student student : students) {
scores.put(student.getName(), student.getScore().get(subject));
}
return scores;
@ -107,7 +103,7 @@ public class ManagerTool {
HashMap<String, Integer> result = new HashMap<>();
int scoreSum = 0;
for (Student student : studentData) {
for (Student student : students) {
for (int score : student.getScore().values()) {
scoreSum += score;
}
@ -116,4 +112,41 @@ public class ManagerTool {
}
return result;
}
/**
* 生成排名信息按总分
*
* @return 包含有排名信息的HashMapkey为姓名value为相应的排名
*/
public HashMap<String, Integer> generateRanking() {
HashMap<String, Integer> ranking = new HashMap<>();
this.getSortedStudentList(SORT_COMPARE_BY_GENERAL_SCORE);
for (Student student : students) {
ranking.put(student.getName(), students.indexOf(student));
}
return ranking;
}
/**
* 获取所有科目名称若无学生信息则返回null
*
* @return 包含所有科目名称的Arraylist若无学生信息则返回null
*/
public ArrayList<String> getAllSubjects() {
if (students.isEmpty()) {
return null;
}
return new ArrayList<>(students.get(0).getScore().keySet());
}
/**
* 获取学生总人数
*
* @return 学生总人数
*/
public int getStudentAmount() {
return students.size();
}
}

@ -1,11 +1,95 @@
package me.lensfrex.manager.data;
import java.util.ArrayList;
import java.util.HashMap;
public class StudentManager {
private ArrayList<Student> studentData = new ArrayList<>();
private final ArrayList<Student> students = new ArrayList<>();
private final ManagerTool managerTool = new ManagerTool(students);
private ArrayList<String> subjects = new ArrayList<>();
public void addStudent() {
/**
* 增添学生输入的字符串格式应为 [id] [name] ...[subjectName,score]
* 23333333 Lag English,100 Math,99 History,98
*
* @param input 待添加的学生信息
*/
public void addStudent(String input) {
String[] split = input.split(" ");
long id = Long.parseLong(split[0]);
String name = split[1];
HashMap<String, Integer> score = new HashMap<>();
String[] scoreInput;
for (int i = 2; i < split.length; i++) {
scoreInput = split[i].split(",");
score.put(scoreInput[0], Integer.parseInt(scoreInput[1]));
}
managerTool.addStudent(id, name, score);
}
// 要是Java能像go语言那样返回两个返回值就好了...
// 这样就能同时返回平均值和总值了...
// 做宝可梦.jpg
// 还是老老实实写两方法吧...
/**
* 获取班级某科目平均成绩
*
* @param subject 欲获取的科目
* @return 科目平均成绩
*/
public float getSubjectAvgScore(String subject) {
return (float) getSubjectScore(subject) / managerTool.getStudentAmount();
}
/**
* 获取班级某科目总成绩
*
* @param subject 欲获取的科目
* @return 科目总成绩
*/
public int getSubjectScore(String subject) {
ArrayList<Integer> scores = new ArrayList<>(managerTool.getSubjectScores(subject).values());
int sum = 0;
for (int score : scores) {
sum += score;
}
return sum;
}
/**
* 获取每门科目的总成绩
*
* @return 包含每门课总成绩的HashMapkey为科目名称value为该科目的总成绩
*/
public HashMap<String, Integer> getAllSubjectScore() {
ArrayList<String> subjects = managerTool.getAllSubjects();
HashMap<String, Integer> scores = new HashMap<>();
for (String subject : subjects) {
scores.put(subject, getSubjectScore(subject));
}
return scores;
}
/**
* 获取每门科目的平均成绩
*
* @return 包含每门课总成绩的HashMapkey为科目名称value为该科目的平均成绩
*/
public HashMap<String, Float> getAllSubjectAvgScore() {
ArrayList<String> subjects = managerTool.getAllSubjects();
HashMap<String, Float> scores = new HashMap<>();
for (String subject : subjects) {
scores.put(subject, getSubjectAvgScore(subject));
}
return scores;
}
}

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