3-8作业 关于统计部门人数的问题
# 具体遇到的问题
# 报错信息的截图
# 相关课程内容截图
# 尝试过的解决思路和结果
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
员工类;
package com.imooc.office.system;
public class staff {
//成员属性 姓名 工号 年龄 性别 部门 职务
private String staffName;
private String staffNo;
private int staffAge;
private String staffSex;
private department staffDepartment;
private post staffPost;
//构造无参方法
public staff() {
}
//构造带参方法 实现成员属性的赋值
public staff(String staffName,String staffNo,int staffAge,String staffSex,department staffDepartment, post staffPost) {
this.staffName = staffName;
this.staffNo = staffNo;
this.staffAge = staffAge;
this.staffSex = staffSex;
this.staffDepartment = staffDepartment;
this.staffPost = staffPost;
}
public String getStaffName() {
return staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName;
}
public String getStaffNo() {
return staffNo;
}
public void setStaffNo(String staffNo) {
this.staffNo = staffNo;
}
public int getStaffAge() {
return staffAge;
}
//限制年龄只能在18-65之间 否则强制为18
public void setStaffAge(int staffAge) {
if(staffAge<18||staffAge>65)
this.staffAge=18;
else
this.staffAge = staffAge;
}
public String getStaffSex() {
return staffSex;
}
//限制性能只能是男或女 反之强制为女
public void setStaffSex(String staffSex) {
String str=staffSex;
if(str.equals("男"))
this.staffSex = staffSex;
else
this.staffSex = staffSex;
}
public department getStaffDaparment() {
if(this.staffDepartment==null)
this.staffDepartment= new department ();
return staffDepartment;
}
public void setStaffDaparment(department staffDepartment) {
this.staffDepartment = staffDepartment;
}
public post getStaffPost() {
if(this.staffPost==null)
this.staffPost=new post();
return staffPost;
}
public void setStaffPost(post staffPost) {
this.staffPost = staffPost;
}
/**
* 员工 姓名 工号 年龄 性别 部门 职务
* @return 返回员工的个人信息
*/
public String introduction(department dm) {
String str = "\n姓名:" + this.getStaffName() + "\n工号:" + this.getStaffNo() + "\n年龄:" + getStaffAge() + "\n性别:"
+ getStaffSex() + "\n部门:" +this.getStaffDaparment().getDepartmentName()+"\n职务:"+this.getStaffPost().getPostName();
return str;
}
}
职务类;
package com.imooc.office.system;
部门类;
public class post {
//成员属性;职务编号 职务名称
private String postNo;
private String postName;
//无参构造方法
public post(){
}
//带参构造 实现对属性的的全赋值
public post (String postNo,String postName) {
//this.postNo=postNo; this 调用成员属性
//this.postName=postName;
this.setPostNo(postNo);
this.setPostName(postName);
}
public void setPostNo(String postNo) {
this.postNo=postNo;
}
public String getPostNO() {
return this.postNo;
}
public void setPostName(String postName) {
this.postName=postName;
}
public String getPostName() {
return this.postName;
}
/**
* 部门工号和职务
* @return 返回部门的工号和职务
*/
public String info() {
String str="职务编号:"+this.getPostNO()+"\n职务名称:"+this.getPostName();
return str;
}
package com.imooc.office.system;
//成员属性 部门编号 名称 人员 统计变量
public class department {
private String departmentNo;
private String departmentName;
private staff[] departmentArray;
private int departmentNum;
private int departmentNum2;
// 构造无参方法
public department() {
}
//带参构造方法 给成员属性赋值
public department(String departmentNo, String departmentName) {
this.setDepartmentNo(departmentNo);
this.setDepartmentName(departmentName);
this.setDepartmentArray(departmentArray);
this.setDepartmentNum(departmentNum);
}
public String getDepartmentNo() {
return departmentNo;
}
public void setDepartmentNo(String departmentNo) {
this.departmentNo = departmentNo;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public int getDepartmentNum() {
return departmentNum;
}
public void setDepartmentNum(int departmentNum) {
this.departmentNum = departmentNum;
}
public staff[] getDepartmentArray() {
if(this.departmentArray==null)
this.departmentArray=new staff[200];
return departmentArray;
}
public void setDepartmentArray(staff[] departmentArray) {
this.departmentArray = departmentArray;
}
public int getDepartmentNum2() {
return departmentNum2;
}
public void setDepartmentNum2(int departmentNum2) {
this.departmentNum2 = departmentNum2;
}
public String info1() {
String str="部门编号:"+this.getDepartmentNo()+"\n部门名称:"+this.getDepartmentName();
return str;
}
public void addStaff(staff dep) {
for(int i=0;i<this.getDepartmentArray().length;i++){
if(this.getDepartmentArray()[i]==null);
this.getDepartmentArray()[i]=dep;
if(dep.getStaffName()=="人事部") {
this.departmentNum=i+1;
return;
} else
if(dep.getStaffName()=="市场部")
this.departmentNum2=i+1;
return;
}
}
}
测试类
package com.imooc.test;
import com.imooc.office.system.post;
import com.imooc.office.system.staff;
import com.imooc.office.system.department;
public class OfficeTest {
public static void main (String[] args) {
department department1 = new department("D001","人事部");
System.out.println(department1.info1());
department department2= new department("D002","市场部");
System.out.println(department2.info1());
//测试部门 编号 职务信息
post post0 = new post("P001","经理");
System.out.println(post0.info());
post post1 = new post("P002","助理");
System.out.println(post1.info());
post post2 = new post("P003","职员");
System.out.println(post2.info());
//测试员工类 姓名 工号 年龄 性别 部门 职务
staff s0=new staff("张三","S001",29,"男",department1,post0);
System.out.println(s0.introduction(department1));
staff s1=new staff("李四","S002",21,"女",department1,post1);
System.out.println(s1.introduction(department1));
staff s2=new staff("王五","S003",29,"男",department1,post2);
System.out.println(s2.introduction(department1));
staff s3=new staff("赵六","S004",26,"女",department2,post0);
System.out.println(s3.introduction(department2));
staff s4=new staff("钱七","S005",37,"男",department2,post0);
System.out.println(s4.introduction(department2));
staff s5=new staff("王八","S006",39,"女",department2,post2);
System.out.println(s5.introduction(department2));
//统计部门人数
department1.addStaff(s0);
department2.addStaff(s5);
System.out.println("人事部人数为;"+department1.getDepartmentNum());
System.out.println("市场部人数为;"+department2.getDepartmentNum2());
}
}
统计部门人数的方法一直有有问题,第一步部门人数放到数组里面一直提示报错 无法存进去,
public void addStaff(staff dep) {
for(int i=0;i<this.getDepartmentArray().length;i++){
if(this.getDepartmentArray()[i]==null);
this.getDepartmentArray()[i]=dep;
if(dep.getStaffName()=="人事部") {
this.departmentNum=i+1;
return;
} else
if(dep.getStaffName()=="市场部")
this.departmentNum2=i+1;
return;
}
这段代码不知道怎么修改才能正确输出统计人数,麻烦老师说明一下,谢谢
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星