11月6日

[实验任务一]:财务审批

某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。

实验要求:

1. 提交类图;

2. 提交源代码;

3. 注意编程规范。

实验内容:

  1. 类图:

 

 

  1. 源代码:

package org.example;

// 采购请求类
class PurchaseRequest {
    private double amount;
    private String description;

    public PurchaseRequest(double amount, String description) {
        this.amount = amount;
        this.description = description;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public String getDesc() {
        return description;
    }

    public void setDesc(String description) {
        this.description = description;
    }
}

// 审批者抽象类
abstract class Approver {
    protected Approver successor;

    public void setSuccessor(Approver successor) {
        this.successor = successor;
    }

    public Approver getSuccessor() {
        return successor;
    }

    public abstract boolean approve(PurchaseRequest request);
}

// 主任类
class Director extends Approver {
    @Override
    public boolean approve(PurchaseRequest request) {
        if (request.getAmount() <= 10000) {
            System.out.println("主管批准了该请求:" + request.getDesc());
            return true;
        } else if (successor != null) {
            return successor.approve(request);
        }
        return false;
    }
}

// 部门经理类
class DepartmentManager extends Approver {
    @Override
    public boolean approve(PurchaseRequest request) {
        if (request.getAmount() > 10000 && request.getAmount() <= 50000) {
            System.out.println("部门经理批准了该请求:" + request.getDesc());
            return true;
        } else if (successor != null) {
            return successor.approve(request);
        }
        return false;
    }
}

// 副总经理类
class DeputyGeneralManager extends Approver {
    @Override
    public boolean approve(PurchaseRequest request) {
        if (request.getAmount() > 50000 && request.getAmount() <= 100000) {
            System.out.println("副总经理批准了该请求:" + request.getDesc());
            return true;
        } else if (successor != null) {
            return successor.approve(request);
        }
        return false;
    }
}

// 总经理类
class GeneralManager extends Approver {
    @Override
    public boolean approve(PurchaseRequest request) {
        if (request.getAmount() > 100000 && request.getAmount() <= 200000) {
            System.out.println("总经理批准了该请求:" + request.getDesc());
            return true;
        } else if (successor != null) {
            return successor.approve(request);
        }
        return false;
    }
}

// 职工大会类
class StaffMeeting extends Approver {
    @Override
    public boolean approve(PurchaseRequest request) {
        if (request.getAmount() > 200000) {
            System.out.println("员工会议上通过了该请求:" + request.getDesc());
            return true;
        }
        return false;
    }
}

// 客户端测试代码
public class ChainOfResponsibilityDemo {
    public static void main(String[] args) {
    PurchaseRequest request1 = new PurchaseRequest(5000, "营销团队的笔记本电脑(5000元)");
PurchaseRequest request2 = new PurchaseRequest(15000, "新项目设备(15000元)");
PurchaseRequest request3 = new PurchaseRequest(150000, "办公室装修(150000元)");
PurchaseRequest request4 = new PurchaseRequest(210000, "公司并购(210000元)");

        Approver director = new Director();
        Approver departmentManager = new DepartmentManager();
        Approver deputyGeneralManager = new DeputyGeneralManager();
        Approver generalManager = new GeneralManager();
        Approver staffMeeting = new StaffMeeting();

        director.setSuccessor(departmentManager);
        departmentManager.setSuccessor(deputyGeneralManager);
        deputyGeneralManager.setSuccessor(generalManager);
        generalManager.setSuccessor(staffMeeting);

        director.approve(request1);
        director.approve(request2);
        director.approve(request3);

director.approve(request4);


    }
}

 

    1. 运行截图:

       

posted @ 2024-11-06 21:48  序章0  阅读(21)  评论(0编辑  收藏  举报