Implementation of employee management system in Java

 The implementation of the employee management system is relatively simple. Create an employee class, which contains the employee's job number, name, age, gender and professional attributes

public class Staff {
    private int id;
    private String name;
    private int age;
    private String sex;
    private String specialty;

    public Staff(int id, String name, int age, String sex, String specialty) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.specialty = specialty;
    }
    public Staff(){

    }
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getSpecialty() {
        return specialty;
    }
    public void setSpecialty(String specialty) {
        this.specialty = specialty;
    }
}

After setting the employee's attributes, it is necessary to store the employee's information. I use the ArrayList dynamic array to store the employee's information

The ArrayList dynamic array is quite powerful. It is not like an array to define the length of the array, and it is very operable, with many methods, such as add(), clear(), get()...

public class StaffManager {
    public static void main(String[] args) {
        ArrayList<Staff> array = new ArrayList<>();
        while (true) {
            System.out.println("--------欢迎来到员工管理系统--------");
            System.out.println("1 添加员工");
            System.out.println("2 删除员工");
            System.out.println("3 修改员工");
            System.out.println("4 查看所有员工");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line) {
                case "1":
                    System.out.println("添加员工");
                    add(array);
                    break;
                case "2":
                    delete(array);
                    break;
                case "3":
                    update(array);
                    break;
                case "4":
                    find(array);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    System.exit(0);
            }
        }
    }
    public static void add(ArrayList<Staff> array) {
        Scanner sc = new Scanner(System.in);
        String id;

        while (true) {
            System.out.println("请输入员工号:");
            id = sc.nextLine();

            boolean flag = isUsed(array, id);
            if (flag) {
                System.out.println("你输入的员工号已经被使用,请重新输入");
            } else {
                break;
            }
        }

        System.out.println("请输入员工姓名:");
        String name = sc.nextLine();
        System.out.println("请输入员工年龄:");
        String age = sc.nextLine();
        System.out.println("请输入员工性别:");
        String sex = sc.nextLine();
        System.out.println("请输入员工专业:");
        String specialty = sc.nextLine();
        Staff s = new Staff();
        s.setId(Integer.parseInt(id));
        s.setName(name);
        s.setAge(Integer.parseInt(age));
        s.setSex(sex);
        s.setSpecialty(specialty);
        array.add(s);
        System.out.println("添加员工成功");
    }
    public static boolean isUsed(ArrayList<Staff> array, String id) {
        boolean flag = false;

        for (int i = 0; i < array.size(); i++) {
            Staff s = array.get(i);
            if (s.getId()==Integer.parseInt(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }
    public static void find(ArrayList<Staff> array) {
        if (array.size() == 0) {
            System.out.println("无信息,请先添加信息再查询");
        } else {
            System.out.println("员工号\t\t姓名\t\t\t\t年龄\t\t\t\t\t性别\t\t\t\t\t专业");
            for (int i = 0; i < array.size(); i++) {
                Staff s = array.get(i);
                System.out.println(s.getId() + "\t\t\t" + s.getName() + "\t\t\t\t" + s.getAge() + "岁\t\t\t\t" + s.getSex()+"\t\t\t\t"+s.getSpecialty());
            }
        }
    }
    public static void delete(ArrayList<Staff> array) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入你要删除的员工号:");
        String id = sc.nextLine();

        int index = -1;

        for (int i = 0; i < array.size(); i++) {
            Staff s = array.get(i);
            if (s.getId()==(Integer.parseInt(id))) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("该员工不存在,请重新输入");
        } else {
            array.remove(index);

            System.out.println("删除成功");
        }
    }
    public static void update(ArrayList<Staff> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的员工号:");
        String id = sc.nextLine();

        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Staff staff = array.get(i);
            if (staff.getId()==(Integer.parseInt(id))) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该员工不存在,请重新输入");
        } else {
            System.out.println("请输入员工新的姓名:");
            String name = sc.nextLine();
            System.out.println("请输入员工新的年龄:");
            String age = sc.nextLine();
            System.out.println("请输入员工新的性别:");
            String sex = sc.nextLine();
            System.out.println("请输入员工新的专业:");
            String specialty = sc.nextLine();
            Staff s = new Staff();
            s.setId(Integer.parseInt(id));
            s.setName(name);
            s.setAge(Integer.parseInt(age));
            s.setSex(sex);
            s.setSpecialty(specialty);
            array.set(index, s);
            System.out.println("修改员工信息成功");
        }
    }
}
The subsequent addition, deletion and modification of employee information is an operation on the dynamic array

 

Let me talk about my understanding of dynamic arrays. It is purely a personal opinion. If I am wrong, please give me pointers.

Ordinary one-dimensional arrays, such as int[ ], String[ ], char[ ] arrays, the length of this array has been fixed before use and cannot be changed. Just like building a house, this house has only one floor and its length cannot be expanded, so the number of rooms inside is also a fixed value, and the number of rooms here is also the length of the array.

And the dynamic array, like our dynamic array here, stores the Staff class, and the Staff class has five attributes, so the length of the dynamic array is given, even if there are only five rooms on the first floor, but it Unlike normal 1D arrays, it can increase its height

 

 That is, like this, its width cannot be changed, but its height can be increased or decreased according to needs, so it is called a dynamic array

Guess you like

Origin blog.csdn.net/weixin_54284906/article/details/125218595