面向对象--员工管理系统(基础实现)

员工管理系统(对象实现)

可以参考本人的博客《面向过程--员工管理系统》
https://blog.csdn.net/qq_43804919/article/details/120969915?spm=1001.2014.3001.5501
对比面向对象和面向过程的区别。

员工类

package com.situ.chapterwork;


public class Employee extends Department {
    private String name;
    private String sex;
    private int age;
    private float salary;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }
}


部门类

package com.situ.chapterwork;

public class Department {
	private int did;
	private String dname;
	private int sum;
	
	public int getDid() {
		return did;
	}
	public void setDid(int did) {
		this.did = did;
	}
	public String getDName() {
		return dname;
	}
	public void setDName(String dname) {
		this.dname = dname;
	}
	public int getSum() {
		return sum;
	}
	public void setSum(int sum) {
		this.sum = sum;
	}
	
}

薪资排序接口

package com.situ.chapterwork;

import java.util.Comparator;

public class MyComparator implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        Employee e1 = (Employee)o1;
        Employee e2 = (Employee)o1;
        if (e1.getSalary() < e2.getSalary()) {
            return -1;
        } else if (e1.getSalary() > e2.getSalary()) {
            return 1;
        } else {
            return 0;
        }
    }
}

主函数main

//如果嫌主函数功能过于复杂,可以重新定义一个类把功能集成到里面,主函数调用这些方法就行!


