//一个部门有多个雇员,一个雇员有一个部门,一个或零个领导
//省略部分属性的setter和getter和无参构造
class Dept {
private int deptno;
private String dname;
private String loc;
private Emp emps[];//定义对象数组属性表示一个部门有多个雇员
public Dept(int deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public void setEmps(Emp emps[]) {
this.emps = emps;
}
public Emp [] getEmps() {
return this.emps;
}
public String getInfo() {
return "部门编号:" + this.deptno + " 部门名称: " + this.dname
+ " 部门位置: " + this.loc;
}
}
class Emp {
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
private Dept dept;//雇员属于哪个部门
private Emp mgr;//雇员的领导
public Emp(int empno, String ename, String job, double sal, double comm) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Dept getDept() {
return this.dept;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public Emp getMgr() {
return this.mgr;
}
public String getInfo() {
return "雇员编号" + this.empno + ",雇员姓名" + this.ename
+",雇员职位" + this.job + ",雇员工资" + this.sal
+ ",雇员佣金" + this.comm;
}
}
public class TestDemo {
public static void main(String args[]) {
//产生各自独立对象
Dept dept = new Dept(1,"西安分部","雁塔区");
Emp ea = new Emp(7101,"james","aaa",1000.5,10);
Emp eb = new Emp(7102,"kobe","bbb",1001.5,20);
Emp ec = new Emp(7103,"tracy","ccc",1002.5,30);
//设置关系数据
//雇员与领导的关系
ea.setMgr(eb);
eb.setMgr(ec);
//部门与雇员的关系
ea.setDept(dept);
eb.setDept(dept);
ec.setDept(etDeptdept);
dept.setEmps(new Emp[] {ea,eb,ec});
//根据条件取得数据
//根据一个雇员查询其所对应的领导信息和部门信息
System.out.println("/t|-" + ea.getMgr().getInfo());
System.out.println("/t|-" + ea.getDept().getInfo());
//根据一个部门取出所有雇员信息及每个雇员对应的领导信息
System.out.println("************************************************");
for (int x = 0; x < dept.getEmps().length; x++) {
System.out.println("/t|-" + dept.getEmps()[x].getInfo());
if (dept.getEmps()[x].getMgr() != null) {
System.out.println("/t|-" + dept.getEmps()[x].getMgr().getInfo());
}
}
}
}