[Java]创建员工类 厨师 服务员 经理 继承于员工 定义公司类展示总资产和所有员工

分析

员工
    属性
        姓名    工号    存款    工资
厨师    继承员工
服务员    继承员工
经理    继承员工
    奖金
公司
    总资产
    所有员工

方法

员工类
private String name;        
空参    带参
get and set

厨师类    继承自 员工类
extends YuanGong
没有添加其他对象 所以
空参    带参

服务员类    继承自 员工类
空参    带参

经理类    继承自 员工类
单独添加 奖金
private double jiangJin();
空参    带参
get and set 

公司类
定义总金额
privaet double zongGongZi():
用集合定义所有员工
private ArrayList<YuanGong> list;
空参    带参
定义方法    展示公司的所有信息
​    如果没有信息输出
​    遍历集合 获取员工信息
​        如果是经理 显示奖金
​            向下转型(类似强制类型转换)
定义方法    发工资
​    如果没有信息输出
​    遍历集合 获取员工信息
​        如果的经理
定义方法    随机抽取幸运员工
定义方法    对幸运员工进行调薪
get and set

text    测试类

代码

员工

public class YuanGong {
    private String id;
    private String name;
    private double cunKuan;
    private double gongZi;

    public YuanGong() {
    }

    public YuanGong(String id, String name, double cunKuan, double gongZi) {
        this.id = id;
        this.name = name;
        this.cunKuan = cunKuan;
        this.gongZi = gongZi;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getCunKuan() {
        return cunKuan;
    }

    public void setCunKuan(double cunKuan) {
        this.cunKuan = cunKuan;
    }

    public double getGongZi() {
        return gongZi;
    }

    public void setGongZi(double gongZi) {
        this.gongZi = gongZi;
    }
}

厨师

public class ChuShi extends YuanGong{

    public ChuShi() {
    }

    public ChuShi(String id, String name, double cunKuan, double gongZi) {
        super(id, name, cunKuan, gongZi);
    }
}

服务员

public class FuWuYuan extends YuanGong{

    public FuWuYuan() {
    }

    public FuWuYuan(String id, String name, double cunKuan, double gongZi) {
        super(id, name, cunKuan, gongZi);
    }
}

经理

public class JingLi extends YuanGong{
    private double jiangJin;

    public JingLi() {
    }

    public JingLi(String id, String name, double cunKuan, double gongZi, double jiangJin) {
        super(id, name, cunKuan, gongZi);
        this.jiangJin = jiangJin;
    }


    public double getJiangJin() {
        return jiangJin;
    }

    public void setJiangJin(double jiangJin) {
        this.jiangJin = jiangJin;
    }
}

公司

public class GongSi {
    //总资产
    private double zongZiChan;
    //所有员工
    private ArrayList<YuanGong> list;

    public GongSi() {
    }

    public GongSi(double zongZiChan, ArrayList<YuanGong> list) {
        this.zongZiChan = zongZiChan;
        this.list = list;
    }

    //展示公司信息的方法
    public void show(){
        System.out.println("总资产:"+zongZiChan);
        if(list == null || list.size() == 0){
            System.out.println("没有员工!");
            return;
        }

        //展示员工信息
        for (int i = 0; i < list.size(); i++) {
            YuanGong yg = list.get(i);
            System.out.println("工号:"+yg.getId());
            System.out.println("姓名:"+yg.getName());
            System.out.println("存款:"+yg.getCunKuan());
            System.out.println("工资:"+yg.getGongZi());

            if(yg instanceof JingLi){
                JingLi jl = (JingLi) yg;
                System.out.println("奖金:"+jl.getJiangJin());
            }
            System.out.println("--------------------------------");

        }

    }

    //定义一个发工资的方法
    /*
         总资产 = 总资产 - 每个员工的工资
         员工的存款 = 原来的存款+ 工资

         如果是经理
             总资产 = 总资产 - 奖金
             存款 = 原来的存储 + 奖金
     */
    public void faGongZi(){
        if(list == null || list.size() == 0){
            System.out.println("没有员工!不发工资!");
            return;
        }

        //遍历集合 依次获取到每个员工信息
        for (int i = 0; i < list.size(); i++) {
            YuanGong yg = list.get(i);
            //总资产 = 总资产 - 每个员工的工资
            zongZiChan -= yg.getGongZi();
            // 员工的存款 = 原来的存款+ 工资
            yg.setCunKuan(yg.getCunKuan()+yg.getGongZi());

            // 如果是经理
            if(yg instanceof  JingLi){
                JingLi jl = (JingLi) yg;
                //总资产 = 总资产 - 奖金
                zongZiChan -= jl.getJiangJin();
                // 存款 = 原来的存储 + 奖金
                jl.setCunKuan(jl.getCunKuan()+jl.getJiangJin());

            }
        }

    }

    //抽取幸运员工的方法
    public YuanGong getLucky(){

        //在集合的索引范围之内生成一个随机索引
        Random r = new Random();
        int index = r.nextInt(list.size());//[0,3)

        YuanGong yg = list.get(index);

        return yg;
    }

    //定义调薪的方法
    public void  tiaoXin(YuanGong yg,double money){
        yg.setGongZi(yg.getGongZi()+money);
    }

    public double getZongZiChan() {
        return zongZiChan;
    }

    public void setZongZiChan(double zongZiChan) {
        this.zongZiChan = zongZiChan;
    }

    public ArrayList<YuanGong> getList() {
        return list;
    }

    public void setList(ArrayList<YuanGong> list) {
        this.list = list;
    }
}

测试

    public static void main(String[] args) {        
        GongSi gs = new GongSi();
        //设置公司的总资产
        gs.setZongZiChan(1000000.0);
        //设置公司的所有员工
        ArrayList<YuanGong> list = new ArrayList<>();
        list.add(new JingLi("001","柳岩",10000.0,20000.0,5000.0));
        list.add(new ChuShi("002","唐嫣",20000.0,15000));
        list.add(new FuWuYuan("003","金莲",0,250));
        gs.setList(list);

        gs.show();

        gs.faGongZi();

        System.out.println("--");
        gs.show();
        System.out.println("--");

        YuanGong lucky = gs.getLucky();
        System.out.println(lucky.getId()+" "+lucky.getName());

        //调用调薪的方法
        gs.tiaoXin(lucky,-200);

        System.out.println(lucky.getGongZi());

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值