公司发工资代码

该程序使用Java编写了一个简单的公司员工管理模型,包括员工、经理、厨师和服务员类,实现了工资发放和奖金处理功能。经理类有额外的奖金属性。公司类包含员工列表,可以展示员工信息、发放工资并抽取幸运员工调整工资。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

员工父类
public class Employee {
    private String id;
    private String name;
    private double cunKuan;
    private double gongZi;

    public Employee() {
    }

    public Employee(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 Manager extends Employee{
    private double jiangjin;

    public Manager() {

    }

    public Manager(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 Cooker extends Employee{
    public Cooker() {

    }

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

服务员

public class Waiter extends Employee{
    public Waiter() {
    }

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

公司

import java.util.ArrayList;
import java.util.Random;

public class Compony {
    private double zongZiChan;
    ArrayList<Employee> list = new ArrayList<>();
    public void show(){
        System.out.println("公司总资产:"+zongZiChan);
        System.out.println("----------------------");
        if(list == null || list.size() == 0){
            System.out.println("没有员工");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            Employee e = list.get(i);
            System.out.println("员工ID:"+e.getId()+" 姓名:"+e.getName()+" 存款:"+e.getCunKuan()+" 工资:"+e.getGongZi());
            if(e instanceof Manager){
                Manager m = (Manager)e;
                System.out.println("奖金:"+m.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++) {
                Employee e = list.get(i);
                zongZiChan -= e.getGongZi();
//                System.out.println("总资产还剩:"+zongZiChan);
                e.setCunKuan(e.getCunKuan() + e.getGongZi());
//                System.out.println(e.getName()+"的存款:"+e.getCunKuan());
                if (e instanceof Manager) {
                    Manager m = (Manager) e;
                    zongZiChan -= m.getJiangjin();
//                    System.out.println("总资产还剩:"+zongZiChan);
                    m.setCunKuan(m.getCunKuan() + m.getJiangjin());
//                    System.out.println(e.getName()+"的存款:"+e.getCunKuan());
                }
        }
    }



    public Employee lucky(){
        Random r = new Random();
        int index = r.nextInt(list.size());
        Employee e = list.get(index);
        return e;
    }

    public void tiaoXin(Employee e,double salary){
        e.setGongZi(e.getGongZi()+salary);
    }



    public Compony() {
    }

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

    public double getZongZiChan() {
        return zongZiChan;
    }

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

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

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

test

   import java.util.ArrayList;
    
    public class Test {
        public static void main(String[] args) {
            Compony c = new Compony();
            c.setZongZiChan(1000000);
    
            ArrayList<Employee> list = new ArrayList<>();
            list.add(new Manager("001","张泽妮",20000,20000,10000));
            list.add(new Cooker("002","陈梦阳",30000,10000));
            list.add(new Waiter("003","程洪瑞",40000,10000));
            c.setList(list);
            c.show();
            System.out.println("------------------------");
            c.faGongZi();
    
            System.out.println("----------------------------");
            Employee luck = c.lucky();
            c.tiaoXin(luck,-5000);
            System.out.println("幸运者ID:"+luck.getId()+"   姓名:"+luck.getName()+"  工资是:"+luck.getGongZi());
    
    
        }
    }

2.自己全部写出来了,无难点

3.开心,充实

4.加油,喝点咖啡别犯困.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值