模拟数据如下:
001|张三|85.5|79.0|92.0
002|李四|58.0|99.0|78.5
003|王五|88.0|51.0|63.0
004|孙六|84.0|88.0|92.0
005|杜七|52.0|51.0|43.0
006|刘八|96.0|100.0|99.0
007|钱九|61.0|77.0|79.5
008|周十|59.5|60.0|62.0
009|吴邪|88.0|92.0|75.0
010|郑州|78.0|91.0|99.0import java.math.BigDecimal;
/**
* 学生类
*/
public class Student {
/**
* 学号
*/
private String code;
/**
* 姓名
*/
private String name;
/**
* 语文
*/
private double chinese;
/**
* 数学
*/
private double math;
/**
* 英语
*/
private double english;
/**
* 总分
*/
private double total;
/**
* 平均分
*/
private double average;
public Student() {
}
public Student(String code, String name, double chinese, double math, double english) {
this.code = code;
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public Student(String[] properties) {
this(properties[0], properties[1],
new BigDecimal(properties[2]).doubleValue(),
new BigDecimal(properties[3]).doubleValue(),
new BigDecimal(properties[4]).doubleValue());
}
public double calculateTotal() {
this.total = this.chinese + this.math + this.english;
return this.total;
}
public double calculateAverage() {
average = (this.chinese + this.math + this.english) / 3;
average = BigDecimal.valueOf(average).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return average;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
}import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
/**
* 根据实际情况修改路径,我是MAC系统
*/
private static final String DATA_INPUT_FILE = "/Users/liuchongguang/Downloads/scores.txt";
/**
* 根据实际情况修改路径,我是MAC系统
*/
private static final String DATA_OUTPUT_FILE = "/Users/liuchongguang/Downloads/scores_result.txt";
private static List<Student> loadStudents(String filePath) {
List<Student> students = new ArrayList<Student>();
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
is = new FileInputStream(new File(filePath));
isr = new InputStreamReader(is, "UTF-8");
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
String[] properties = line.split("\\|");
students.add(new Student(properties));
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
br.close();
isr.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return students;
}
private static void saveStudents(String filePath, List<Student> students, Map<String, Map<Double, List<Student>>> totalStudents,
Map<String, Map<Double, List<Student>>> chineseStudents, Map<String, Map<Double, List<Student>>> mathStudents,
Map<String, Map<Double, List<Student>>> englishStudents) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();// 不存在则创建
}
fw = new FileWriter(file, false);
bw = new BufferedWriter(fw);
for (Student student : students) {
bw.write("[学号:" + student.getCode() + "," + student.getName() + "] 语文:"
+ student.getChinese() + ",数学:" + student.getMath() + ",英语:" + student.getEnglish() +
" 总分:" + student.getTotal() + ",平均分:" + student.getAverage() + "\r\n");
}
bw.write("\r\n");
saveStudents(bw, chineseStudents, "语文");
saveStudents(bw, mathStudents, "数学");
saveStudents(bw, englishStudents, "英语");
saveStudents(bw, totalStudents, "总分");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void saveStudents(BufferedWriter bw, Map<String, Map<Double, List<Student>>> map, String language) throws Exception {
bw.write("==============================" + language + "成绩统计==============================\r\n");
Map<Double, List<Student>> highMap = map.get("high");
Double score = (Double) highMap.keySet().toArray()[0];
List<Student> students1 = highMap.get(score);
bw.write(language + "成绩最高分:" + score + ",获得者:");
for (Student s : students1) {
bw.write("[学号:" + s.getCode() + "," + s.getName() + "] ");
}
bw.write("\r\n");
Map<Double, List<Student>> lowMap = map.get("low");
score = (Double) lowMap.keySet().toArray()[0];
students1 = lowMap.get(score);
bw.write(language + "成绩最低分:" + score + ",获得者:");
for (Student s : students1) {
bw.write("[学号:" + s.getCode() + "," + s.getName() + "] ");
}
bw.write("\r\n");
bw.write("\r\n");
}
private static void calculate(List<Student> students, Map<String, Map<Double, List<Student>>> totalStudents,
Map<String, Map<Double, List<Student>>> chineseStudents,
Map<String, Map<Double, List<Student>>> mathStudents,
Map<String, Map<Double, List<Student>>> englishStudents) {
for (Student student : students) {
student.calculateTotal();
student.calculateAverage();
compare(totalStudents, student.getTotal(), student);
compare(chineseStudents, student.getChinese(), student);
compare(mathStudents, student.getMath(), student);
compare(englishStudents, student.getEnglish(), student);
}
}
private static void compare(Map<String, Map<Double, List<Student>>> map, double score, Student student) {
if (map.keySet().size() == 0) {
HashMap<Double, List<Student>> highMap = new HashMap<Double, List<Student>>();
HashMap<Double, List<Student>> lowMap = new HashMap<Double, List<Student>>();
List<Student> students = new ArrayList<Student>();
students.add(student);
highMap.put(score, students);
lowMap.put(score, students);
map.put("high", highMap);
map.put("low", lowMap);
} else {
Map<Double, List<Student>> highMap = map.get("high");
Double key = (Double) highMap.keySet().toArray()[0];
if (key == score) {
List<Student> students = highMap.get(key);
students.add(student);
} else if (key < score) {
highMap.clear();
highMap = new HashMap<Double, List<Student>>();
List<Student> students = new ArrayList<Student>();
students.add(student);
highMap.put(score, students);
map.put("high", highMap);
}
Map<Double, List<Student>> lowMap = map.get("low");
key = (Double) lowMap.keySet().toArray()[0];
if (key == score) {
List<Student> students = lowMap.get(key);
students.add(student);
} else if (key > score) {
lowMap.clear();
lowMap = new HashMap<Double, List<Student>>();
List<Student> students = new ArrayList<Student>();
students.add(student);
lowMap.put(score, students);
map.put("low", lowMap);
}
}
}
public static void main(String[] args) {
//从文件中读取学生数据
List<Student> students = loadStudents(DATA_INPUT_FILE);
//总分最高分和最低分
Map<String, Map<Double, List<Student>>> totalStudents = new HashMap<String, Map<Double, List<Student>>>();
//语文最高分和最低分
Map<String, Map<Double, List<Student>>> chineseStudents = new HashMap<String, Map<Double, List<Student>>>();
//数学最高分和最低分
Map<String, Map<Double, List<Student>>> mathStudents = new HashMap<String, Map<Double, List<Student>>>();
//英语最高分和最低分
Map<String, Map<Double, List<Student>>> englishStudents = new HashMap<String, Map<Double, List<Student>>>();
//计算结果
calculate(students, totalStudents, chineseStudents, mathStudents, englishStudents);
//保存结果至文件
saveStudents(DATA_OUTPUT_FILE, students, totalStudents, chineseStudents, mathStudents, englishStudents);
}
}