职责链模式javac++

软件设计                  石家庄铁道大学信息学院

 

实验15:职责链模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 

1、理解职责链模式的动机,掌握该模式的结构;

2、能够利用职责链模式解决实际问题。

 

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

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

实验要求:

1. 提交类图;

 

 

 

2. 提交源代码;

 

 

 

 

3. 注意编程规范。

 

 

 

package test15;

public class Client {
    public static void main(String[] args) {
        Leader zhuren,bumen,fuzong,zong;
        Order danzi=new Order();
        zong=new Zong("总经理");
        fuzong=new Fuzong("副总经理",zong);
        bumen=new Bumen("部门经理",fuzong);
        zhuren=new Zhuren("主任",bumen);
        danzi.setMoney(5000);
        zhuren.approvel(danzi);
        System.out.println("--------—----------------------------");
        danzi.setMoney(50000);
        zhuren.approvel(danzi);
        System.out.println("--------—----------------------------");
        danzi.setMoney(1000000);
        zhuren.approvel(danzi);

    }
}
package test15;

public class Zong extends Leader{

    public Zong(String a) {
        super(a);
    }
    public Zong(String a,Leader b){
        super(a,b);
    }
    public void approvel(Order dan) {
        if(dan.getMoney()<=200000){
            System.out.println(name+"同意审批这个"+dan.getMoney()+"元的采购单");
        }else {
            System.out.println(name+"不能审批20万元以上的采购单");
            System.out.println("这个"+dan.getMoney()+"元的采购单"+"需要职工大会确定");
        }
    }

}
package test15;

public class Fuzong extends Leader{

    public Fuzong(String a) {
        super(a);
    }
    public Fuzong(String a,Leader b){
        super(a,b);
    }
    public void approvel(Order dan) {
        if(dan.getMoney()<=100000){
            System.out.println(name+"同意审批这个"+dan.getMoney()+"元的采购单");
        }else {
            System.out.println(name+"不能审批10万元以上的采购单");
            this.successor.approvel(dan);
        }
    }

}
package test15;

public class Zhuren extends Leader{

    public Zhuren(String a) {
        super(a);
    }
    public Zhuren(String a,Leader b){
        super(a,b);
    }
    public void approvel(Order dan) {
        if(dan.getMoney()<=10000){
            System.out.println(name+"同意审批这个"+dan.getMoney()+"元的采购单");
        }else {
            System.out.println(name+"不能审批1万元以上的采购单");
            this.successor.approvel(dan);
        }
    }

}
package test15;

public class Bumen extends Leader{

    public Bumen(String a) {
        super(a);
    }
    public Bumen(String a,Leader b){
        super(a,b);
    }
    public void approvel(Order dan) {
        if(dan.getMoney()<=50000){
            System.out.println(name+"同意审批这个"+dan.getMoney()+"元的采购单");
        }else {
            System.out.println(name+"不能审批5万元以上的采购单");
            this.successor.approvel(dan);
        }
    }

}
package test15;

public abstract class Leader {
    protected String name;
    protected Leader successor;
    public Leader(String a){name=a;}
    public void setSuccessor(Leader a){
        successor=a;
    }
    public Leader(String a,Leader b){
        name=a;
        successor=b;
    }

    public abstract void approvel(Order dan);
}
package test15;

public class Order {
    private double money;
    public double getMoney(){
        return money;
    }
    public void setMoney(double a){
        money=a;
    }
}
#include<iostream>
using namespace std;
#include<string>
//抽象管理者
class Manager
{
protected:
    Manager *m_manager;
    string m_name;
public:
    Manager(Manager *manager, string name) :m_manager(manager), m_name(name){}
    virtual void DealWithRequest(string name, int num)  {}
};
//主任
class Zhuren : public Manager
{
public:
    Zhuren(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 10000)
        {
            cout << "主任" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "主任" << m_name << "无法处理,交由部门经理处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
//部门经理
class Bumen : public Manager
{
public:
    Bumen(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 50000)
        {
            cout << "部门经理" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "部门经理" << m_name << "无法处理,交由副总经理处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
//副总经理
class  Fuzong : public Manager
{
public:
    Fuzong(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 100000)
        {
            cout << "副总经理" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "副总经理" << m_name << "无法处理,交由总经理处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
//总经理
class  Zong : public Manager
{
public:
    Zong(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 200000)
        {
            cout << "总经理" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "总经理" << m_name << "无法处理,召开职工大会处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
class Congress : public Manager
{
public:
    Congress(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        cout << "职工大会" << m_name << "审批采购单:" << name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
    }
};
//主函数
int main()
{
    //设置上级
    Manager *congress = new Congress(NULL, "");
    Manager *zong = new Zong(congress, "赵六");
    Manager *fuzong = new Fuzong(zong, "王五");
    Manager *bumen = new Bumen(fuzong, "李四");
    Manager *zhuren = new Zhuren(bumen, "张三");
    zhuren->DealWithRequest("001", 5000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("002", 10000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("003", 60000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("004", 110000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("005", 300000);
    cout<<"-----------------------------------------"<<endl;
    delete zhuren;
    delete bumen;
    delete fuzong;
    delete zong;
    delete congress;
    system("pause");
    return 0;
}

 

 

posted @ 2022-12-23 16:57  zrswheart  阅读(70)  评论(0编辑  收藏  举报