|
|
|
@ -16,6 +16,10 @@ public class StudentManager { |
|
|
|
|
this.students = managerTool.getStudentList(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean hasStudent() { |
|
|
|
|
return !students.isEmpty(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 增添学生。输入的字符串格式应为 [id] [name] ...[subjectName,score] |
|
|
|
|
* 如: 23333333 Lag English,100 Math,99 History,98 |
|
|
|
@ -24,18 +28,26 @@ public class StudentManager { |
|
|
|
|
*/ |
|
|
|
|
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])); |
|
|
|
|
try { |
|
|
|
|
long id = Long.parseLong(split[0]); |
|
|
|
|
String name = split[1]; |
|
|
|
|
|
|
|
|
|
if (managerTool.getStudent(id) != null && managerTool.getStudent(name) != null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
managerTool.addStudent(id, name, score); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -43,7 +55,7 @@ public class StudentManager { |
|
|
|
|
* |
|
|
|
|
* @param id 学生id,long格式 |
|
|
|
|
*/ |
|
|
|
|
protected void removeStudent(long id) { |
|
|
|
|
public void removeStudent(long id) { |
|
|
|
|
managerTool.removeStudent(id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -52,7 +64,7 @@ public class StudentManager { |
|
|
|
|
* |
|
|
|
|
* @param name 学生姓名,long格式 |
|
|
|
|
*/ |
|
|
|
|
protected void removeStudent(String name) { |
|
|
|
|
public void removeStudent(String name) { |
|
|
|
|
managerTool.removeStudent(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -96,7 +108,7 @@ public class StudentManager { |
|
|
|
|
HashMap<String, Integer> scores = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
for (String subject : subjects) { |
|
|
|
|
scores.put(subject, getSubjectScore(subject)); |
|
|
|
|
scores.put(subject, this.getSubjectScore(subject)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return scores; |
|
|
|
@ -112,12 +124,22 @@ public class StudentManager { |
|
|
|
|
HashMap<String, Float> scores = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
for (String subject : subjects) { |
|
|
|
|
scores.put(subject, getSubjectAvgScore(subject)); |
|
|
|
|
scores.put(subject, this.getSubjectAvgScore(subject)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return scores; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取某学生总成绩 |
|
|
|
|
* |
|
|
|
|
* @param student 欲获取的学生 |
|
|
|
|
* @return 该学生所有科目总成绩 |
|
|
|
|
*/ |
|
|
|
|
public int getStudentGeneralScore(Student student) { |
|
|
|
|
return student.getGeneralScore(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取所有科目 |
|
|
|
|
* |
|
|
|
@ -132,19 +154,21 @@ public class StudentManager { |
|
|
|
|
* |
|
|
|
|
* @return 名次表字符串 |
|
|
|
|
*/ |
|
|
|
|
public String generateRankingTable() { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder("Ranking | Name | General Score\n"); |
|
|
|
|
public ArrayList<String> generateRankingTable() { |
|
|
|
|
ArrayList<String> rankingTable = new ArrayList<>(); |
|
|
|
|
ArrayList<Student> sortStudentList = managerTool.getSortedStudentList(ManagerTool.SORT_COMPARE_BY_GENERAL_SCORE); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < sortStudentList.size(); i++) { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
stringBuilder |
|
|
|
|
.append(i + 1).append(" | ") |
|
|
|
|
.append(sortStudentList.get(i).getName()).append(" | ") |
|
|
|
|
.append(students.get(i).getGeneralScore()) |
|
|
|
|
.append('\n'); |
|
|
|
|
.append(students.get(i).getGeneralScore()); |
|
|
|
|
|
|
|
|
|
rankingTable.add(stringBuilder.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return stringBuilder.toString(); |
|
|
|
|
return rankingTable; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -152,19 +176,15 @@ public class StudentManager { |
|
|
|
|
* |
|
|
|
|
* @return 成绩表字符串 |
|
|
|
|
*/ |
|
|
|
|
public String generateScoreTableById() { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder("ID | Name | General Score | Subject1: score Subject2: score ...\n"); |
|
|
|
|
public ArrayList<String> generateScoreTableById() { |
|
|
|
|
ArrayList<String> scoreTable = new ArrayList<>(); |
|
|
|
|
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(" | ") |
|
|
|
|
.append(listScore(student)).append('\n'); |
|
|
|
|
scoreTable.add(this.getStudentInfo(student)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return stringBuilder.toString(); |
|
|
|
|
return scoreTable; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -172,18 +192,31 @@ public class StudentManager { |
|
|
|
|
* |
|
|
|
|
* @return 成绩表字符串 |
|
|
|
|
*/ |
|
|
|
|
public String generateScoreTableByName() { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder("Name | Id | General Score | Subject1: score Subject2: score ...\n"); |
|
|
|
|
public ArrayList<String> generateScoreTableByName() { |
|
|
|
|
ArrayList<String> scoreTable = new ArrayList<>(); |
|
|
|
|
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(" | ") |
|
|
|
|
.append(listScore(student)).append('\n'); |
|
|
|
|
scoreTable.add(this.getStudentInfo(student)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return scoreTable; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取学生信息条目 |
|
|
|
|
* |
|
|
|
|
* @param student 欲获取学生 |
|
|
|
|
* @return 学生信息 |
|
|
|
|
*/ |
|
|
|
|
public String getStudentInfo(Student student) { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
stringBuilder |
|
|
|
|
.append(student.getId()).append(" | ") |
|
|
|
|
.append(student.getName()).append(" | ") |
|
|
|
|
.append(this.getStudentGeneralScore(student)).append(" | ") |
|
|
|
|
.append(listScore(student)); |
|
|
|
|
|
|
|
|
|
return stringBuilder.toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -200,48 +233,83 @@ public class StudentManager { |
|
|
|
|
for (String subject : subjects) { |
|
|
|
|
stringBuilder |
|
|
|
|
.append(subject).append(": ") |
|
|
|
|
.append(student.getScore().get(subject)).append(' '); |
|
|
|
|
.append(student.getScore().get(subject)).append(" "); |
|
|
|
|
} |
|
|
|
|
return stringBuilder.toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 列出所有科目的成绩信息,包括总分、平均分 |
|
|
|
|
* |
|
|
|
|
* @return 信息字符串 |
|
|
|
|
*/ |
|
|
|
|
public String listAllSubjectScore() { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
ArrayList<String> subjects = getSubjects(); |
|
|
|
|
public ArrayList<String> generateAllSubjectInfo() { |
|
|
|
|
ArrayList<String> subjects = this.getSubjects(); |
|
|
|
|
ArrayList<String> record = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
for (String subject : subjects) { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
stringBuilder |
|
|
|
|
.append(subject).append(" | General Score: ") |
|
|
|
|
.append(this.getSubjectScore(subject)).append(" | Avg Score: ") |
|
|
|
|
.append(this.getSubjectAvgScore(subject)).append('\n'); |
|
|
|
|
.append(this.getSubjectAvgScore(subject)); |
|
|
|
|
record.add(stringBuilder.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return stringBuilder.toString(); |
|
|
|
|
return record; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 生成总的学生及科目成绩信息 |
|
|
|
|
* |
|
|
|
|
* @return 信息字符串 |
|
|
|
|
*/ |
|
|
|
|
public String generateAllInformation() { |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
stringBuilder |
|
|
|
|
.append("------------------------------------") |
|
|
|
|
.append(" All Student Information ") |
|
|
|
|
.append("------------------------------------").append('\n') |
|
|
|
|
.append(this.generateScoreTableByName()).append('\n') |
|
|
|
|
.append("------------------------------------") |
|
|
|
|
.append(" All Subject Information ") |
|
|
|
|
.append("------------------------------------").append('\n') |
|
|
|
|
.append(this.listAllSubjectScore()); |
|
|
|
|
.append("------------------------------------\n") |
|
|
|
|
.append(" All Student Information \n") |
|
|
|
|
.append("------------------------------------\n"); |
|
|
|
|
|
|
|
|
|
ArrayList<String> studentTable = this.generateScoreTableByName(); |
|
|
|
|
for (String record : studentTable) { |
|
|
|
|
stringBuilder.append(record).append('\n'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
stringBuilder |
|
|
|
|
.append("------------------------------------\n") |
|
|
|
|
.append(" All Subject Information \n") |
|
|
|
|
.append("------------------------------------\n"); |
|
|
|
|
|
|
|
|
|
ArrayList<String> subjectTable = this.generateAllSubjectInfo(); |
|
|
|
|
for (String record : subjectTable) { |
|
|
|
|
stringBuilder.append(record).append('\n'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return stringBuilder.toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
public ArrayList<Student> getStudents() { |
|
|
|
|
return students; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 通过名字获取学生 |
|
|
|
|
* |
|
|
|
|
* @param name 学生姓名 |
|
|
|
|
* @return 学生类 |
|
|
|
|
*/ |
|
|
|
|
public Student getStudent(String name) { |
|
|
|
|
return managerTool.getStudent(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 通过学号获取学生 |
|
|
|
|
* |
|
|
|
|
* @param id 学号 |
|
|
|
|
* @return 学生类 |
|
|
|
|
*/ |
|
|
|
|
public Student getStudent(long id) { |
|
|
|
|
return managerTool.getStudent(id); |
|
|
|
|
} |
|
|
|
|
} |