【JAVA】发奖金进阶版(工厂模式)

文章目录

    • 题目
    • 项目层级结构
    • 解答
      • UML
      • 核心代码快照
        • Staff接口
        • StaffFactory接口
        • PrimaryTecFactor类
        • Primary_technology类
        • Techonolgy 抽象类
        • main函数
      • 代码
        • Offer_reward.java
        • Staff.java
        • StaffFactory.java
        • Intermediate_management.java
        • Intermediate_technologicalManagement.java
        • Intermediate_technology.java
        • IntermediateManaFactor.java
        • IntermediateTecFactor.java
        • IntermediateTecManaFactor.java
        • Management.java
        • Primary_management.java
        • Primary_technologicalManagement.java
        • Primary_technology.java
        • PrimaryManaFactor.java
        • PrimaryTecFactor.java
        • PrimaryTecManaFactor.java
        • SuperManaFactor.java
        • SuperTecFactor.java
        • SuperTecManaFactor.java
        • Supreme_management.java
        • Supreme_technologicalManagement
        • Supreme_technology.java
        • Technical_Management.java
        • Technology.java
      • 运行结果
    • 结语

题目

以下是基本所需要满足的条件

某软件公司年末奖金发放现在需要给公司员工发奖金,其中员工属性有以下五种

  • 编号
  • 姓名
  • 员工类别
  • 总工作量 ( x ) (x) (x)
  • 奖金 ( y ) (y) (y)

其中员工类别分为

  • 技术类
  • 管理类
  • 技术管理类

