StudentManager: - 录入每个学生的学号、姓名和各科考试成绩 - 计算每门课程的总分和平均分 ManagerTool: - 获取学生总人数 - 获取所有科目名称 - 生成排名信息(按总分)main
parent
3e3c93bb48
commit
ad3c333aab
@ -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 包含每门课总成绩的HashMap,key为科目名称,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 包含每门课总成绩的HashMap,key为科目名称,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…
Reference in new issue