关于3-11的作业问题
我先把我的码附上
public class Post {
// 成员属性,分别:职务编号、职务名称
private String postNum;
private String postName;
// 无参构造
public Post() {
}
// 有参构造,对所以属性进行赋值
public Post(String postNum, String postName) {
this.setPostNum(postNum);
this.setPostName(postName);
}
public void setPostNum(String postNum) {
this.postNum = postNum;
}
public String getPostNum() {
return postNum;
}
public void setPostName(String postName) {
this.postName = postName;
}
public String getPostName() {
return postName;
}
// 创建方法
public String infoPost() {
String str = "职务信息如下:"+"\n职务编号" + " " + "职务名称" + "\n" + this.getPostNum() + " "+ this.getPostName();
return str;
}
public String infoPost2() {
String str=this.getPostNum()+" "+this.getPostName();
return str;
}
}
public class Staff {
// 定义属性,分别:员工姓名、工号、年龄、性别、所属部门、职务信息
private String name;
private String jodNum;
private int age;
private String sex;
private Department departmentInfor;
private Post postInfor;
// 无参构造
public Staff() {
}
//有参构造,对姓名,工号,年龄,性别进行赋值
// public Staff(String name, String jodNum, int age, String sex) {
// this.setName(name);
// this.setJodNum(jodNum);
// this.setAge(age);
// this.setSex(sex);
// }
// 有参构造,对属性进行赋值
public Staff(String name, String jodNum,String sex,int age,Department departmentInfor,Post postInfor) {
this.setName(name);
this.setJodNum(jodNum);
this.setSex(sex);
this.setAge(age);
this.setDepartmentInfor(departmentInfor);
this.setPostInfor(postInfor);
}
// 设置set/get方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setJodNum(String jodNum) {
this.jodNum = jodNum;
}
public String getJodNum() {
return jodNum;
}
public void setAge(int age) {
if (age < 18 || age > 65) {
this.age = 18;
} else
this.age = age;
}
public int getAge() {
return age;
}
public void setSex(String sex) {
if (sex.equals("男") || sex.equals("女")) {
this.sex = sex;
} else
this.sex = "男";
}
public String getSex() {
return sex;
}
public void setDepartmentInfor(Department departmentInfor) {
this.departmentInfor = departmentInfor;
}
/**
* 获取部门信息对象,如果没有实例化,先实例化,后返回
*
* @return 部门信息
*/
public Department getDepartmentInfor() {
if (departmentInfor == null) {
this.departmentInfor = new Department();
}
return departmentInfor;
}
public void setPostInfor(Post postInfor) {
this.postInfor = postInfor;
}
/**
* 获取部门职务信息,如果没有实例化则先实例化,后返回
*
* @return 职务信息
*/
public Post getPostInfor() {
if (postInfor == null) {
this.postInfor = new Post();
}
return postInfor;
}
/**
* 设置方法,实现员工自我介绍信息,将员工信息字符串返回
* @return 员工信息
*/
public String staffInfo() {
String str = "姓名:" + this.getName() + "\n工号:" + this.getJodNum() + "\n性别:" + this.getSex() + "\n年龄:"
+ this.getAge() + "\n部门名称:" + this.getDepartmentInfor().getDepartmentName() + "\n部门职务:"
+ this.getPostInfor().getPostName();
return str;
}
}
public class Department {
// 定义属性,分别部门编号、部门名称、员工数组、统计部门的员工个数
private String departmentNum;
private String departmentName;
private Staff[] myStaff;
private int staffNum;
// 无参构造
public Department() {
}
//有参构成,对部门编号,部门名称进行赋值
public Department(String departmentNum, String departmentName) {
this.setDepartmentNum(departmentNum);
this.setDepartmentName(departmentName);
}
// 有参构造,对所以属性进行赋值
public Department(String departmentNum, String departmentName,Staff[] myStaff,int staffNum) {
this.setDepartmentNum(departmentNum);
this.setDepartmentName(departmentName);
this.setMyStaff(myStaff);
this.setStaffNum(staffNum);
}
// 设置set/get方法
public void setDepartmentNum(String departmentNum) {
this.departmentNum = departmentNum;
}
public String getDepartmentNum() {
return departmentNum;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getDepartmentName() {
return departmentName;
}
public void setMyStaff(Staff[] myStaff) {
this.myStaff=myStaff;
}
/**
* 获取员工信息,如果保存员工的数据未被初始化,则先初始化6
* @return 返回员工信息数组
*/
public Staff[] getMyStaff() {
if(myStaff==null) {
this.myStaff=new Staff[6];
}
return myStaff;
}
public void setStaffNum(int staffNum) {
this.staffNum=staffNum;
}
public int getStaffNum() {
return staffNum;
}
// 设置部门方法
public String departmentInfo() {
String str = "部门信息如下:"+"\n部门编号" + " " + "部门名称" + " " + "员工数组" + " " + "员工人数";
return str;
}
public String departmentInfo2() {
String str=this.getDepartmentNum()+" "+this.getDepartmentName();
return str;
}
//设置员工数组方法
public void addStaff(Staff sta) {
for(int i=0;i<this.getMyStaff().length;i++) {
if(this.getMyStaff()[i]==null) {
this.getMyStaff()[i]=sta;
this.staffNum=i+1;
return;
}
}
}
}
public class firmTest {
public static void main(String[] args) {
//职务信息
Post postDemo1=new Post("P001","经理");
Post postDemo2=new Post("P002","助理");
Post postDemo3=new Post("P003","职员");
System.out.println(postDemo1.infoPost());
System.out.println(postDemo2.infoPost2());
System.out.println(postDemo3.infoPost2());
System.out.println("==========================");
//部门信息
Department dep1=new Department("D001","人事部");
System.out.println(dep1.departmentInfo());
System.out.println(dep1.departmentInfo2());
Department dep2=new Department("D002","市场部");
System.out.println(dep2.departmentInfo2());
System.out.println("==========================");
//员工介绍信息
System.out.println("员工信息如下:");
Staff st1=new Staff("张铭","S001","男",29,dep1,postDemo1);
System.out.println(st1.staffInfo());
System.out.println("==========================");
Staff st2=new Staff("李艾爱","S002","女",21,dep1,postDemo2);
System.out.println(st2.staffInfo());
System.out.println("==========================");
Staff st3=new Staff("孙超","S003","男",29,dep1,postDemo3);
System.out.println(st3.staffInfo());
System.out.println("==========================");
Staff st4=new Staff("张美美","S004","女",26,dep2,postDemo3);
System.out.println(st4.staffInfo());
System.out.println("==========================");
Staff st5=new Staff("蓝迪","S005","男",37,dep2,postDemo1);
System.out.println(st5.staffInfo());
System.out.println("==========================");
Staff st6=new Staff("米莉","S006","女",24,dep2,postDemo3);
System.out.println(st6.staffInfo());
//测试员工各部门人数
System.out.println("==========================");
dep1.addStaff(st1);
dep1.addStaff(st2);
dep1.addStaff(st3);
dep2.addStaff(st4);
dep2.addStaff(st5);
dep2.addStaff(st6);
System.out.println(dep1.getDepartmentName()+"总共有"+dep1.getStaffNum()+"名员工");
System.out.println(dep2.getDepartmentName()+"总共有"+dep2.getStaffNum()+"名员工");
}
}
部门信息如下:
部门编号 部门名称 员工数组 员工人数
D001 人事部
D002 市场部
==========================
我这个写不出来,该怎么写啊。。
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星