线下编程机试题一枚。

2019-02-27 11:53:17来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

一个data.txt文件,内容类似如下 第一列为员工名,第二列为工号,第三列为年龄,第四列为其汇报的上级工号。

fff, 111, 20
eee, 222, 21, 111
ddd, 333, 22, 222
ccc, 444, 23, 222
bbb, 555, 24, 111
aaa, 666, 25
ggg, 777, 26, 444
hhh, 888, 27, 555

要求在终端输入工号时,显示该人信息的记录。

输入All时,显示所有人的姓名的层级架构图,有缩进,同级按姓名字母顺序输出。

如:

aaa
fff
  bbb
    hhh
  eee
    ccc
      ggg
    ddd

输入exit时,退出。

否则可以反复查询。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
    private static HashMap<Integer, Employee> map = new HashMap<Integer, Employee>();

    public static void main(String[] args) throws IOException {
        // load file into map
        System.out.println("File loading...");
        loadFile();
        System.out.println("File load succeed.");
        System.out.println("Please input employee's id:");
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String temp = sc.next();
            if (temp.equals("exit")) {
                System.out.println("Exit succeed.");
                break;
            } else if (temp.equals("All")) {
                System.out.println("All employees:");
                showAll();
            } else if (checkIdLegal(temp)) {
                int id = Integer.parseInt(temp);
                if (findId(id)) {
                    Employee e = map.get(id);
                    e.print();
                } else {
                    System.out.println("This id is not exist.");
                    System.out.println("Please input employee's id:");
                }
            } else {
                System.out.println("Your input is illegal.");
                System.out.println("Please input employee's id:");
            }
        }
        sc.close();
    }
    
    // check if the id is exist. 
    private static boolean findId(int id) {
        if (map.containsKey(id)) {
            return true;
        }
        return false;
    }

    // check if the input id is numeric.
    private static boolean checkIdLegal(String temp) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(temp).matches();
    }

    // show all employees' information.
    private static void showAll() {
        // find the leader first.
        Iterator<Map.Entry<Integer, Employee>> it = map.entrySet().iterator();
        ArrayList<Employee> leaders = new ArrayList<Employee>();
        while (it.hasNext()) {
            Map.Entry<Integer, Employee> entry = it.next();
            Employee e = entry.getValue();
            if (e.getSupId() == 0) {
                leaders.add(e);
            }
        }
        Collections.sort(leaders);
        for (int i = 0; i < leaders.size(); i++) {
            Employee leader = leaders.get(i);
            leader.printName();
            showSub(leader, 1);
        }
    }

    private static void showSub(Employee e, int tab) {
        ArrayList<Integer> subList = e.getSubId();
        ArrayList<Employee> subEmployees = new ArrayList<Employee>();
        for (int i = 0; i < subList.size(); i++) {
            subEmployees.add(map.get(subList.get(i)));
        }
        Collections.sort(subEmployees);
        for (int i = 0; i < subEmployees.size(); i++) {
            Employee subEmployee = subEmployees.get(i);
            for (int j = 0; j < tab; j++) {
                System.out.print("\t");
            }
            subEmployee.printName();
            showSub(subEmployee, tab + 1);
        }
    }

    // load file into map.
    private static void loadFile() throws FileNotFoundException, IOException {
        String filePath = "C:/Users/chenyangwang/Downloads/data.txt";
        FileInputStream fis = new FileInputStream(filePath);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String str = "";
        while ((str = br.readLine()) != null) {
            String[] strLine = str.split(",");
            for (int i = 0; i < strLine.length; i++) {
                strLine[i] = strLine[i].trim();
            }
            // if the employee has no superior, is a leader.
            if (strLine.length == 3) {
                String name = strLine[0];
                int id = Integer.parseInt(strLine[1]);
                int age = Integer.parseInt(strLine[2]);
                // see if the map contains the employee.
                if (map.containsKey(id)) {
                    Employee e = map.get(id);
                    e.setName(name);
                    e.setId(id);
                    e.setAge(age);
                    e.setSupId(0);
                    map.put(id, e);
                } else {
                    Employee e = new Employee(name, id, age, 0);// make the leader's supId 0.
                    map.put(id, e);
                }
            } else {
                // the employee has a superior.
                String name = strLine[0];
                int id = Integer.parseInt(strLine[1]);
                int age = Integer.parseInt(strLine[2]);
                int supId = Integer.parseInt(strLine[3]);
                // see if the map contains the employee.
                if (map.containsKey(id)) {
                    Employee e = map.get(id);
                    e.setName(name);
                    e.setId(id);
                    e.setAge(age);
                    e.setSupId(supId);
                    map.put(id, e);
                } else {
                    Employee e = new Employee(name, id, age, supId);
                    map.put(id, e);
                }
                // update the superior's subId. if not exist, create it.
                if (map.containsKey(supId)) {
                    Employee superior = map.get(supId);
                    superior.addSubId(id);
                    map.put(supId, superior);
                } else {
                    Employee superior = new Employee("", -1, -1, -1);
                    superior.addSubId(id);
                    map.put(supId, superior);
                }
            }
        }
        br.close();
    }
}
import java.util.ArrayList;

public class Employee implements Comparable<Employee>{
    private String name;
    private int id;
    private int age;
    private int supId;
    private ArrayList<Integer> subId;

    public Employee(String name, int id, int age, int supId) {
        this.name = name;
        this.id = id;
        this.age = age;
        this.supId = supId;
        this.subId = new ArrayList<Integer>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSupId() {
        return supId;
    }

    public void setSupId(int supId) {
        this.supId = supId;
    }

    public ArrayList<Integer> getSubId() {
        return subId;
    }

    public void setSubId(ArrayList<Integer> subId) {
        this.subId = subId;
    }

    public void addSubId(Integer id) {
        this.subId.add(id);
    }

    // output the employee's information.
    public void print() {
        System.out.println("Name:" + this.name + " Id:" + this.id + " Age:" + this.age + " SupId:" + this.supId);
    }
    
    // output the employee's name.
    public void printName(){
        System.out.println(this.name);
    }

    // sort by name.
    @Override
    public int compareTo(Employee e) {
        return this.getName().compareTo(((Employee) e).getName());
    }

}

 


原文链接:https://www.cnblogs.com/chang-kai/p/10442018.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法

下一篇:spring_08aop原理及案例