老师,在限定年龄和性别的这个地方怎么编写呢?
老师,在限定年龄和性别的这个地方怎么编写呢?这个地方一直没做出来
personnel类
public class Personnel {
//成员属性:姓名,工号,性别,年龄,所在部门,职务,人事部员工信息,人事部员工个数
private String PersonnelName;
private String PersonnelNo;
private String PersonnelSex;
private int PersonnelAge;
private String PersonnelBumen;
private String PersonnelZhiwu;
private Personnel[] myPersonnel;
private int personnelNum;
//无参构造方法
public Personnel() {
super();
}
//多参构造方法,实现对部分属性的赋值
public Personnel(String personnelName, String personnelNo,String personnelSex,int personnelAge,
String personnelBumen, String personnelZhiwu) {
this.setPersonnelName(personnelName);
this.setPersonnelNo(personnelNo);
this.setPersonnelSex(personnelSex);
this.setPersonnelAge(personnelAge);
this.setPersonnelBumen(personnelBumen);
this.setPersonnelZhiwu(personnelZhiwu);
}
public int getPersonnelNum() {
return personnelNum;
}
public void setPersonnelNum(int personnelNum) {
this.personnelNum = personnelNum;
}
public String getPersonnelName() {
return PersonnelName;
}
public void setPersonnelName(String personnelName) {
PersonnelName = personnelName;
}
public String getPersonnelNo() {
return PersonnelNo;
}
public void setPersonnelNo(String personnelNo) {
PersonnelNo = personnelNo;
}
public String getPersonnelSex() {
String getPersonnelSex;
if(PersonnelSex.equals("女"))
System.out.println(getPersonnelSex="女");
else
System.out.println(getPersonnelSex="男");
return PersonnelSex;
}
public void setPersonnelSex(String personnelSex) {
PersonnelSex = personnelSex;
}
public int getPersonnelAge() {
return PersonnelAge;
}
public void setPersonnelAge(int personnelAge) {
PersonnelAge = personnelAge;
}
public String getPersonnelBumen() {
return PersonnelBumen;
}
public void setPersonnelBumen(String personnelBumen) {
PersonnelBumen = personnelBumen;
}
public String getPersonnelZhiwu() {
return PersonnelZhiwu;
}
public void setPersonnelZhiwu(String personnelZhiwu) {
PersonnelZhiwu = personnelZhiwu;
}
/**
* 人事部自我介紹的方法
* @return 自我介紹的信息,包括姓名,工号,性別,年齡,职务
*/
public String introduction() {
String str1="姓名:"+this.getPersonnelName()+"\n工号:"+this.getPersonnelNo()+"\n性别:"+this.getPersonnelSex()+"\n年龄:"+this.getPersonnelAge()+"\n职务:"+this.getPersonnelBumen()+this.getPersonnelZhiwu();
return str1;
}
public void addPersonnel(Personnel per) {
/*
* 1.将员工保存到数组中
* 2.将员工个数保存到personnelNum中
*/
//1.将员工保存到数组中
for(int i=0;i<this.getMyPersonnel().length;i++) {
if(this.getMyPersonnel()[i]==null) {
this.getMyPersonnel()[i]=per;
//2.将员工个数保存到personnelNum中
this.personnelNum=i+1;
return;
}
}
}
/**
* 获取人事部的员工信息,如果保存员工信息的数组未被初始化,则先初始化长度为10
* @return
*/
public Personnel[] getMyPersonnel() {
if(this.myPersonnel==null)
this.myPersonnel=new Personnel[10];
return myPersonnel;
}
public void setMyPersonnel(Personnel[] myPersonnel) {
this.myPersonnel = myPersonnel;
}
}
department类
package com.zuoye.model;
public class Department {
//成员属性:姓名,工号,性别,年龄,所在部门,职务,市场部员工信息,市场部员工个数
private String DepartmentName;
private String DepartmentNo;
private String DepartmentSex;
private int DepartmentAge;
private String DepartmentBumen;
private String DepartmentZhiwu;
private Department[] myDepartment;
private int DepartmentNum;
//无参构造方法
public Department() {
super();
}
//多参构造方法,实现对全部属性的赋值
public Department(String departmentName, String departmentNo, String departmentSex, int departmentAge,
String departmentBumen, String departmentZhiwu) {
this.setDepartmentName(departmentName);
this.setDepartmentNo(departmentNo);
this.setDepartmentSex(departmentSex);
this.setDepartmentAge(departmentAge);
this.setDepartmentBumen(departmentBumen);
this.setDepartmentZhiwu(departmentZhiwu);
}
/**
* 获取市场部的员工信息,如果保存员工信息的数组未被初始化,则先初始化长度为10
* @return
*/
public Department[] getMyDepartment() {
if(this.myDepartment==null)
this.myDepartment=new Department[10];
return myDepartment;
}
public void setMyDepartment(Department[] myDepartment) {
this.myDepartment = myDepartment;
}
public int getDepartmentNum() {
return DepartmentNum;
}
public void setDepartmentNum(int departmentNum) {
DepartmentNum = departmentNum;
}
public String getDepartmentName() {
return DepartmentName;
}
public void setDepartmentName(String departmentName) {
DepartmentName = departmentName;
}
public String getDepartmentNo() {
return DepartmentNo;
}
public void setDepartmentNo(String departmentNo) {
DepartmentNo = departmentNo;
}
public String getDepartmentSex() {
return DepartmentSex;
}
public void setDepartmentSex(String departmentSex) {
DepartmentSex = departmentSex;
}
public int getDepartmentAge() {
return DepartmentAge;
}
public void setDepartmentAge(int departmentAge) {
DepartmentAge = departmentAge;
}
public String getDepartmentBumen() {
return DepartmentBumen;
}
public void setDepartmentBumen(String departmentBumen) {
DepartmentBumen = departmentBumen;
}
public String getDepartmentZhiwu() {
return DepartmentZhiwu;
}
public void setDepartmentZhiwu(String departmentZhiwu) {
DepartmentZhiwu = departmentZhiwu;
}
/**
* 市场部自我介紹的方法
* @return 自我介紹的信息,包括姓名,工号,性別,年齡,职务
*/
public String introduction() {
String str2="姓名:"+this.getDepartmentName()+"\n工号:"+this.getDepartmentNo()+"\n性别:"+this.getDepartmentSex()+"\n年龄:"+this.getDepartmentAge()+"\n职务:"+this.getDepartmentBumen()+this.getDepartmentZhiwu();
return str2;
}
public void addDepartment(Department dep) {
/*
* 1.将员工保存到数组中
* 2.将员工个数保存到personnelNum中
*/
//1.将员工保存到数组中
for(int i=0;i<this.getMyDepartment().length;i++) {
if(this.getMyDepartment()[i]==null) {
this.getMyDepartment()[i]=dep;
//2.将员工个数保存到personnelNum中
this.DepartmentNum=i+1;
return;
}
}
}
}
测试类managetest
package com.zuoye.test;
import com.zuoye.model.Department;
import com.zuoye.model.Personnel;
public class ManageTest {
public static void main(String[] args) {
//測試Personnel
Personnel per1=new Personnel("张铭","S001","男",29,"人事部","经理" );
System.out.println(per1.introduction());
System.out.println("===========================");
Personnel per2=new Personnel("李艾爱","S002","女",21,"人事部","助理" );
System.out.println(per2.introduction());
System.out.println("===========================");
Personnel per3=new Personnel("孙超","S004","男",29,"人事部","职员" );
System.out.println(per3.introduction());
System.out.println("===========================");
//測試Department
Department dep1=new Department("张美美","S005","女",26,"市场部","职员" );
System.out.println(dep1.introduction());
System.out.println("===========================");
Department dep2=new Department("蓝迪","S006","男",37,"市场部","经理" );
System.out.println(dep2.introduction());
System.out.println("===========================");
Department dep3=new Department("米莉","S007","女",24,"市场部","职员" );
System.out.println(dep3.introduction());
System.out.println("===========================");
//测试人事部有多少名员工
per1.addPersonnel(per1);
per1.addPersonnel(per2);
per1.addPersonnel(per3);
System.out.println(per1.getPersonnelBumen()+"总共有"+per1.getPersonnelNum()+"名员工");
//测试市场部有多少名员工
dep1.addDepartment(dep1);
dep1.addDepartment(dep2);
dep1.addDepartment(dep3);
System.out.println(dep1.getDepartmentBumen()+"总共有"+dep1.getDepartmentNum()+"名员工");
}
}
正在回答 回答被采纳积分+1
同学,你好 首先限定年龄和性别的限定条件应该写入年龄和性别的set写方法中
1、针对性别判断,将sex的判断条件写在了get读方法中,建议将
public void setPersonnelSex(String personnelSex) { if(personnelSex.equals("女")) this.personnelSex=personnelSex; }else this.personnelSex="男"; }
条件判断写在set写方法中。
2、针对年龄的判断,建议将年龄的判断条件
public void setPersonnelSex(String personnelSex) { if(personnelSex>=18&&personnelSex<=65) this.personnelSex=personnelSex; else this.personnelSex="18"; }
写入年龄的set方法中
请注意变量的命名规范,变量名单词首字母小写
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星