package com.sf._02_字节流输出流;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test2 {
    /**
     * 创建Student stu(id,name,age,score)
     * 创建出来一个List<Student> list -> 添加3个学生分数要是一个无需
     *
     * 把集合信息最终写到student.txt 文件中
     *
     * 分数要从大到小降序排序
     * id    name    age   score    表头
     * 1    zhangsan 10    99
     * 2     lisi    20    98
     * 3     wangwu  10    90
     *
     * 最大分数:   99
     * 最小分数;   90
     * 平均分数:   xxx
     * 总分:   xxxx
     * 总人数: xxx
     */
    public static void main(String[] args) throws IOException {
        // 先把学生放到集合中
        List<Student> list = new ArrayList<>();
        list.add(new Student(1L,"zhangsan",15,90.0));
        list.add(new Student(2L,"lisi",16,80.0));
        list.add(new Student(3L,"wangwu",14,99.0));
        // 堆集合进行排序
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return (int) (o2.getScore()-o1.getScore());
            }
        });
        System.out.println(list);
        // 求出来最大值
        Student maxStudent = list.get(0);
        // 求最小值
        Student minStudent = list.get(list.size()-1);
        // 求总人数
        int totalStudents = list.size();
        // 总分
        double totalScore = 0;
        for (Student student : list) {
            totalScore += student.getScore();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\student.txt"),true);
        fileOutputStream.write("学生信息列表".getBytes());
        fileOutputStream.write(System.lineSeparator().getBytes());

        // 写表头
        String headStr = "id       name        age       score";
        fileOutputStream.write(headStr.getBytes());
        fileOutputStream.write(System.lineSeparator().getBytes());
        //写表格内容
        for (Student student : list) {
            fileOutputStream.write((student.getId()+"       "+student.getName()+"        "+student.getAge()+"       "+student.getScore()).getBytes());
            fileOutputStream.write(System.lineSeparator().getBytes());
        }
        //写其他信息
        fileOutputStream.write(System.lineSeparator().getBytes());
        fileOutputStream.write(("最大分数:"+ maxStudent.getScore()).getBytes());
        fileOutputStream.write(System.lineSeparator().getBytes());
        fileOutputStream.write(("最小分数:"+ minStudent.getScore()).getBytes());
        fileOutputStream.write(System.lineSeparator().getBytes());
        fileOutputStream.write(("平均分:"+ totalScore/list.size()).getBytes());
        fileOutputStream.write(System.lineSeparator().getBytes());
        fileOutputStream.write(("总分:"+ totalScore).getBytes());
        fileOutputStream.write(System.lineSeparator().getBytes());
        fileOutputStream.write(("总人数:"+ list.size()).getBytes());



    }
}
