人事部只有3名员工,统计结果为6名
package cn.imooc; //测试类 public class Test { public static void main(String[] args) { /* * 实例化职务 */ Position position=new Position(001, "经理"); Position position2=new Position(002, "助理"); Position position3=new Position(003, "职员"); /* * 实例化部门 */ Department department=new Department(002, "人事部"); Department department2=new Department(002, "市场部"); /* * 实例化员工类 */ Employee employee=new Employee("张铭", "S001", 29, "男", department, position); Employee employee2=new Employee("李艾爱", "S002", 21, "女", department, position2); Employee employee3=new Employee("孙超", "S003", 29, "男", department, position3); Employee employee4=new Employee("张美美", "S004", 26, "女", department2, position3); Employee employee5=new Employee("蓝迪", "S006", 37, "男", department2, position); Employee employee6=new Employee("米莉", "S007", 24, "男", department2, position3); /* * 输出员工基本信息 */ System.out.println(employee.show()+"\n"+"============="); System.out.println(employee2.show()+"\n"+"============="); System.out.println(employee3.show()+"\n"+"============="); System.out.println(employee4.show()+"\n"+"============="); System.out.println(employee5.show()+"\n"+"============="); System.out.println(employee6.show()+"\n"+"============="); /* * 调用部门人员统计方法 */ department.addEmployee(employee); department.addEmployee(employee2); department.addEmployee(employee3); department.addEmployee(employee4); department.addEmployee(employee5); department.addEmployee(employee6); department2.addEmployee(employee6); /* * 输出部门统计结果 */ System.out.println(department.getEmployeeNum()); System.out.println(department2.getEmployeeNum()); } }
package cn.imooc;
//部门类
public class Department {
private int departmentId;//部门ID
private String departmentName;//部门名称
private Employee[] employees;//部门员工信息存储数组
private int employeeNum;//部门员工信息统计
public Department() {
}
public Department(int departmentId, String departmentName) {
super();
setDepartmentId(departmentId);
setDepartmentName(departmentName);
}
public Department(int departmentId, String departmentName, Employee[] employees, int employeeNum) {
super();
setDepartmentId(departmentId);
setDepartmentName(departmentName);
setEmployees(employees);
setEmployeeNum(employeeNum);
}
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public Employee[] getEmployees() {
if (this.employees==null) {
employees=new Employee[200];
}
return employees;
}
public void setEmployees(Employee[] employees) {
this.employees = employees;
}
public int getEmployeeNum() {
return employeeNum;
}
public void setEmployeeNum(int employeeNum) {
this.employeeNum = employeeNum;
}
/**
* 部门员工统计方法
* @param employee
*/
public void addEmployee(Employee employee) {
for (int i = 0; i < getEmployees().length; i++) {
if(this.getEmployees()[i]==null) {
this.getEmployees()[i]=employee;
this.employeeNum=i+1;
return;
}
}
}
}
package cn.imooc;
//职务类
public class Position {
private int positionId;//职务ID
private String positionName;//职务名称
public Position() {
}
public Position(int positionId, String positionName) {
super();
setPositionId(positionId);
setPositionName(positionName);
}
public int getPositionId() {
return positionId;
}
public void setPositionId(int positionId) {
this.positionId = positionId;
}
public String getPositionName() {
return positionName;
}
public void setPositionName(String positionName) {
this.positionName = positionName;
}
}
package cn.imooc;
//员工类
public class Employee {
private String name;//姓名
private String id;//工号
private int age;//年龄
private String gender;//性别
private Department department;//部门
private Position position;//职务
public Employee() {
}
public Employee(String name, String id, int age, String gender, Department department, Position position) {
super();
setName(name);
setAge(age);
setId(id);
setGender(gender);
setDepartment(department);
setPosition(position);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
/**
* 员工信息展示方法
* @return返回员工基本信息
*/
public String show() {
return "姓名:"+getName()+"\r"+"工号:"+getId()+"\r"+"性别:"+getGender()+"\r"+"年龄:"+getAge()+
"\r"+"职务:"+department.getDepartmentName()+position.getPositionName();
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星