老师你们 请问这当中的 给部门分类并计算次数的代码有什么优化方案 我这个虽然可以做到但是感觉过于简陋
package com.imooc.office;
public class Section {
//部门编号、部门名称、员工数组、员工人数
private String sectionNo;
private String sectionName;
private Staff[] sectionNum; //用于存放信息的数组
private int StaffNum; //用于统计人事部员工人数
private int StaffNum2; //用于统计市场部员工人员
int a,b; //用于辅助计算人事部和市场部的人数
//无参构造方法
public Section() {
}
//带参构造方法,实现对部门的初始化
public Section(String sectionNo, String sectionName) {
this.setSectionName(sectionName);
this.setSectionNo(sectionNo);
}
public String getSectionNo() {
return sectionNo;
}
public void setSectionNo(String sectionNo) {
this.sectionNo = sectionNo;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
// 当数组没赋值时给数组赋初值
public Staff[] getSectionNum() {
if (this.sectionNum == null) {
this.sectionNum = new Staff[200];
}
return sectionNum;
}
public int getStaffNum() {
return StaffNum;
}
public void setStaffNum(int staffNum) {
StaffNum = staffNum;
}
public void setSectionNum(Staff[] sectionNum) {
this.sectionNum = sectionNum;
}
public int getStaffNum2() {
return StaffNum2;
}
public void setStaffNum2(int staffNum2) {
StaffNum2 = staffNum2;
}
/**
* 给各部添加成员
* @param check
*/
public void add(Staff check) {
//1 根据部门给员工分组
//2 计算部门员工人数。
for(int i=0;i<this.getSectionNum().length;i++) {
if(this.getSectionNum()[i]==null) {
this.getSectionNum()[i]=check;
// this.setStaffNum(i+1);
break;
}
}
if(check.getSection().getSectionName().equals("人事部")) {
//a++;
this.setStaffNum(++a);
}if(check.getSection().getSectionName().equals("市场部")){
//b++;
this.setStaffNum2(++b);
}
}
}package com.imooc.office;
public class Staff {
// 姓名、性别、年龄、工号、所在部门、职务
private String name;
private String set;
private int age;
private String workNo;
private Section section;
private Position position;
// 无参构造方法
public Staff() {
}
// 带参构造方法,实现对姓名、性别、年龄、工号、部门、职务的赋值
public Staff(String name, String workNo, String set, int age,Section section,Position position) {
this.setAge(age);
this.setName(name);
this.setSet(set);
this.setWorkNo(workNo);
this.setSection(section);
this.setPosition(position);
}
public String getWorkNo() {
return workNo;
}
public void setWorkNo(String workNo) {
this.workNo = workNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSet() {
return set;
}
//加入性别不是男女当中一个 就判定默认为男
public void setSet(String set) {
if("男".equals(set)|"女".equals(set)) {
this.set = set;
}else {
this.set="男";
}
}
public int getAge() {
return age;
}
//当年龄小于18或者大于65时 默认为18
public void setAge(int age) {
if(age<18|age>65) {
this.age=18;
}else
this.age = age;
}
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section = section;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
/**
* 用于返回姓名、性别、年龄、工号、职务、部门
* @return 返回要输出的值
*/
public String ouPut() {
String str;
str = "姓名:" + this.getName() + "\n工号:" + this.getWorkNo() + "\n性别:" + this.getSet() + "\n年龄:" + this.getAge()
+ "\n职务:" + this.getSection().getSectionName() + this.getPosition().getPositionName()
+ "\n=====================================================";
return str;
}
}package com.imooc.office;
public class Position {
//职务编号、职务名称
private String positionNo;
private String positionName;
//无参构造方法
public Position() {
}
//带参构造方法,实现对职务的初始化
public Position(String positionNo, String positionName) {
this.setPositionName(positionName);
this.setPositionNo(positionNo);
}
public String getPositionNo() {
return positionNo;
}
public void setPositionNo(String positionNo) {
this.positionNo = positionNo;
}
public String getPositionName() {
return positionName;
}
public void setPositionName(String positionName) {
this.positionName = positionName;
}
}package com.imooc.text;
import com.imooc.office.*;
public class Text {
public static void main(String[] args) {
//初始化部门
Section bm1=new Section("D001","人事部");
Section bm2=new Section("D002","市场部");
//初始化职务
Position zw1=new Position("P001","经理");
Position zw2=new Position("P002","助理");
Position zw3=new Position("P003","职员");
//输入员工信息
Staff a1=new Staff("张铭","S001","男",29,bm1,zw1);
Staff a2=new Staff("李艾爱","S002","女",21,bm1,zw2);
Staff a3=new Staff("孙超","S003","男",29,bm1,zw3);
Staff a4=new Staff("张美美","S004","女",26,bm2,zw3);
Staff a5=new Staff("蓝迪","S005","男",37,bm2,zw1);
Staff a6=new Staff("米莉","S006","女",24,bm2,zw3);
//将员工信息传入数组
Section se=new Section();
se.add(a1);
se.add(a2);
se.add(a3);
se.add(a4);
se.add(a5);
se.add(a6);
//输出员工信息并给部门分组
System.out.println(a1.ouPut());
System.out.println(a2.ouPut());
System.out.println(a3.ouPut());
System.out.println(a4.ouPut());
System.out.println(a5.ouPut());
System.out.println(a6.ouPut());
System.out.println("人事部总共有"+se.getStaffNum()+"名员工");
System.out.println("市场部总共有"+se.getStaffNum2()+"名员工");
}
}19
收起
正在回答
2回答
同学你好,添加员工的方法优化代码思路如下:
写一个添加员工的方法,参数是员工类,方法内写一个for循环,for循环中的条件是int i = 0 ; i < this.getSectionNum().length ; i ++,循环内写一个if语句,判断如果 this.getSectionNum()[i]为null,把传递过来的员工对象赋值给this.getSectionNum()[i]。并且把员工数加一,return。此过程是把员工添加到数组中,并且统计对应员工个数。具体修改的代码如下:
Section类:

Section类:

Test类将代码粘贴给同学,修改之处把同学的代码进行了注释,同学注意区分:
public class Text {
public static void main(String[] args) {
//初始化部门
Section bm1=new Section("D001","人事部");
Section bm2=new Section("D002","市场部");
//初始化职务
Position zw1=new Position("P001","经理");
Position zw2=new Position("P002","助理");
Position zw3=new Position("P003","职员");
//输入员工信息
Staff a1=new Staff("张铭","S001","男",29,bm1,zw1);
Staff a2=new Staff("李艾爱","S002","女",21,bm1,zw2);
Staff a3=new Staff("孙超","S003","男",29,bm1,zw3);
Staff a4=new Staff("张美美","S004","女",26,bm1,zw3);
Staff a5=new Staff("蓝迪","S005","男",37,bm2,zw1);
Staff a6=new Staff("米莉","S006","女",24,bm2,zw3);
//将员工信息传入数组
/*Section se=new Section();
se.addStaff(a1);
se.addStaff(a2);
se.addStaff(a3);
se.addStaff(a4);
se.addStaff(a5);
se.addStaff(a6);*/
bm1.addStaff(a1);
bm1.addStaff(a2);
bm1.addStaff(a3);
bm2.addStaff(a4);
bm2.addStaff(a5);
bm2.addStaff(a6);
//输出员工信息并给部门分组
System.out.println(a1.ouPut());
System.out.println(a2.ouPut());
System.out.println(a3.ouPut());
System.out.println(a4.ouPut());
System.out.println(a5.ouPut());
System.out.println(a6.ouPut());
/*System.out.println("人事部总共有"+se.getStaffNum()+"名员工");
System.out.println("市场部总共有"+se.getStaffNum2()+"名员工");*/
System.out.println("人事部总共有"+bm1.getStaffNum()+"名员工");
System.out.println("市场部总共有"+bm2.getStaffNum()+"名员工");
}
}如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
好帮手慕小琪
2020-04-27 20:48:50
同学你好,老师用的类分别是:同学更改后的Section类,之前粘贴的Position类和Staff类,和老师改过的Test类。老师运行是没有问题的,老师将全部代码粘贴给同学,同学注意区分一下:
//Section类
public class Section {
//部门编号、部门名称、员工数组、员工人数
private String sectionNo;
private String sectionName;
private Staff[] sectionNum; //用于存放信息的数组
private int StaffNum; //用于统计人事部员工人数
//无参构造方法
public Section() {
}
//带参构造方法,实现对部门的初始化
public Section(String sectionNo, String sectionName) {
this.setSectionName(sectionName);
this.setSectionNo(sectionNo);
}
public String getSectionNo() {
return sectionNo;
}
public void setSectionNo(String sectionNo) {
this.sectionNo = sectionNo;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
// 当数组没赋值时给数组赋初值
public Staff[] getSectionNum() {
if (this.sectionNum == null) {
this.sectionNum = new Staff[200];
}
return sectionNum;
}
public void setSectionNum(Staff[] sectionNum) {
this.sectionNum = sectionNum;
}
public int getStaffNum() {
return StaffNum;
}
public void setStaffNum(int staffNum) {
StaffNum = staffNum;
}
/**
* 给各部添加成员
*
* @param check
*/
public void add(Staff check) {
//1 根据部门给员工分组
//2 计算部门员工人数。
Staff[] staff = this.getSectionNum();
for (int i = 0; i < staff.length; i++) {
if (staff[i] == null) {
check.setSection(this);
//将新成员放入部门中的员工数组
staff[i] = check;
//统计部门员工个数
this.StaffNum = i + 1;
return;
}
}
}
}//Position类
public class Position {
//职务编号、职务名称
private String positionNo;
private String positionName;
//无参构造方法
public Position() {
}
//带参构造方法,实现对职务的初始化
public Position(String positionNo, String positionName) {
this.setPositionName(positionName);
this.setPositionNo(positionNo);
}
public String getPositionNo() {
return positionNo;
}
public void setPositionNo(String positionNo) {
this.positionNo = positionNo;
}
public String getPositionName() {
return positionName;
}
public void setPositionName(String positionName) {
this.positionName = positionName;
}
}//Staff类
public class Staff {
// 姓名、性别、年龄、工号、所在部门、职务
private String name;
private String set;
private int age;
private String workNo;
private Section section;
private Position position;
// 无参构造方法
public Staff() {
}
// 带参构造方法,实现对姓名、性别、年龄、工号、部门、职务的赋值
public Staff(String name, String workNo, String set, int age,Section section,Position position) {
this.setAge(age);
this.setName(name);
this.setSet(set);
this.setWorkNo(workNo);
this.setSection(section);
this.setPosition(position);
}
public String getWorkNo() {
return workNo;
}
public void setWorkNo(String workNo) {
this.workNo = workNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSet() {
return set;
}
//加入性别不是男女当中一个 就判定默认为男
public void setSet(String set) {
if("男".equals(set)|"女".equals(set)) {
this.set = set;
}else {
this.set="男";
}
}
public int getAge() {
return age;
}
//当年龄小于18或者大于65时 默认为18
public void setAge(int age) {
if(age<18|age>65) {
this.age=18;
}else
this.age = age;
}
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section = section;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
/**
* 用于返回姓名、性别、年龄、工号、职务、部门
* @return 返回要输出的值
*/
public String ouPut() {
String str;
str = "姓名:" + this.getName() + "\n工号:" + this.getWorkNo() + "\n性别:" + this.getSet() + "\n年龄:" + this.getAge()
+ "\n职务:" + this.getSection().getSectionName() + this.getPosition().getPositionName()
+ "\n=====================================================";
return str;
}
}//Text类
public class Text {
public static void main(String[] args) {
//初始化部门
Section bm1=new Section("D001","人事部");
Section bm2=new Section("D002","市场部");
//初始化职务
Position zw1=new Position("P001","经理");
Position zw2=new Position("P002","助理");
Position zw3=new Position("P003","职员");
//输入员工信息
Staff a1=new Staff("张铭","S001","男",29,bm1,zw1);
Staff a2=new Staff("李艾爱","S002","女",21,bm1,zw2);
Staff a3=new Staff("孙超","S003","男",29,bm1,zw3);
Staff a4=new Staff("张美美","S004","女",26,bm1,zw3);
Staff a5=new Staff("蓝迪","S005","男",37,bm2,zw1);
Staff a6=new Staff("米莉","S006","女",24,bm2,zw3);
//将员工信息传入数组
/*Section se=new Section();
se.addStaff(a1);
se.addStaff(a2);
se.addStaff(a3);
se.addStaff(a4);
se.addStaff(a5);
se.addStaff(a6);*/
bm1.add(a1);
bm1.add(a2);
bm1.add(a3);
bm1.add(a4);
bm2.add(a5);
bm2.add(a6);
//输出员工信息并给部门分组
System.out.println(a1.ouPut());
System.out.println(a2.ouPut());
System.out.println(a3.ouPut());
System.out.println(a4.ouPut());
System.out.println(a5.ouPut());
System.out.println(a6.ouPut());
/*System.out.println("人事部总共有"+se.getStaffNum()+"名员工");
System.out.println("市场部总共有"+se.getStaffNum2()+"名员工");*/
System.out.println("人事部总共有"+bm1.getStaffNum()+"名员工");
System.out.println("市场部总共有"+bm2.getStaffNum()+"名员工");
}
}如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星