关于3-11的作业问题

关于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

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿慧 2020-12-07 14:47:32

同学你好,同学代码完成的不错,测试类中可以按如下思路完成:

1、创建2个部门类对象,分别为市场部和人事部。创建3个职务类对象,分别为经理,助理,职员。

例如:

http://img1.sycdn.imooc.com//climg/5fcdcf3209cd20e706750202.jpg

2、创建6个员工对象。调用员工的介绍方法,展示一下员工信息。

例如:

http://img1.sycdn.imooc.com//climg/5fcdcf630959c5e306830156.jpg

3、各个部门对象通过调用添加员工的方法,把对应的员工对象传递进去。

例如:

http://img1.sycdn.imooc.com//climg/5fcdcfa8094d4bb304260152.jpg

4、打印输出dep1和dep2的staffNum,展示一下员工数。

例如:

http://img1.sycdn.imooc.com//climg/5fcdcfd609531be406800093.jpg

  • 提问者 rock221 #1
    老师没有回答我的问题,我那个写不出来,应该怎么写啊
    2020-12-07 14:51:10
  • 好帮手慕阿慧 回复 提问者 rock221 #2

    同学你好,同学可以往部门中添加员工后,再输出部门信息。

    参考代码如下:

    http://img1.sycdn.imooc.com//climg/5fcde3010965b9cd10060466.jpg

    测试代码如下:

    http://img1.sycdn.imooc.com//climg/5fcde36509d9f0f108060513.jpg

    祝学习愉快~

    2020-12-07 16:10:38
  • 提问者 rock221 回复 好帮手慕阿慧 #3
    什么意思啊,我想了好久不知道该怎么弄,这个数组怎么出来啊,还有后面的人数,在实例化中,括号里怎么写啊…
    2020-12-07 16:37:51
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师