1. //EmpReader.java
    2. package edu.hue.jk.io;
    3. import java.io.BufferedReader;
    4. import java.io.File;
    5. import java.io.FileNotFoundException;
    6. import java.io.FileReader;
    7. import java.io.IOException;
    8. import java.sql.Date;
    9. import java.util.Collection;
    10. import java.util.HashMap;
    11. import edu.hue.jk.bean.Emp;
    12. public class EmpReader {
    13. public HashMap<Integer, Emp> read(File inFile) {
    14. HashMap<Integer, Emp> empMap = new HashMap<>();
    15. BufferedReader reader = null;
    16. String line;
    17. try {
    18. reader = new BufferedReader(new FileReader(inFile));
    19. reader.readLine(); //跳过第一行
    20. String[] items;
    21. Emp emp;
    22. while ((line = reader.readLine()) != null) {
    23. //使用item=line.split("\t");将读入的数据行按照制表符切割成一组字符串数组
    24. items = line.split("\t");
    25. Integer empno = Integer.parseInt(items[0]);
    26. String ename = items[1];
    27. String job = items[2];
    28. Integer mgr = items[3].equals("") ? null : Integer.parseInt(items[3]);
    29. Date hiredate = Date.valueOf(items[4]);
    30. Double sal = items[5].equals("") ? null : Double.parseDouble(items[5]);
    31. Double comm = items[6].equals("") ? null : Double.parseDouble(items[6]);
    32. Integer deptno = items[7].equals("") ? null : Integer.parseInt(items[7]);
    33. emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
    34. empMap.put(empno, emp);
    35. }
    36. } catch (FileNotFoundException e) {
    37. // TODO Auto-generated catch block
    38. e.printStackTrace();
    39. } catch (IOException e) {
    40. // TODO Auto-generated catch block
    41. e.printStackTrace();
    42. } finally {
    43. if (reader != null) {
    44. try {
    45. reader.close();
    46. } catch (IOException e) {
    47. // TODO Auto-generated catch block
    48. e.printStackTrace();
    49. }
    50. }
    51. }
    52. return empMap;
    53. }
    54. public static void main(String[] args) {
    55. File inFile = new File("etc/emp.txt");
    56. EmpReader reader = new EmpReader();
    57. HashMap<Integer, Emp> empMap = reader.read(inFile);
    58. Collection<Emp> empSet = empMap.values();
    59. for (Emp e : empSet) {
    60. System.out.println(e);
    61. }
    62. }
    63. }
    1. //DeptReader.java
    2. package edu.hue.jk.io;
    3. import java.io.BufferedReader;
    4. import java.io.File;
    5. import java.io.FileNotFoundException;
    6. import java.io.FileReader;
    7. import java.io.IOException;
    8. import java.util.Collection;
    9. import java.util.HashMap;
    10. import edu.hue.jk.bean.Dept;
    11. public class DeptReader {
    12. public HashMap<Integer, Dept> read(File inFile) {
    13. HashMap<Integer, Dept> deptMap = new HashMap<>();
    14. BufferedReader reader = null;
    15. String line;
    16. try {
    17. reader = new BufferedReader(new FileReader(inFile));
    18. reader.readLine();
    19. String[] items;
    20. Dept dept;
    21. while ((line = reader.readLine()) != null) {
    22. items = line.split("\t");
    23. Integer deptno = Integer.parseInt(items[0]);
    24. String dname = items[1];
    25. String loc = items[2];
    26. dept = new Dept(deptno, dname, loc);
    27. deptMap.put(deptno, dept);
    28. }
    29. } catch (FileNotFoundException e) {
    30. e.printStackTrace();
    31. } catch (IOException e) {
    32. e.printStackTrace();
    33. } finally {
    34. if (reader != null) {
    35. try {
    36. reader.close();
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. }
    42. return deptMap;
    43. }
    44. public static void main(String[] args) {
    45. File inFile = new File("etc/dept.txt");
    46. DeptReader reader = new DeptReader();
    47. HashMap<Integer, Dept> deptMap = reader.read(inFile);
    48. Collection<Dept> deptSet = deptMap.values();
    49. for (Dept e : deptSet) {
    50. System.out.println(e);
    51. }
    52. }
    53. }
    1. //MainApp.java
    2. package edu.hue.jk.dao;
    3. import java.io.File;
    4. import java.sql.Date;
    5. import java.util.HashMap;
    6. import edu.hue.jk.bean.Dept;
    7. import edu.hue.jk.bean.Emp;
    8. import edu.hue.jk.io.DeptReader;
    9. import edu.hue.jk.io.EmpReader;
    10. public class MainApp {
    11. public static void main(String[] args) {
    12. File empinFile = new File("etc/emp.txt");
    13. EmpReader empreader = new EmpReader();
    14. HashMap<Integer, Emp> empMap = empreader.read(empinFile);
    15. File deptinFile = new File("etc/dept.txt");
    16. DeptReader deptreader = new DeptReader();
    17. HashMap<Integer, Dept> deptMap = deptreader.read(deptinFile);
    18. System.out.println("DEPT_NAME\tEMPLPYEE");
    19. for (Emp e : empMap.values()) {
    20. if (e.getComm()==null &&
    21. (e.getSal()<2000 ||
    22. e.getHiredate().compareTo(Date.valueOf("1981-05-01"))>=0 &&
    23. e.getHiredate().compareTo(Date.valueOf("1981-11-30"))<=0)) {
    24. Integer deptno = e.getDeptno();
    25. Dept dept = deptMap.get(deptno);
    26. System.out.println(dept.getDname() + "\t" + e.getEname());
    27. }
    28. }
    29. }
    30. }
    1. //EmpDeptVO.java
    2. package edu.hue.jk.dao;
    3. public class EmpDeptVO implements Comparable<EmpDeptVO> {
    4. private String deptt;
    5. private Integer count;
    6. public EmpDeptVO(String deptt, Integer count) {
    7. super();
    8. this.deptt = deptt;
    9. this.count = count;
    10. }
    11. public String getDeptt() {
    12. return deptt;
    13. }
    14. public void setDeptt(String deptt) {
    15. this.deptt = deptt;
    16. }
    17. public Integer getCount() {
    18. return count;
    19. }
    20. public void setCount(Integer count) {
    21. this.count = count;
    22. }
    23. @Override
    24. public String toString() {
    25. return "EmpDeptVO [deptt=" + deptt + ", count=" + count + "]";
    26. }
    27. @Override
    28. public int compareTo(EmpDeptVO o) {
    29. // this为当前对象,o为目标对象,比较两者count,实现降序排列
    30. return (this.getCount()-o.getCount()) * -1;
    31. }
    32. }
    1. //MainApp2.java
    2. package edu.hue.jk.dao;
    3. import java.io.File;
    4. import java.sql.Date;
    5. import java.util.ArrayList;
    6. import java.util.Arrays;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import edu.hue.jk.bean.Dept;
    10. import edu.hue.jk.bean.Emp;
    11. import edu.hue.jk.io.DeptReader;
    12. import edu.hue.jk.io.EmpReader;
    13. public class MainApp2 {
    14. public static void main(String[] args) {
    15. File empinFile = new File("etc/emp.txt");
    16. EmpReader empreader = new EmpReader();
    17. HashMap<Integer, Emp> empMap = empreader.read(empinFile);
    18. File deptinFile = new File("etc/dept.txt");
    19. DeptReader deptreader = new DeptReader();
    20. HashMap<Integer, Dept> deptMap = deptreader.read(deptinFile);
    21. HashMap<String, List<Emp>> data = new HashMap<>();
    22. for (Emp emp : empMap.values()) {
    23. if (emp.getComm()==null && (emp.getSal()<2000 || emp.getHiredate().compareTo(Date.valueOf("1981-05-01"))>=0 && emp.getHiredate().compareTo(Date.valueOf("1981-11-30"))<=0)) {
    24. Integer deptno = emp.getDeptno();
    25. Dept dept = deptMap.get(deptno);
    26. if (data.containsKey(dept.getDname())) {
    27. List<Emp> empList = data.get(dept.getDname());
    28. empList.add(emp);
    29. } else {
    30. List<Emp> empList = new ArrayList<Emp>();
    31. empList.add(emp);
    32. data.put(dept.getDname(), empList);
    33. }
    34. }
    35. }
    36. List<EmpDeptVO> voList = new ArrayList<EmpDeptVO>();
    37. for (String Dept : data.keySet()) {
    38. voList.add(new EmpDeptVO(Dept, data.get(Dept).size()));
    39. }
    40. Object[] outList = voList.toArray();
    41. Arrays.sort(outList);
    42. System.out.println("DEPT_NAME" + "\t" + "total");
    43. for (Object vo : outList) {
    44. System.out.println(((EmpDeptVO)vo).getDeptt() + "\t" + ((EmpDeptVO)vo).getCount());
    45. }
    46. }
    47. }
    1. //emp.txt
    2. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    3. 7369 SMITH CLERK 7902 1980-12-17 800 20
    4. 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
    5. 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
    6. 7566 JONES MANAGER 7839 1981-04-02 2975 20
    7. 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
    8. 7698 BLAKE MANAGER 7839 1981-05-01 2850 30
    9. 7782 CLARK MANAGER 7839 1981-06-09 2450 10
    10. 7839 KING PRESIDENT 1981-11-17 5000 10
    11. 7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
    12. 7900 JAMES CLERK 7698 1981-12-03 950 30
    13. 7902 FORD ANALYST 7566 1981-12-03 3000 20
    14. 7934 MILLER CLERK 7782 1982-01-23 1300 10
    1. //DEPT.txt
    2. DEPTNO DNAME LOC
    3. 10 ACCOUNTING NEW YORK
    4. 20 RESEARCH DALLAS
    5. 30 SALES CHICAGO
    6. 40 OPERATIONS BOSTON

    image.png