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;
}
}
package com.imooc.homework;
import java.util.Arrays;
import java.util.List;
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() {
System.out.println(this.getDepartmentNum()+" "+this.getDepartmentName()+" "+this.getStaffNum()+" ");
//输出员工名称
List<Staff> staffList=Arrays.asList(this.getMyStaff());
for(Staff sta:staffList) {
if(sta!=null) {
System.out.print(sta.getName()+" ");
}
}
System.out.println();
return " ";
}
//设置员工数组方法
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;
}
}
}
}
package com.imooc.homework.test;
import com.imooc.homework.Post;
import com.imooc.homework.Department;
import com.imooc.homework.Staff;
import java.util.Arrays;
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","人事部");
Department dep2=new Department("D002","市场部");
//员工介绍信息
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()+"名员工");
System.out.println("==========================");
System.out.println(dep1.departmentInfo());
dep1.departmentInfo2();
dep2.departmentInfo2();
}
}
最后运行
部门编号 部门名称 员工人数 员工数组
D001 人事部 3
张铭 李艾爱 孙超
D002 市场部 3
张美美 蓝迪 米莉
是这样的。哪里出问题了阿?老师,
问题2
//输出员工名称
List<Staff> staffList=Arrays.asList(this.getMyStaff());
for(Staff sta:staffList) {
if(sta!=null) {
System.out.print(sta.getName()+" ");
}
}
System.out.println();
return " ";
这段代码什么意思阿, 是不是没有讲过啊,前面的课程,最后名字按顺序执行的,是为什么?
正在回答 回答被采纳积分+1
同学你好,
1、同学的运行结果中,部门中的员工数组占了一行。
如果同学想要在一行输出部门编号,员工人数等信息,可以使用print()方法,输出完信息后不换行。
参考代码如下:
2、Arrays.asList()是将数组转换为List集合。for(Staff sta:staffList) {}是遍历staffList集合,System.out.print(sta.getName()+" ");是输出遍历到的Staff对象的name值,并输出空格。return之前的System.out.println();是换行。
在后面课程中会学习到集合,建议同学可以获得部门中员工数组,然后使用for循环遍历员工数组,依次输出员工的信息,例如输出员工的名字。
参考代码如下:
public String departmentInfo() { String str = "部门信息如下:"+"\n部门编号" + " " + "部门名称" + " " + "员工人数" + " " + "员工数组"; return str; } public String departmentInfo2() { System.out.print(this.getDepartmentNum()+" "+this.getDepartmentName()+" "+this.getStaffNum()+" "); //输出员工名称 Staff[] staffs = this.getMyStaff(); for(int i=0; i<staffs.length; i++) { Staff sta = staffs[i]; if(sta != null) { System.out.print(sta.getName()+" "); } } System.out.println(); //换行 return " "; }
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星