麻烦老师查看是否有优化的项
部门类
package com.self.model;
public class Dept {
// 部门编号
private String deptNo;
// 部门名称
private String deptName;
// 部门员工
private People[] peopleArray;
// 部门员工个数
private int peopleNum;
public String getDeptNo() {
return this.deptNo;
};
public void setDeptNo(String deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
};
/**
* 获取人员列表
*
* @return 当没有进行初始化的时候,默认初始 200
*/
public People[] getPeopleArray() {
if (this.peopleArray == null)
this.peopleArray = new People[200];
return this.peopleArray;
}
public void setPeopleArray(People[] peopleArray) {
this.peopleArray = peopleArray;
}
public int getPeopleNum() {
return peopleNum;
}
public void setPeopleNum(int peopleNum) {
this.peopleNum = peopleNum;
}
// 无参构造函数
public Dept() {
};
// 带参构造函数
public Dept(String deptNo, String deptName) {
this.setDeptNo(deptNo);
this.setDeptName(deptName);
}
// 人员和部门关联信息
public void addUserDept(People peo) {
// 循环列表
for (int i = 0; i < this.getPeopleArray().length; i++) {
// 判断是否有空位
if (this.getPeopleArray()[i] == null) {
// 关联部门和人员的信息
peo.setDeptObj(this);
// 赋值人员信息
this.getPeopleArray()[i] = peo;
// 累加人员的有效数
this.setPeopleNum(i + 1);
// 退出循环
return;
}
;
}
}
// 打印部门的列表信息
public String deptInfo() {
// 初始化员工信息
String userNameInfo = "";
// 信息返回
String str = "";
// 循环列表
for (int i = 0; i < this.getPeopleNum(); i++) {
// 累加员工名字
if (i < this.getPeopleNum() - 1) {
userNameInfo += this.getPeopleArray()[i].getName() + ",";
} else {
userNameInfo += this.getPeopleArray()[i].getName();
}
};
System.out.println("=================================");
// 显示部门信息
str = "部门编号:" + this.getDeptNo() + "\n部门名称:" + this.getDeptName() + "\n员工数组:" + userNameInfo + "\n员工人数:"
+ this.getPeopleNum();
// 返回部门信息
return str;
}
}职务类
package com.self.model;
public class Duties {
// 职务编号
private String dutiesNo;
// 职务名称
private String dutiesName;
// 关联职务
private People[] dutiesArray;
// 关联职务人员
private int dutiesNum;
public String getDutiesNo() {
return this.dutiesNo;
};
public void setDutiesNo(String dutiesNo) {
this.dutiesNo = dutiesNo;
}
public String getDutiesName() {
return dutiesName;
}
public void setDutiesName(String dutiesName) {
this.dutiesName = dutiesName;
};
public People[] getDutiesArray() {
if (this.dutiesArray == null)
this.dutiesArray = new People[200];
return this.dutiesArray;
}
public void setDutiesArray(People[] dutiesArray) {
this.dutiesArray = dutiesArray;
}
public int getDutiesNum() {
return dutiesNum;
}
public void setDutiesNum(int dutiesNum) {
this.dutiesNum = dutiesNum;
}
// 无参构造函数
public Duties() {
};
// 带参构造函数
public Duties(String dutiesNo, String dutiesName) {
this.setDutiesNo(dutiesNo);
this.setDutiesName(dutiesName);
}
// 人员和部门关联信息
public void addUserDuties(People peo) {
// 循环列表
for (int i = 0; i < this.getDutiesArray().length; i++) {
// 判断是否有空位
if (this.getDutiesArray()[i] == null) {
// 关联部门和人员的信息
peo.setDutiesObj(this);
// 赋值人员信息
this.getDutiesArray()[i] = peo;
// 累加人员的有效数
this.setDutiesNum(i + 1);
// 退出
return;
}
;
}
}
// 打印职务的列表信息
public String dutiesInfo() {
// 初始化员工信息
String userNameInfo = "";
// 信息返回
String str = "";
// 循环列表
for (int i = 0; i < this.getDutiesNum(); i++) {
// 累加员工名字
if (i < this.getDutiesNum() - 1) {
userNameInfo += this.getDutiesArray()[i].getName() + ",";
} else {
userNameInfo += this.getDutiesArray()[i].getName();
}
}
;
System.out.println("=================================");
// 显示部门信息
str = "职务编号:" + this.getDutiesNo() + "\n职务名称:" + this.getDutiesName() + "\n员工数组:" + userNameInfo + "\n员工人数:"
+ this.getDutiesNum();
// 返回部门信息
return str;
}
}员工类
package com.self.model;
public class People {
// 员工姓名
private String name;
// 员工工号
private String peopleNo;
// 员工年龄
private int age;
// 性别
private String sex;
// 部门
private Dept deptObj;
// 职务
private Duties dutiesObj;
public String getName() {
return this.name;
};
public void setName(String name) {
this.name = name;
};
public String getPeopleNo() {
return peopleNo;
};
public void setPeopleNo(String peopleNo) {
this.peopleNo = peopleNo;
};
public String getSex() {
return this.sex;
};
/**
* 设置性别
*
* 性别只能是"男"或"女", 反之则设置默认为"男"
*
* @param age
*/
public void setSex(String sex) {
if (!(sex.equals("男") || sex.equals("女"))) {
this.sex = "男";
} else {
this.sex = sex;
}
};
public int getAge() {
return age;
};
/**
* 设置年龄
*
* 限定年龄只能是18-65之间,反之则默认为18岁
*
* @param age
*/
public void setAge(int age) {
if (age < 18 || age > 65) {
this.age = 18;
} else {
this.age = age;
}
};
/**
* 当没有进行部门赋值的时候,默认赋值部门信息
*
* @return 返回部门对象
*/
public Dept getDeptObj() {
if (this.deptObj == null)
this.deptObj = new Dept();
return this.deptObj;
};
public void setDeptObj(Dept deptObj) {
this.deptObj = deptObj;
};
/**
* 当没有进行职务赋值的时候,默认职务职务信息
*
* @return 返回部门对象
*/
public Duties getDutiesObj() {
if (this.dutiesObj == null)
this.dutiesObj = new Duties();
return dutiesObj;
}
public void setDutiesObj(Duties dutiesObj) {
this.dutiesObj = dutiesObj;
}
// 无参构造函数
public People() {
}
/**
* 赋值个人信息
*
* @param name
* @param peopleNo
* @param age
* @param sex
* @param deptObj
* @param dutiesObj
*/
public People(String name, String peopleNo, int age, String sex) {
this.setName(name);
this.setPeopleNo(peopleNo);
this.setAge(age);
this.setSex(sex);
};
/**
* 赋值个人信息
*
* @param name
* @param peopleNo
* @param age
* @param sex
* @param deptObj
* @param dutiesObj
*/
public People(String name, String peopleNo, int age, String sex, Dept deptObj, Duties dutiesObj) {
this.setName(name);
this.setPeopleNo(peopleNo);
this.setAge(age);
this.setSex(sex);
this.setDeptObj(deptObj);
this.setDutiesObj(dutiesObj);
};
public String peopleInfo() {
System.out.println("=================================");
String str = "姓名:" + this.getName() + "\n工号:" + this.getPeopleNo() + "\n性别:" + this.getSex() + "\n年龄:"
+ this.getAge() + "\n部门:" + this.getDeptObj().getDeptName() + "\n职务:"
+ this.getDutiesObj().getDutiesName();
return str;
};
}主方法测试类
package com.self.Test;
import com.self.model.Dept;
import com.self.model.Duties;
import com.self.model.People;
public class Test {
public static void main(String[] args) {
// 初始化部门
Dept dept1 = new Dept("D001", "人事部");
Dept dept2 = new Dept("D002", "市场部");
// 初始化职务
Duties dut1 = new Duties("P001", "经理");
Duties dut2 = new Duties("P002", "助理");
Duties dut3 = new Duties("P003", "职员");
// 创建对象
People peo1 = new People("张铭", "S001", 29, "男");
// 关联部门
dept1.addUserDept(peo1);
// 关联职务
dut1.addUserDuties(peo1);
// 调用人员信息
System.out.println(peo1.peopleInfo());
// 创建对象
People peo2 = new People("李艾爱", "S002", 21, "女");
// 关联部门
dept1.addUserDept(peo2);
// 关联职务
dut2.addUserDuties(peo2);
// 调用人员信息
System.out.println(peo2.peopleInfo());
// 创建对象
People peo3 = new People("孙超", "S003", 29, "男");
// 关联部门
dept1.addUserDept(peo3);
// 关联职务
dut3.addUserDuties(peo3);
// 调用人员信息
System.out.println(peo3.peopleInfo());
// 创建对象
People peo4 = new People("张美美", "S004", 26, "女");
// 关联部门
dept2.addUserDept(peo4);
// 关联职务
dut3.addUserDuties(peo4);
// 调用人员信息
System.out.println(peo4.peopleInfo());
// 创建对象
People peo5 = new People("蓝迪", "S005", 37, "男");
// 关联部门
dept2.addUserDept(peo5);
// 关联职务
dut1.addUserDuties(peo5);
// 调用人员信息
System.out.println(peo5.peopleInfo());
// 创建对象
People peo6 = new People("米莉", "S006", 24, "女");
// 关联部门
dept2.addUserDept(peo6);
// 关联职务
dut3.addUserDuties(peo6);
// 调用人员信息
System.out.println(peo6.peopleInfo());
// 打印部门信息
System.out.println(dept1.deptInfo());
System.out.println(dept2.deptInfo());
// 打印职务信息
System.out.println(dut1.dutiesInfo());
System.out.println(dut2.dutiesInfo());
System.out.println(dut3.dutiesInfo());
}
}17
收起
正在回答 回答被采纳积分+1
1回答
ImoocZhang
2025-09-09 09:43:46
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星