package com.sf._03_properties_config;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

public class Test1 {
    /**
     * 通过类加载器方式去加载配置文件
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // 通过类加载器加载配置文件
        /**
         * Thread.currentThread()  获取当前线程
         * getContextClassLoader()  获取当前的这个类加载器 classLoader
         * getResourceAsStream()   通过类加载器上resources 目录找到配置, 把配置文件信息加载输入流中
         *
         * 类加载器大家不需要知道是什么
         * 他里面提供方法可以让我们去resources 中加载信息不需要去写具体盘符路径
         */
        InputStream inputStream = Thread.currentThread().
                getContextClassLoader().
                getResourceAsStream("employee.properties");
        Properties properties = new Properties();
        // 加载
        properties.load(inputStream);
        System.out.println(properties.getProperty("id"));
        inputStream.close();
    }


    /**
     * 1 employee.properties 文件中
     * employee1_id=1
     * employee1_name=zhangsan
     * employee1_age=10
     * employee1_email=123@qq.com
     *
     * employee2_id=2
     * employee2_name=wangwu
     * employee2_age=20
     * employee2_email=123@qq.com
     *
     * employee3_id=3
     * employee3_name=zhaoliu
     * employee3_age=17
     * employee3_email=123@qq.com
     *
     * 要求把配置文件当中属性存储到一个List<Employee> 集合中
     * 要求1 从大到小排序   最大年龄是employee   最小年龄employee
     *
     *
     */
    @Test
    public void test1() throws IOException {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("employee.properties");
        //加载配置文件
        Properties p = new Properties();
        p.load(inputStream);

        List<Employee> employees = new ArrayList<>();
        Employee employee1 = new Employee();
        // 从配置文件中获取属性并且设置
        employee1.setId(Long.valueOf(p.getProperty("employee1_id")));
        employee1.setName(p.getProperty("employee1_name"));
        employee1.setEmail(p.getProperty("employee1_email"));
        employee1.setAge(Integer.valueOf(p.getProperty("employee1_age")));
        // 把employee 添加结合中
        employees.add(employee1);

        Employee employee2 = new Employee();
        // 从配置文件中获取属性并且设置
        employee2.setId(Long.valueOf(p.getProperty("employee2_id")));
        employee2.setName(p.getProperty("employee2_name"));
        employee2.setEmail(p.getProperty("employee2_email"));
        employee2.setAge(Integer.valueOf(p.getProperty("employee2_age")));
        // 把employee 添加结合中
        employees.add(employee2);

        Employee employee3 = new Employee();
        // 从配置文件中获取属性并且设置
        employee3.setId(Long.valueOf(p.getProperty("employee3_id")));
        employee3.setName(p.getProperty("employee3_name"));
        employee3.setEmail(p.getProperty("employee3_email"));
        employee3.setAge(Integer.valueOf(p.getProperty("employee3_age")));
        // 把employee 添加结合中
        employees.add(employee3);



        // 调用工具类排序
        Collections.sort(employees);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
        // 关闭资源
        inputStream.close();

    }
}
