修改后是否正确
package com.imooc.demo;
/**
* 员工类:名字,工号,性别,年龄
* @author Lyle
*
*/
public class Staff {
//定义员工属性:名字。工号,性别,年龄,
private String staffName;//员工姓名
private String staffNo;//工号
private String staffSex;//员工性别
private int staffAge;//员工年龄
private Department myDepartment;//部门信息
private Position myPosition;//职位信息
public Staff() {
}
//构建带参函数
public Staff(String staffName,String staffNo,String staffSex,int staffAge,Department myDeprtment,Position myPosition) {
this.setStaffName(staffName);
this.setStaffSex(staffSex);
this.setStaffAge(staffAge);
this.setStaffNo(staffNo);
this.setMyDepartment(myDeprtment);
this.setMyPosition(myPosition);
}
//构建get,set函数
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 String getStaffSex() {
return staffSex;
}
/**
* 定义性别属性,只能是“男”或“女”,反之则强制赋值为男
* @param staffSex
*/
public void setStaffSex(String staffSex) {
if(staffSex.equals("男")||staffSex.equals("女"))
this.staffSex = staffSex;
else
this.staffSex="男";
}
public int getStaffAge() {
return staffAge;
}
/**
* 获取部门信息,如果没有实例化,先实例化后返回
* @return 专业对象信息
*/
public Position getMyPosition() {
if(this.myPosition==null) {
this.myPosition=new Position();
}
return myPosition;
}
public void setMyPosition(Position myPosition) {
this.myPosition = myPosition;
}
/**
* 获取职位信息,如果没有实例化,先实例化后返回
* @return 职位信息
*/
public Department getMyDepartment() {
if(this.myDepartment==null)
this.myDepartment=new Department();
return myDepartment;
}
public void setMyDepartment(Department myDepartment) {
this.myDepartment = myDepartment;
}
/**
* 限定年龄是在18-65之间
* 反之则设置为18岁
* @param staffAge
*/
public void setStaffAge(int staffAge) {
if(staffAge<18||staffAge>65) {
this.staffAge=18;
}else
this.staffAge = staffAge;
}
/**
* 输出员工信息:名字,工号,性别,年龄,部门职务
* @param myDepartment 部门
* @param myPosition 职务
* @return
*/
public String into(Department myDepartment,Position mtPosition) {
String str="姓名:"+this.getStaffName()+"\n工号:"+this.getStaffNo()+"\n性别:"+this.getStaffSex()+"\n年龄:"+this.getStaffAge()+"\n职务:"+this.myDepartment.getDepartName()+this.myPosition.getJobName();
return str;
}
}
package com.imooc.demo;
/**
* 职务名称
* @author lyle
*
*/
public class Position {
private String jobName;//职务名称
private int jobNo;//职务编号
//构建无参方法
public Position() {
}
//构建带参方法,福所有属性进行赋值
public Position(String jobName,int jobNo) {
this.setJobName(jobName);
this.setJobNo(jobNo);
}
//生成get,set方法
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public int getJobNo() {
return jobNo;
}
public void setJobNo(int jobNo) {
this.jobNo = jobNo;
}
}
package com.imooc.demo;
/**
* 定义部门信息:部门名称,部门编号,员工组,员工个数
* @author lyle
*
*/
public class Department {
private String departName;//部门名称
private int departNo;//部门编号
private Staff[] myStaffs=new Staff[111];//员工总数
private int departNum;//员工个数
//构建无参方法
public Department() {
}
//带参构造,实现对部门名称,部门编号。员工数,
public Department(String departName,int departNo){
this.setDepartName(departName);
this.setDepartNo(departNo);
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
public int getDepartNo() {
return departNo;
}
public void setDepartNo(int departNo) {
this.departNo = departNo;
}
public Staff[] getMyStaffs() {
return myStaffs;
}
public void setMyStaffs(Staff[] myStaffs) {
this.myStaffs = myStaffs;
}
public int getDepartNum() {
return departNum;
}
public void setDepartNum(int departNum) {
this.departNum = departNum;
}
public void addDep(Staff stu) {
/**
* 1.将员工传入数组中
* 2.将员工的个数保存到departNum
*/
for(int i=0;i<this.getMyStaffs().length;i++) {
if(this.getMyStaffs()[i]==null) {
this.getMyStaffs()[i]=stu;
this.departNum=i+1;
break;
}
}
}
}
package com.imooc.demo;
public class Test {
public static void main(String[] args) {
//测试员工信息
//构建部门:市场部,人事部
Department dep1=new Department("人事部",1);
Department dep2=new Department("市场部",2);
//构建三个职位
Position pos1=new Position("经理",1);
Position pos2=new Position("助理",2);
Position pos3=new Position("职员",3);
//员工张铭
Staff sta1=new Staff("张铭","S001","男",23,dep1,pos1);
System.out.println(sta1.into(dep1, pos1));
System.out.println("====================================");
//员工李艾爱
Staff sta2=new Staff("李艾爱","S002","女",21,dep1,pos2);
System.out.println(sta2.into(dep1, pos2));
System.out.println("====================================");
//员工孙超
Staff sta3=new Staff("孙超","S004","男",29,dep1,pos3);
System.out.println(sta3.into(dep1, pos3));
System.out.println("====================================");
//员工张美美
Staff sta4=new Staff("张美美","S005","女",26,dep2,pos3);
System.out.println(sta4.into(dep2, pos2));
System.out.println("====================================");
//员工蓝迪
Staff sta5=new Staff("蓝迪","S006","男",37,dep2,pos1);
System.out.println(sta5.into(dep2, pos1));
System.out.println("====================================");
//员工米莉
Staff sta6=new Staff("米莉","S007","女",24,dep2,pos3);
System.out.println(sta6.into(dep2, pos3));
System.out.println("====================================");
//显示市场部人数,人事部人数
dep1.addDep(sta1);
dep1.addDep(sta2);
dep1.addDep(sta3);
dep2.addDep(sta4);
dep2.addDep(sta5);
dep2.addDep(sta6);
System.out.println(dep1.getDepartName()+"总共有"+dep1.getDepartNum()+"名员工");
System.out.println(dep2.getDepartName()+"总共有"+dep2.getDepartNum()+"名员工");
}
}
正在回答
同学你好,代码完成的很棒哦~继续努力,但是建议同学下次将作业上传哦~不要在问答区提交作业哦~方便老师批复
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星