Java简单类(部门、领导、雇员关系)

public class Swater{
    public static void main(String []args){
        Print print = new Print();
        print.out();
    }
}
//打印类
class Print{
    public void out(){
        Department dep1 = new Department(122,"技术部","开发工程师");//第一个部门
        Department dep2 = new Department(123,"运维部","Hadoop工程师");//第二个部门
        Department dep3 = new Department(124,"人事部","会计师");//第三个部门
        Employee e1 = new Employee(001,"张三",6000.0);//第一个员工
        Employee e2 = new Employee(002,"李四",7000.0);//第二个员工
        Employee e3 = new Employee(003,"王五",10000.0);//第二个员工
        e1.setLeader(e2);//雇员与领导
        e2.setLeader(e3);//雇员与领导
        e1.setLeader(e3);//雇员与领导
        e1.setDepartment(dep1);//雇员与部门
        e2.setDepartment(dep2);//雇员与部门
        e3.setDepartment(dep3);//雇员与部门
        
        //根据部门找到所有的雇员以及每个雇员的领导信息
        dep1.setEmployee(new Employee[]{e1,e2});//部门与雇员
        dep2.setEmployee(new Employee[]{e3,e2});//部门与雇员
        dep3.setEmployee(new Employee[]{e1,e2,e3});//部门与雇员
        
        System.out.print(e1.getInfo());//通过雇员1找到部门、领导 的信息
        System.out.print("===部门==领导====\n");
        System.out.print(e1.getLeader().getInfo());
        System.out.print(e1.getDepartment().getInfo());
        System.out.println();
        
        System.out.print(e2.getInfo());//通过雇员2找到部门、领导 的信息
        System.out.print("===部门==领导====\n");
        System.out.print(e2.getLeader().getInfo());
        System.out.print(e2.getDepartment().getInfo());
        System.out.println();
        
        System.out.print(e3.getInfo());//通过雇员3找到部门的信息
        System.out.print("===部门====\n");
        System.out.print(e3.getDepartment().getInfo());    
        System.out.println();
        
        System.out.print("--根据部门找到所有的雇员以及每个雇员的领导信息-\n");
        System.out.print("------------------------------\n");
        System.out.println(dep1.getInfo()) ;
        
        for(int i=0;i<dep1.getEmployee().length;i++){
            System.out.print("|-"+dep1.getEmployee()[i].getInfo()+"");
            if(dep1.getEmployee()[i].getLeader()!=null){
                System.out.print("|-"+dep1.getEmployee()[i].getLeader().getInfo());
            }
        }
        
        System.out.print("--根据部门找到所有的雇员以及每个雇员的领导信息-\n");
        System.out.print("------------------------------\n");
        System.out.println(dep2.getInfo()) ;
        
        for(int j=0;j<dep2.getEmployee().length;j++){
            System.out.print("|-"+dep1.getEmployee()[j].getInfo()+"");
            if(dep2.getEmployee()[j].getLeader()!=null){
                System.out.print("|-"+dep2.getEmployee()[j].getLeader().getInfo());
            }
        }
        
        System.out.print("--根据部门找到所有的雇员以及每个雇员的领导信息-\n");
        System.out.print("------------------------------\n");
        System.out.println(dep3.getInfo()) ;
        
        for(int k=0;k<dep3.getEmployee().length;k++){
            System.out.print("|-"+dep3.getEmployee()[k].getInfo()+"");
            if(dep3.getEmployee()[k].getLeader()!=null){
                System.out.print("|-"+dep3.getEmployee()[k].getLeader().getInfo());
            }
        }
    }
}

//部门 类
class Department{
    private int did;//部门编号
    private String dname;//部门名字
    private String dposition;//职位
    private Employee []employee;//多个雇员
    public Department(){
        //无参构造方法
    }
    public Department(int did,String dname,String dposition){
        this.did=did;
        this.dname=dname;
        this.dposition=dposition;
    }
    //部门下的雇员
    public void setEmployee(Employee[] employee){
        this.employee=employee;
    }
    public Employee[] getEmployee(){
        return this.employee;
    }
    public String getInfo(){
        return "部门编号-"+did+"\n部门-"+dname+"\n职位-"+dposition+"\n";
    }
}
//员工类
class Employee{
    private int id;//工号
    private String name;//姓名
    private double salary;//薪水
    private Department department;//雇员对应部门
    private Employee Leader;
    public Employee(){
        //无参构造方法
    }
    public Employee(int id,String name,double salary){
        this.id = id;
        this.name=name;
        this.salary=salary;
    }
    //雇员的部门
    public void setDepartment(Department department){
        this.department=department;
    }
    public Department getDepartment(){
        return this.department;
    }
    //雇员的领导
    public void setLeader(Employee Leader){
        this.Leader=Leader;
    }
    public Employee getLeader(){
        return this.Leader;
    }
    public String getInfo(){
        return "雇员编号-"+id+"\n雇员姓名-"+name+"\n雇员薪水-"+" "+salary+"\n";
    }
    
}

 

转载于:https://www.cnblogs.com/suway/articles/9320754.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值