综合案例
程序员文章站
2022-07-04 16:26:01
...
选择
1.B
2.AD
编程
//部门类
public class Department {
private String departmentNum;//部门编号
private String departmentName;//部门名称
private Staff[] s;//部门员工数组
private int number;//部门员工人数
public Department() {}
public String getDepartmentNum() {
return departmentNum;
}
public void setDepartmentNum(String departmentNum) {
this.departmentNum = departmentNum;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public Staff[] getS() {
if (this.s == null) {
this.s = new Staff[3];
}
return s;
}
public void setS(Staff[] s) {
this.s = s;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
//带参构造
public Department(String departmentNum, String departmentName) {
super();
this.setDepartmentNum(departmentNum);
this.setDepartmentName(departmentName);
}
//统计该部门人数
public void addStaff(Staff s) {
int i;
for(i = 0; i < this.getS().length; i++) {
if(this.getS()[i] == null) {
this.getS()[i] = s;
break;
}
}
this.setNumber(i + 1);
}
}
//职务类
public class Post {
private String postNum;//职务编号
private String postName;//职务名称
public Post() {}
public String getPostNum() {
return postNum;
}
public void setPostNum(String postNum) {
this.postNum = postNum;
}
public String getPostName() {
return postName;
}
public void setPostName(String postName) {
this.postName = postName;
}
public Post(String postNum, String postName) {
super();
this.setPostNum(postNum);
this.setPostName(postName);
}
}
//员工类
public class Staff {
private String staffName;//员工姓名
private String staffNum;//员工工号
private String sex;//性别
private int age;//年龄
private String department;//所在部门
private String post;//职务
public Staff() {}
public String getStaffName() {
return staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName;
}
public String getStaffNum() {
return staffNum;
}
public void setStaffNum(String staffNum) {
this.staffNum = staffNum;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
if(sex.equals("男") || sex.equals("女")) {
this.sex = sex;
} else {
sex = "男";
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age >= 18 && age <= 65) {
this.age = age;
} else {
age = 18;
}
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public Staff(String staffName, String staffNum, String sex, int age, String department, String post) {
super();
this.setStaffName(staffName);
this.setStaffNum(staffNum);
this.setSex(sex);
this.setAge(age);
this.setDepartment(department);
this.setPost(post);
}
public String info() {
String str = "姓名:" + this.getStaffName() + "\n";
str += "工号:" + this.getStaffNum() + "\n";
str += "性别:" + this.getSex() + "\n";
str += "年龄:" + this.getAge() + "\n";
str += "职务:" + this.getDepartment() + this.getPost();
return str;
}
}
//测试类
public class Test {
public static void main(String[] args) {
Staff s1 = new Staff("张铭", "S001", "男", 29, "人事部", "经理");
System.out.println(s1.info());
System.out.println("======================");
Staff s2 = new Staff("李艾爱", "S002", "女", 21, "人事部", "助理");
System.out.println(s2.info());
System.out.println("======================");
Staff s3 = new Staff("孙超", "S003", "男", 29, "人事部", "职员");
System.out.println(s3.info());
System.out.println("======================");
Staff s4 = new Staff("张美美", "S004", "女", 26, "市场部", "职员");
System.out.println(s4.info());
System.out.println("======================");
Staff s5 = new Staff("蓝迪", "S005", "男", 37, "市场部", "经理");
System.out.println(s5.info());
System.out.println("======================");
Staff s6 = new Staff("米莉", "S006", "女", 24, "市场部", "职员");
System.out.println(s6.info());
System.out.println("======================");
Department d1 = new Department("D001", "人事部");
d1.addStaff(s1);
d1.addStaff(s2);
d1.addStaff(s3);
System.out.println("人事部总共有" + d1.getNumber() + "员工");
Department d2 = new Department("D002", "市场部");
d2.addStaff(s4);
d2.addStaff(s5);
d2.addStaff(s6);
System.out.println("市场部总共有" + d2.getNumber() + "员工");
}
}
上一篇: 综合案例
下一篇: 案例-axios版 (列表增删查)