企业人事管理系统
完成一个企业人事管理系统,该系统中包含两个实体类:员工(Emp),部门(Dept),两个类中分别包含以下属性:
员工(工号,姓名,性别,职位,年龄,月薪,部门)
部门(编号,部门名,部门介绍,分机号,)
要求实现以下功能:
完成部门的创建,添加3个部门
向各个部门中添加若干员工
查询所有的员工信息和所在的部门信息
根据员工工号显示员工信息和所在的部门信息
修改指定工号员工的薪资
根据部门号查询出门中的所有员工
部门类:
根据需求,先创立一个部门类,在部门类中加入一个带员工类的链表,将每次向此部门添加的员工都放入本链表中:
package com;import java.util.ArrayList;public class Section {private Integer deptid;private String deptname;private String deptsynopsis;private Integer deptnumber;private ArrayList<EmployeeData> emps;public Section(Integer deptid, String deptname, String deptsynopsis, Integer deptnumber) {super();this.emps = new ArrayList<EmployeeData>(); //创建一个员工类的链表this.deptid = deptid;this.deptname = deptname;this.deptsynopsis = deptsynopsis;this.deptnumber = deptnumber;}protected Integer getDeptid() {return deptid;}protected void setDeptid(Integer deptid) {this.deptid = deptid;}protected String getDeptname() {return deptname;}protected void setDeptname(String deptname) {this.deptname = deptname;}protected String getDeptsynopsis() {return deptsynopsis;}protected void setDeptsynopsis(String deptsynopsis) {this.deptsynopsis = deptsynopsis;}protected Integer getDeptnumber() {return deptnumber;}protected void setDeptnumber(Integer deptnumber) {this.deptnumber = deptnumber;}protected ArrayList<EmployeeData> getEmps() {return emps;}protected void setEmps(ArrayList<EmployeeData> emps) {this.emps = emps;}@Overridepublic String toString() {return "部门Id为 " + deptid + ",部门名 " + deptname + ",部门介绍 " + deptsynopsis + ",部门分机号 " + deptnumber + "]";}public Section() {super();this.emps = new ArrayList<EmployeeData>();}}
员工类:
创建一个员工类,用于保存员工信息,其中输入部门,以此将其加入相应的部门中
package com;public class EmployeeData {private Integer id;private String name;private String sex;private String position;private Integer age;private Double salary;private String section;public EmployeeData(Integer id, String name, String sex, String position, Integer age, Double salary,String section) {super();this.id = id;this.name = name;this.sex = sex;this.position = position;this.age = age;this.salary = salary;this.section = section;}protected Integer getId() {return id;}protected void setId(Integer id) {this.id = id;}protected String getName() {return name;}protected void setName(String name) {this.name = name;}protected String getSex() {return sex;}protected void setSex(String sex) {this.sex = sex;}protected String getPosition() {return position;}protected void setPosition(String position) {this.position = position;}protected Integer getAge() {return age;}protected void setAge(Integer age) {this.age = age;}protected Double getSalary() {return salary;}protected void setSalary(Double salary) {this.salary = salary;}protected String getSection() {return section;}protected void setSection(String section) {this.section = section;}public EmployeeData() {super();}@Overridepublic String toString() {return "员工id为 " + id + ", 姓名:" + name + ",性别:" + sex + ",职位:" + position + ",年龄:" + age + ",月薪:" + salary+ ",部门:" + section + "]";}}
功能类:
再建立一个功能类,包含菜单和所有的功能
定义基本属性
public class EnterprisePersonnelManagementSystem {private Scanner scanner;private ArrayList<Section> secs; //设置全局的部门动态列表Integer empId; //定义全局的默认部门IdInteger secId; //定义全局的默认员工Idpublic EnterprisePersonnelManagementSystem() {scanner = new Scanner(System.in);secs = new ArrayList<Section>(); //创建一个部门链表empId = 10086; //设置部门的默认IdsecId = 10010; //设置员工的默认Idmenu();}
}
菜单类
public void menu() { //主菜单,通过菜单进入各个方法System.out.println("企业人事管理系统");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("6.根据部门号查询部门中的所有员工");System.out.println("----------------------------------");System.out.print(">");int select = Integer.parseInt(scanner.nextLine());switch (select) {case 1:addDept();menu();break;case 2:addEmp();menu();case 3:findAllEmp();menu();case 4:findEmp();menu();case 5:setEmpsalary();menu();case 6:getSectionEmp();menu();default:System.out.println("运行结束");break;}}
添加部门
private void addDept() { //添加部门Section s = new Section();s.setDeptid(secId++); //自增的部门Id,保证部门Id唯一性System.out.print("请输入部门名:");s.setDeptname(scanner.nextLine());System.out.print("请输入部门介绍:");s.setDeptsynopsis(scanner.nextLine());System.out.print("请输入部门分机号:");s.setDeptnumber(Integer.parseInt(scanner.nextLine()));secs.add(s);System.out.println(s.toString());}
添加员工
private void addEmp() { //添加员工EmployeeData e = new EmployeeData();e.setId(empId++); //自增的员工Id,保证员工Id唯一性System.out.print("请输入姓名:");e.setName(scanner.nextLine());System.out.print("请输入性别:");e.setSex(scanner.nextLine());System.out.print("请输入职位:");e.setPosition(scanner.nextLine());System.out.print("请输入年龄:");e.setAge(Integer.parseInt(scanner.nextLine()));System.out.print("请输入月薪:");e.setSalary(Double.parseDouble(scanner.nextLine()));System.out.print("请输入部门名:");e.setSection(scanner.nextLine());boolean sign = false;for (int i = 0; i < secs.size(); i++) {if (secs.get(i).getDeptname().equals(e.getSection())) { //通过对比部门名,查找是否有此部门sign=true;secs.get(i).getEmps().add(e);System.out.println(e.toString());menu();}}if (sign == false) { //没有找到部门,返回错误System.out.println("没有找到此部门,请重新输入"); }}
寻找所有员工
private void findAllEmp() { // 寻找所有员工for (int i = 0; i < secs.size(); i++) { //遍历链表,找到输出所有员工for (int j = 0; j < secs.get(i).getEmps().size(); j++) {System.out.println("以下为全部员工");System.out.println(secs.get(i).getEmps().get(j));}}}
通过Id寻找员工
private void findEmp() { //通过Id寻找员工System.out.println("请输入您需要操作的员工的Id");Integer findid = Integer.parseInt(scanner.nextLine());boolean sign = false;for (int i = 0; i < secs.size(); i++) {for (int j = 0; j < secs.get(i).getEmps().size(); j++) { if (secs.get(i).getEmps().get(j).getId() == findid) { //遍历所有的员工Id,找到符合的Id,然后输出此员工System.out.println(secs.get(i).getEmps().get(j));sign = true;menu();}}}if (sign == false) {System.out.println("没有找到此员工,请重新输入");}}
通过Id修改员工的薪资
private void setEmpsalary() { //通过Id修改员工的薪资System.out.println("请输入您需要操作的员工的Id");Integer findid = Integer.parseInt(scanner.nextLine());boolean sign = false;for (int i = 0; i < secs.size(); i++) {for (int j = 0; j < secs.get(i).getEmps().size(); j++) {if (secs.get(i).getEmps().get(j).getId() == findid) { //方法同上System.out.println("该员工的当前薪资为:" + secs.get(i).getEmps().get(j).getSalary()); System.out.println("请输入修改后的薪资:");secs.get(i).getEmps().get(j).setSalary(Double.parseDouble(scanner.nextLine()));System.out.println(secs.get(i).getEmps().get(j));sign = true;menu();}}}if (sign == false) {System.out.println("没有找到此员工,请重新输入");}}
通过部门号查找员工
private void getSectionEmp() { //通过部门号查询部门中的所有员工System.out.println("请输入您需要查询的部门号");Integer getid = Integer.parseInt(scanner.nextLine());Boolean sign = false;for (int i = 0; i < secs.size(); i++) {if (secs.get(i).getDeptid() == getid) { //遍历部门,找到符合的部门sign = true;System.out.println(secs.get(i).getDeptname() + "有以下员工");for (int j = 0; j < secs.get(i).getEmps().size(); j++) { //输出部门中的所有员工System.out.println(secs.get(i).getEmps().get(j));}break;}}if (sign == false) {System.out.println("未找到该部门,请重新输入");}}
测试类
public class Test {public static void main(String[] args) {EnterprisePersonnelManagementSystem epls=new EnterprisePersonnelManagementSystem();}}