各个类别的奖金颁发如下

  1. 技术类
    y = { x ∗ 30 高级工程师 x ∗ 25 中级工程师 x ∗ 20 初级工程师 y=\begin{cases} &x*30 &高级工程师\\ &x*25 &中级工程师\\ &x*20 &初级工程师 \end{cases} y= x30x25x20高级工程师中级工程师初级工程师
  2. 管理类
    y = { x ∗ 15 高管 ( x − 50 ) ∗ 10 中层管理人员 ( x − 100 ) ∗ 5 普通管理职员 y=\begin{cases} &x*15 &高管\\ &(x-50)*10 &中层管理人员\\ &(x-100)*5 &普通管理职员 \end{cases} y= x15(x50)10(x100)5高管中层管理人员普通管理职员
  3. 技术管理类
    y = { x ∗ 25 + 100 高级技术管理类 x ∗ 25 + 70 中级技术管理类 x ∗ 25 + 50 初级技术管理类 y=\begin{cases} &x*25+100 &高级技术管理类\\ &x*25+70 &中级技术管理类\\ &x*25+50 &初级技术管理类 \end{cases} y= x25+100x25+70x25+50高级技术管理类中级技术管理类初级技术管理类

要求选定一个员工类别,输入工作量,输出奖金并且运用面向对象的思想

所需要满足新的要求如下

  1. 按照国家标准收税
  2. 按照国家标准免税
  3. 从可复用、可扩展、可移植的角度出发,没有标准的答案,尽量符合编码原则和设计模式,越符合越好

项目层级结构


├─.idea
│      .gitignore
│      encodings.xml
│      misc.xml
│      modules.xml
│      uiDesigner.xml
│      workspace.xml
│      
├─out
│  └─production
│      └─HW2
│              IntermediateManaFactor.class
│              IntermediateTecFactor.class
│              IntermediateTecManaFactor.class
│              Intermediate_management.class
│              Intermediate_technologicalManagement.class
│              Intermediate_technology.class
│              Management.class
│              Offer_reward.class
│              PrimaryManaFactor.class
│              PrimaryTecFactor.class
│              PrimaryTecManaFactor.class
│              Primary_management.class
│              Primary_technologicalManagement.class
│              Primary_technology.class
│              Staff.class
│              StaffFactory.class
│              SuperManaFactor.class
│              SuperTecFactor.class
│              SuperTecManaFactor.class
│              Supreme_management.class
│              Supreme_technologicalManagement.class
│              Supreme_technology.class
│              Technical_Management.class
│              Technology.class
│              
└─src
        IntermediateManaFactor.java
        IntermediateTecFactor.java
        IntermediateTecManaFactor.java
        Intermediate_management.java
        Intermediate_technologicalManagement.java
        Intermediate_technology.java
        Management.java
        Offer_reward.java
        PrimaryManaFactor.java
        PrimaryTecFactor.java
        PrimaryTecManaFactor.java
        Primary_management.java
        Primary_technologicalManagement.java
        Primary_technology.java
        Staff.java
        StaffFactory.java
        SuperManaFactor.java
        SuperTecFactor.java
        SuperTecManaFactor.java
        Supreme_management.java
        Supreme_technologicalManagement.java
        Supreme_technology.java
        Technical_Management.java
        Technology.java
        


解答

该项目采用的模式为工厂模式

UML

【JAVA】发奖金进阶版(工厂模式)_第1张图片


核心代码快照

由于类过多所以只展示了部分核心的代码快照

Staff接口

【JAVA】发奖金进阶版(工厂模式)_第2张图片

StaffFactory接口

【JAVA】发奖金进阶版(工厂模式)_第3张图片

PrimaryTecFactor类

【JAVA】发奖金进阶版(工厂模式)_第4张图片

Primary_technology类

【JAVA】发奖金进阶版(工厂模式)_第5张图片

Techonolgy 抽象类

【JAVA】发奖金进阶版(工厂模式)_第6张图片

main函数

【JAVA】发奖金进阶版(工厂模式)_第7张图片


代码

Offer_reward.java

import java.util.Scanner;

public class Offer_reward {
    public static void main(String[] args) {
        int StaffType; //员工的类型
        System.out.println("请选择员工的类别:1.初级技术工程师;2.中级技术工程师;3.高级技术工程师;4.普通管理人员;5.中层管理人员;6.高管;7.初级技术管理人员;8.中级技术管理人员;9.高级技术管理人员");
        Scanner scanner = new Scanner(System.in);
        StaffType = scanner.nextInt();
        StaffFactory factory = null;
        switch (StaffType){
            case 1:
                factory = new PrimaryTecFactor();
                break;
            case 2:
                factory = new IntermediateTecFactor();
                break;
            case 3:
                factory = new SuperTecFactor();
                break;
            case 4:
                factory = new PrimaryManaFactor();
                break;
            case 5:
                factory = new IntermediateManaFactor();
                break;
            case 6:
                factory = new SuperManaFactor();
                break;
            case 7:
                factory = new PrimaryTecManaFactor();
                break;
            case 8:
                factory = new IntermediateTecManaFactor();
                break;
            case 9:
                factory = new SuperTecManaFactor();
                break;
        }
        int workTime; //员工的工作时间
        System.out.println("请输入员工的工作时间");
        Scanner scanner1 = new Scanner(System.in);
        workTime = scanner1.nextInt();
        factory.setWorkTime(workTime);
        boolean taxes; //是否收税
        System.out.println("是否收税呢?");
        Scanner scanner2 = new Scanner(System.in);
        taxes = scanner2.nextBoolean();
        int reward; //工资
        Staff staff = factory.createStaff();
        reward = staff.getReward(factory.getWorkTime(),taxes);
        System.out.println("该员工的工资为" + reward);
    }
}

Staff.java

public  interface Staff {
    int getReward(int workTime, boolean taxes);
    int getReward(int workTime, boolean taxes, int subsidy);
    int getSubsidy(int workTime);
}

StaffFactory.java

public interface StaffFactory {
    Staff createStaff();
    void setWorkTime(int workTime);
    int getWorkTime();
}

Intermediate_management.java

public class Intermediate_management extends Management{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = (workTime - 50) * 10;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
}

Intermediate_technologicalManagement.java

public class Intermediate_technologicalManagement extends Technical_Management{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = workTime * 25 + 70;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
}

Intermediate_technology.java

public class Intermediate_technology extends Technology{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = workTime*25;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }

    @Override
    public int getReward(int workTime, boolean taxes, int subsidy) {
        int salary = workTime*25;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97 + subsidy);
        }
        else {
            return salary + subsidy;
        }
    }
}

IntermediateManaFactor.java

