公司里面系统中,采购审批子系统的设计:
- 采购金额小于5万,主任审批
- 采购金额大于等于5万,小于10万,经理审批
- 采购金额大于等于10万,小于20万,副总经理审批
- 采购金额大于等于20万,总经理审批
封装采购的基本信息PurchasingInformation.java
/**
* 封装采购的基本信息
*/
public class PurchasingInformation {
//员工姓名
private String empName;
//采购金额
private double money;
//采购理由
private String reason;
public PurchasingInformation(String empName, double money, String reason) {
this.empName = empName;
this.money = money;
this.reason = reason;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
}
抽象类Leader.java
/**
* 抽象类
*/
public abstract class Leader {
protected String name;
//责任链上的后继对象
protected Leader nextLeader;
public Leader(String name) {
this.name = name;
}
public void setNextLeader(Leader nextLeader) {
this.nextLeader = nextLeader;
}
/**
* 处理请求的核心的业务方法
*
* @param information
*/
public abstract void handleRequest(PurchasingInformation information);
}
主任类Director.java
/**
* 主任
*/
public class Director extends Leader{
public Director(String name) {
super(name);
}
@Override
public void handleRequest(PurchasingInformation information) {
if (information.getMoney() < 50000.00) {
System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
System.out.println("主任:" + this.name + ",审批通过");
} else {
if (this.nextLeader != null) {
this.nextLeader.handleRequest(information);
}
}
}
}
经理类Manager.java
/**
* 经理
*/
public class Manager extends Leader{
public Manager(String name) {
super(name);
}
@Override
public void handleRequest(PurchasingInformation information) {
if (information.getMoney() < 100000.00) {
System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
System.out.println("经理:"+this.name+"审批通过");
}else {
if (this.nextLeader != null) {
this.nextLeader.handleRequest(information);
}
}
}
}
副总经理类ViceGeneraManger.java
/**
* 副总经理
*/
public class ViceGeneraManger extends Leader {
public ViceGeneraManger(String name) {
super(name);
}
@Override
public void handleRequest(PurchasingInformation information) {
if (information.getMoney() < 200000.00) {
System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
System.out.println("副总经理:" + this.name + "审批通过");
} else {
if (this.nextLeader != null) {
this.nextLeader.handleRequest(information);
}
}
}
}
总经理类GeneraManger.java
/**
* 总经理
*/
public class GeneraManger extends Leader{
public GeneraManger(String name) {
super(name);
}
@Override
public void handleRequest(PurchasingInformation information) {
if (information.getMoney() >= 200000.00) {
System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
System.out.println("总经理:"+this.name+"审批通过");
}
}
}
主程序Client.java
public class Client {
public static void main(String[] args) {
Leader director = new Director("张三");
Leader manger = new Manager("李四");
Leader viceGeneraManger = new ViceGeneraManger("王五");
Leader generaManger = new GeneraManger("张三丰");
//组织关系
director.setNextLeader(manger);
manger.setNextLeader(viceGeneraManger);
viceGeneraManger.setNextLeader(generaManger);
//开始审批操作
PurchasingInformation information1 = new PurchasingInformation("小明", 20000.00, "需要新设备");
director.handleRequest(information1);
//分割
System.out.println("----------");
PurchasingInformation information2 = new PurchasingInformation("小华", 80000.00, "需要新设备");
director.handleRequest(information2);
System.out.println("----------");
PurchasingInformation information3 = new PurchasingInformation("小峰", 150000.00, "需要新设备");
director.handleRequest(information3);
System.out.println("----------");
PurchasingInformation information4 = new PurchasingInformation("小花", 300000.00, "需要新设备");
director.handleRequest(information4);
}
}