
import java.util.*;
import java.util.stream.Stream;
import static java.lang.Double.MAX_VALUE;
import static java.lang.Double.MIN_VALUE;
abstract class Score{
Score(String courseName) {
}
abstract String getCourseName();
abstract void setCourseName(String courseName);
abstract Score add(String studentName, double score);
abstract Score remove(String studentName);
abstract String[] getStudentNames(boolean... isDesc);
abstract double[] getStudentScores(boolean... isDesc);
abstract double min();
abstract double max();
abstract double avg();
public static Score begin(String courseName){
return new subClass(courseName);
}
}
class subClass extends Score{
Map<String,Double> map;
String courseName;
int length;
List<String>sname;
subClass(String courseName) {
super(courseName);
this.courseName = courseName;
this.map = new HashMap<>();
this.sname = new ArrayList<>();
this.length = 0;
}
@Override
String getCourseName() {
return this.courseName;
}
@Override
void setCourseName(String courseName) {
this.courseName = courseName;
}
@Override
Score add(String studentName, double score) {
this.map.put(studentName,score);
this.sname.add(studentName);
this.length+=1;
return this;
}
@Override
Score remove(String studentName) {
if(this.sname.contains(studentName)) {
this.map.remove(studentName);
this.sname.remove(studentName);
this.length -= 1;
}
return this;
}
@Override
String[] getStudentNames(boolean... isDesc) {
int length = this.length;
String[]res = new String[length];
// 转换成list 排序 小到大
List<Map.Entry<String, Double>> lst = new ArrayList<Map.Entry<String, Double>>(this.map.entrySet());
if((isDesc != null) && (isDesc.length>0)) {
if (!isDesc[0]) // 升序
lst.sort(new Comparator<Map.Entry<String, Double>>() {
public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
else//减序
lst.sort(new Comparator<Map.Entry<String, Double>>() {
public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});
for (int i = 0; i < length; i++) {
res[i] = lst.get(i).getKey();
}
}else{
for (int i = 0; i < length; i++) {
res[i] = this.sname.get(i);
}
}
return res;
}
@Override
double[] getStudentScores(boolean... isDesc) {
int length = this.length;
int i=0;
if((isDesc != null) && (isDesc.length>0)) {
Double[]res = new Double[length];
for(double value : map.values()) {
res[i++] = value;
}
if (isDesc[0])// 降序
Arrays.sort(res, Collections.reverseOrder());
else
Arrays.sort(res);//ascending
return Stream.of(res).mapToDouble(Double::doubleValue).toArray();
}else{
double[]res= new double[length];
for (i = 0; i < length; i++) {
res[i] = this.map.get(this.sname.get(i));
}
return res;
}
}
@Override
double min() {
double value = MAX_VALUE;
for(double v : map.values()) {
if(v<value) value=v;
}
return value;
}
@Override
double max() {
double value = MIN_VALUE;
for(double v : map.values()) {
if(v>value) value=v;
}
return value;
}
@Override
double avg() {
int length = this.length;
double sum=0;
for(double value : map.values()) {
sum += value;
}
return sum/length;
}
}
public class Test {
public static void main(String[]args){
Score score= Score. begin("Java语言程序设计");
score.add("张伞",98)
.add("李思",86)
.add("王伍",90)
.add("赵陆",52)
.add("刘齐",23);
//以下语句应能输出Java语言程序设计的最高分 98,最低分23,平均分69.8°
System.out.printf("%s的最高分%g,最低分%g 平均分%g", score.getCourseName(), score.max(), score.min(), score.avg());
System.out.println("\n参与测试的学生名单:");
for(String n:score.getStudentNames()){
System.out.printf("%s ",n);
}
System.out.println("\n对应成绩是:");
for(double s:score.getStudentScores()){
System.out.printf("%g ",s);
}
score.remove("刘齐").remove("赵陆");
System.out.println("\n考试通过的学生成绩(由高到低):");
String[]names=score.getStudentNames(true);
double[]scores=score.getStudentScores(true);
//以下的循环应能输出:张伞98,王伍90,李思
for(int i=0; i< names.length; i++)
System.out.printf("%s %g,", names[i], scores[i]);
System.out.println("\n成绩由低到高排列是:");
//以下循环语句应能输出:李思王伍张伞
for(String name: score.getStudentNames(false))
System.out.printf("%s ", name);
//以下输出语句应能输出:最高分数是98
System.out.println("\n最高分数是" + score.getStudentScores(false)[names. length-1]);
}
}