public class IntermediateManaFactor implements StaffFactory{
    private int workTime;
    @Override
    public Staff createStaff() {
        return new Intermediate_management();
    }
    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

IntermediateTecFactor.java

public class IntermediateTecFactor implements StaffFactory{
    int workTime;
    @Override
    public Staff createStaff() {
        return new Intermediate_technology();
    }

    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

IntermediateTecManaFactor.java

public class IntermediateTecManaFactor implements StaffFactory{
    private int workTime;
    @Override
    public Staff createStaff() {
        return new Intermediate_technologicalManagement();
    }
    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

Management.java

abstract class Management implements Staff{
    @Override
    public abstract int getReward(int workTime, boolean taxes);
    public int getReward(int workTime, boolean taxes, int subsidy){
        return 0;
    };
    @Override
    public int getSubsidy(int workTime) {
        return 0;
    }
}

Primary_management.java

public class Primary_management extends Management{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = (workTime - 100) * 5;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
}

Primary_technologicalManagement.java

public class Primary_technologicalManagement extends Technical_Management{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = workTime*25 + 50;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
}

Primary_technology.java

public class Primary_technology extends Technology{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = workTime * 20;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
    @Override
    public int getSubsidy(int workTime) {
        return 1500;
    }
    @Override
    public int getReward(int workTime, boolean taxes, int subsidy) {
        int salary = workTime*25;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97 + subsidy);
        }
        else {
            return salary + subsidy;
        }
    }
}

PrimaryManaFactor.java

public class PrimaryManaFactor implements StaffFactory{
    private int workTime;
    @Override
    public Staff createStaff() {
        return new Primary_management();
    }
    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

PrimaryTecFactor.java

public class PrimaryTecFactor implements StaffFactory{
    private int workTime;
    @Override
    public Staff createStaff() {
        return new Primary_technology();
    }
    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

PrimaryTecManaFactor.java

public class PrimaryTecManaFactor implements StaffFactory{
    private int workTime;
    @Override
    public Staff createStaff() {
        return new Primary_technologicalManagement();
    }
    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

SuperManaFactor.java

public class SuperManaFactor implements StaffFactory{
    private int workTime;
    @Override
    public Staff createStaff() {
        return new Supreme_management();
    }
    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

SuperTecFactor.java

public class SuperTecFactor implements StaffFactory{
    int workTime;
    @Override
    public Staff createStaff() {
        return new Supreme_technology();
    }

    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

SuperTecManaFactor.java

public class SuperTecManaFactor implements StaffFactory{
    private int workTime;
    @Override
    public Staff createStaff() {
        return new Supreme_technologicalManagement();
    }
    @Override
    public void setWorkTime(int workTime) {
        this.workTime = workTime;
    }

    @Override
    public int getWorkTime() {
        return workTime;
    }
}

Supreme_management.java

public class Supreme_management extends Management{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = workTime * 15;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
}

Supreme_technologicalManagement

public class Supreme_technologicalManagement extends Technical_Management{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = workTime * 25 + 100;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
}

Supreme_technology.java

public class Supreme_technology extends Technology{
    @Override
    public int getReward(int workTime, boolean taxes) {
        int salary = workTime*30;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97);
        }
        else {
            return salary;
        }
    }
    @Override
    public int getReward(int workTime, boolean taxes, int subsidy) {
        int salary = workTime*25;
        if (taxes && salary >= 5000) {
            return (int) (salary * 0.97 + subsidy);
        }
        else {
            return salary + subsidy;
        }
    }
}

Technical_Management.java

abstract class Technical_Management implements Staff{
    public abstract int getReward(int workTime, boolean taxes);

    @Override
    public int getReward(int workTime, boolean taxes, int subsidy) {
        return 0;
    }
    @Override
    public int getSubsidy(int workTime) {
        return 0;
    }
}

Technology.java

abstract class Technology implements Staff{
    public abstract int getReward(int workTime, boolean taxes);

    @Override
    public abstract int getReward(int workTime, boolean taxes, int subsidy);
    @Override
    public int getSubsidy(int workTime) {
        return 1000;
    }
}


运行结果

【JAVA】发奖金进阶版(工厂模式)_第8张图片


结语

如果有疑问欢迎大家留言讨论,你如果觉得这篇文章对你有帮助可以给我一个免费的赞吗?我们之间的交流是我最大的动力!

你可能感兴趣的:(JAVA,java,开发语言)