package com.situ.chapterwork;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);// 输入

        List employee = new ArrayList();// 存储员工

        while (true) {
            System.out.println("************************************************");
            System.out.println("* 1.添加 2.修改 3.查询 4.删除 5.排名 6.部门管理 7.退出 *");
            System.out.println("************************************************");
            System.out.println("请选择您要进行的操作:");

            String choice = scanner.next();// 获取字符串输出
            if (choice.equals("1")) {// 添加
                add(scanner, employee);
            } else if (choice.equals("2")) {// 修改
                edit(scanner, employee);
            } else if (choice.equals("3")) {// 查询
                find(scanner, employee);
            } else if (choice.equals("4")) {
                delete(scanner, employee);
            } else if (choice.equals("5")) {
                sort(employee);
            } else if (choice.equals("6")) {
                departmentmannger(employee);
            }
            else if (choice.equals("7")) {
                // 退出
                System.out.println("感谢使用本系统");
                break;
            }
        }
    }

    private static void departmentmannger(List employee) {
    	Scanner scanner = new Scanner(System.in);
		// TODO 自动生成的方法存根
		while(true) {
			System.out.println("1.部门查询 2.部门修改 3.返回上层");
			System.out.println("请输入操作:");
			int choice = scanner.nextInt();
			if(choice == 1) {
				findment(employee);
			}else if(choice == 2) {
				editment(employee);
			}else if(choice == 3) {
				break;
			}
		}
	}

	private static void editment(List employee) {
		// TODO 自动生成的方法存根
		while(true) {
			Scanner sc = new Scanner(System.in);
			System.out.println("1.修改部门号2.修改部门名 3.返回上层");
			System.out.println("请输入操作:");
			int flag = 0;
			int choice = sc.nextInt();
			
			
			if(choice == 1) {
			int count = employee.size();
			System.out.println("输入查找部门号:");
			int id = sc.nextInt();
			for(int i = 0;i<count;i++) {
				Employee e = (Employee)employee.get(i);
				if(e.getDid() == id) {
					System.out.println("输入部门号:");
					int eid = sc.nextInt();
					e.setDid(eid);
					printEmployeeInfo(e);
					flag++;
				}
			}
			if(flag == 0) {
				System.out.println("没有此部门号!");
			}else {
				System.out.println("修改成功!");
			}
			}else if(choice == 2) {
				int count = employee.size();
				System.out.println("输入查找部门名:");
				String name= sc.next();
				for(int i = 0;i<count;i++) {
					Employee e = (Employee)employee.get(i);
					if(e.getDName().equals(name)) {
						System.out.println("输入部门名:");
						String ename = sc.next();
						e.setDName(ename);
						printEmployeeInfo(e);
						flag++;
					}
				}
				if(flag == 0) {
					System.out.println("没有此部门号!");
				}else {
					System.out.println("修改成功!");
				}
			}else if(choice ==3) {
				break;
			}
		}
	}

	private static void findment(List employee) {
		// TODO 自动生成的方法存根
		while(true) {
		Scanner sc = new Scanner(System.in);
		System.out.println("1.按部门号查找 2.按部门名查找 3.返回上层");
		System.out.println("请输入操作:");
		
		int choice = sc.nextInt();
		int count = employee.size();
		
		
		if(choice == 1) {
			int flag = 0;
			System.out.println("输入部门号:");
			int id =sc.nextInt();
			for(int i=0;i<count;i++) {
				Employee e = (Employee)employee.get(i);
				if(e.getDid()==id) {
					printEmployeeInfo(e);
					flag++;
				}
			}
			if(flag == 0) {
				System.out.println("没有找到此部门!");
			}else {
				System.out.println(id+"部门共有"+flag+"人");
			}
		}else if(choice == 2) {
			int flag = 0;
			System.out.println("输入部门名:");
			String name =sc.next();
			for(int i=0;i<count;i++) {
				Employee e = (Employee)employee.get(i);
				if(e.getDName().equals(name)) {
					printEmployeeInfo(e);
					flag++;
				}
			}
			if(flag == 0) {
				System.out.println("没有找到此部门!");
			}else {
				System.out.println(name+"部门共有"+flag+"人");
			}
		}else if(choice == 3) {
			break;
		}
		}
	}

	/**
     * 添加员工
     */
    public static void add(Scanner scanner, List employee) {
        while (true) {
            System.out.println("====您现在执行的操作是添加员工====");
            System.out.println("请输入员工姓名:");
            String name = scanner.next();
            System.out.println("请输入员工性别:");
            String sex = scanner.next();
            System.out.println("请输入员工年龄:");
            int age = scanner.nextInt();
            System.out.println("请输入员工部门号:");
            int did = scanner.nextInt();
            System.out.println("请输入员工部门名:");
            String mname = scanner.next();
            System.out.println("请输入员工工资:");
            float salary = scanner.nextFloat();

            Employee e = new Employee();
            e.setName(name);
            e.setAge(age);
            e.setSex(sex);
            e.setSalary(salary);
            e.setDid(did);
            e.setDName(mname);
            // 存储到动态数组中
            employee.add(e);

            System.out.println("录入成功,是否继续?y/n");
            String choice = scanner.next();
            if (choice.equals("y")) {
                continue;
            } else {
                break;
            }
        }
    }

    /**
     * 查询员工
     */
    public static void find(Scanner scanner, List employee) {
        System.out.println("====您现在执行的操作是查询====");
        int count = employee.size();
        int max = count < 5 ? count : 5;// 条件运算符

        for (int i = 0; i < max; i++) {
            Employee e = (Employee)employee.get(i);
            printEmployeeInfo(e);
        }

        if (count > 5) {
            while (true) {
                System.out.println("当前总员工数:" + count);
                System.out.println("1.展示全部 2.根据姓名查询 3.根据年龄查询 4.返回上一层");
                String choice = scanner.next();

                if (choice.equals("1")) {
                    for (int i = 0; i < count; i++) {
                        Employee e = (Employee)employee.get(i);
                        printEmployeeInfo(e);
                    }
                } else if (choice.equals("2")) {
                    String name = scanner.next();// 获取用户输出的姓名
                    for (int i = 0; i < count; i++) {
                        Employee e = (Employee)employee.get(i);

                        if (e.getName().equals(name)) {
                            printEmployeeInfo(e);
                        }
                    }

                } else if (choice.equals("3")) {
                    int age = scanner.nextInt();// 获取用户输出的年龄
                    for (int i = 0; i < count; i++) {
                        Employee e = (Employee)employee.get(i);
                        if (e.getAge() == age) {
                            printEmployeeInfo(e);
                        }
                    }
                } else if (choice.equals("4")) {
                    break;
                }
            }
        }
    }

    /**
     * 删除员工
     * 
     * @param scanner
     * @param nameArray
     * @param sexArray
     * @param ageArray
     * @param salaryArray
     * @param count
     */
    public static void delete(Scanner scanner, List employee) {
        while (true) {
            System.out.println("====您现在执行的操作是删除====");
            System.out.println("请输入您要删除的员工姓名:");

            int count = employee.size();
            String name = scanner.next();// 获取用户输入的姓名

            int id = 1;// 序号
            List forDelete = new ArrayList();// 要删除的员工

            for (int i = 0; i < count; i++) {
                Employee e = (Employee)employee.get(i);
                if (e.getName().equals(name)) {
                    forDelete.add(e);
                    printEmployeeInfo(e);
                    id++;
                }
            }

            if (id == 1) {
                System.out.println("未找到您要删除的员工");
            } else {
                System.out.println("请输入您要删除的员工序号:");
                int choice = scanner.nextInt();// 用户输入的员工序号

                Employee deleteEmp = (Employee)forDelete.get(choice - 1);
                employee.remove(deleteEmp);

                System.out.println("删除成功,是否继续删除?y/n");
                String choose = scanner.next();
                if (choose.equals("y")) {
                    continue;
                } else {
                    break;
                }
            }
        }
    }

    /**
     * 工资排名
     * 
     * @param nameArray
     * @param sexArray
     * @param ageArray
     * @param salaryArray
     * @param count
     */
    public static void sort(List employee) {
        List newEmployee = new ArrayList();
        newEmployee.addAll(employee);

        // newEmployee.sort(new MyComparator());

        for (int i = 0; i < newEmployee.size() - 1; i++) {
            for (int j = i + 1; j < newEmployee.size(); j++) {
                Employee ej = (Employee)newEmployee.get(j);
                Employee ei = (Employee)newEmployee.get(i);
                if (ej.getSalary() > ei.getSalary()) {
                    Employee t = ei;
                    newEmployee.set(i, ej);
                    newEmployee.set(j, t);
                }
            }
        }

        for (int i = 0; i < employee.size(); i++) {
            Employee e = (Employee)employee.get(i);
            printEmployeeInfo(e);
        }
    }

    /**
     * 修改员工
     * 
     * @param scanner
     * @param nameArray
     * @param sexArray
     * @param ageArray
     * @param salaryArray
     * @param count
     */
    public static void edit(Scanner scanner, List employee) {
        while (true) {
            System.out.println("====您现在执行的操作是修改====");
            System.out.println("请输入您要修改的员工姓名:");

            String name = scanner.next();// 获取用户输入的姓名

            int id = 1;// 序号
            List forEdit = new ArrayList();// 要修改的员工索引

            for (int i = 0; i < employee.size(); i++) {
                Employee e = (Employee)employee.get(i);
                if (e.getName().equals(name)) {
                    forEdit.add(e);
                    System.out.println(id + ". 姓名:" + e.getName() + " 性别:" + e.getSex() + " 年龄:" + e.getAge() + " 部门:"+e.getDName()+" 工资:"
                        + e.getSalary());
                    id++;
                }
            }

            if (id == 1) {
                System.out.println("未找到您要修改的员工");
            } else {
                System.out.println("请输入您要修改的员工序号:");
                int choice = scanner.nextInt();// 用户输入的员工序号
                Employee editEmp = (Employee)forEdit.get(choice - 1);// 要修改员工索引

                System.out.println("请输入员工姓名:");
                String newName = scanner.next();
                System.out.println("请输入员工性别:");
                String newSex = scanner.next();
                System.out.println("请输入员工年龄:");
                int newAge = scanner.nextInt();
                System.out.println("请输入员工部门号:");
                int newDid = scanner.nextInt();
                System.out.println("请输入员工部门:");
                String newMent = scanner.next();
                System.out.println("请输入员工工资:");
                float newSalary = scanner.nextFloat();

                // 存储到数组中
                editEmp.setName(newName);
                editEmp.setSex(newSex);
                editEmp.setAge(newAge);
                editEmp.setDid(newDid);
                editEmp.setDName(newMent);
                editEmp.setSalary(newSalary);

                System.out.println("修改成功,是否继续?y/n");
                String choose = scanner.next();
                if (choose.equals("y")) {
                    continue;
                } else {
                    break;
                }

            }
        }
    }

    public static void printEmployeeInfo(Employee e) {
        System.out.println("姓名:" + e.getName() + " 性别:" + e.getSex() + " 年龄:" + e.getAge() +" 部门号:"+e.getDid()+" 部门:"+e.getDName()+ " 工资:" + e.getSalary());
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A little sea pig

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值