相关类图
具体代码
//BuyObject.java
package org.example.test014;
public class BuyObject {
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public BuyObject(int id, int money) {
this.id = id;
this.money = money;
}
private int id;
private int money;
}
//Leader.java
package org.example.test014;
public abstract class Leader {
protected String name;
public Leader getSuccessor() {
return successor;
}
public void setSuccessor(Leader successor) {
this.successor = successor;
}
protected Leader successor;
public Leader(String name) {
this.name = name;
}
public abstract void handleBuy(BuyObject buyObject);//管理采购单的函数方法
}
//Zhuren.java
package org.example.test014;
public class Zhuren extends Leader {
public Zhuren(String name) {
super(name);
}
@Override
public void handleBuy(BuyObject buyObject) {
if (buyObject.getMoney() < 10000) {
System.out.println("主任" + name + "审批采购单号" + buyObject.getId() + ",,共计" + buyObject.getMoney() + "元");
}
else {
if (this.successor != null) {
this.successor.handleBuy(buyObject);
}
}
}
}
//Bumen.java
package org.example.test014;
public class Bumen extends Leader {
public Bumen(String name) {
super(name);
}
@Override
public void handleBuy(BuyObject buyObject) {
if (buyObject.getMoney() >= 10000 && buyObject.getMoney() < 50000) {
System.out.println("部门经理" + name + "审批采购单号" + buyObject.getId() + ",,共计" + buyObject.getMoney() + "元");
}
else {
if (this.successor != null) {
this.successor.handleBuy(buyObject);
}
}
}
}
//Fuzong.java
package org.example.test014;
public class Fuzong extends Leader {
public Fuzong(String name) {
super(name);
}
@Override
public void handleBuy(BuyObject buyObject) {
if (buyObject.getMoney() >= 50000 && buyObject.getMoney() < 100000) {
System.out.println("副总经理" + name + "审批采购单号" + buyObject.getId() + ",,共计" + buyObject.getMoney() + "元");
}
else {
if (this.successor != null) {
this.successor.handleBuy(buyObject);
}
}
}
}
//Zong.java
package org.example.test014;
public class Zong extends Leader {
public Zong(String name) {
super(name);
}
@Override
public void handleBuy(BuyObject buyObject) {
if (buyObject.getMoney() >= 100000 && buyObject.getMoney() < 200000) {
System.out.println("总经理" + name + "审批采购单号" + buyObject.getId() + ",,共计" + buyObject.getMoney() + "元");
}
else {
System.out.println("金额太高,打算开职工大会商量这件事~");
}
}
}
//Client.java
package org.example.test014;
public class Client {
public static void main(String[] args) {
Leader objZhuren, objBumen, objFuzong, objZong;
objZhuren = new Zhuren("王明");
objBumen = new Bumen("赵强");
objFuzong = new Fuzong("李波");
objZong = new Zong("李甜甜");
objZhuren.setSuccessor(objBumen);
objBumen.setSuccessor(objFuzong);
objFuzong.setSuccessor(objZong);
BuyObject b1 = new BuyObject(1, 3000);
objZhuren.handleBuy(b1);
BuyObject b2 = new BuyObject(2, 40000);
objZhuren.handleBuy(b2);
BuyObject b3 = new BuyObject(3, 70000);
objZhuren.handleBuy(b3);
BuyObject b4 = new BuyObject(4, 160000);
objZhuren.handleBuy(b4);
BuyObject b5 = new BuyObject(5, 230000);
objZhuren.handleBuy(b5);
}
}
运行结果