首页 > 其他分享 >11.14

11.14

时间:2024-11-13 23:19:01浏览次数:1  
标签:name 11.14 request PurchaseRequest handleRequest public successor

实验15:职责链模式

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

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

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

 

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

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

实验要求:

1、类图

 

 

2、源代码

①、Leader.java

package org.example.shiyanshiwu;

public abstract class Leader {
    protected String name;
    protected Leader successor;

    public Leader(String name) {
        this.name = name;
    }

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

    public abstract void handleRequest(PurchaseRequest request);
}

 

②、Director.java

package org.example.shiyanshiwu;

public class Director extends Leader {
    public Director(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 10000) {
            System.out.println("主任" + name + "审批采购单,采购单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(request);
            }
        }
    }
}

 

③、DepartmentManager.java

package org.example.shiyanshiwu;

public class DepartmentManager extends Leader {
    public DepartmentManager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 50000) {
            System.out.println("部门经理" + name + "审批采购单,采购单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(request);
            }
        }
    }
}

 

④、Manager.java

package org.example.shiyanshiwu;

public class Manager extends Leader {


    public Manager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 100000) {
            System.out.println("副总经理" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(request);
            }
        }
    }
}

 

⑤、GeneralManager.java

package org.example.shiyanshiwu;

public class GeneralManager extends Leader {

    public GeneralManager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 200000) {
            System.out.println("总经理" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
        } else {
            System.out.println("采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元,金额太大,需开职工大会确定是否审批通过");
        }
    }
}

 

⑥、PurchaseRequest.java

package org.example.shiyanshiwu;

public class PurchaseRequest {
    private int money;
    private int id;

    public PurchaseRequest() {
    }

    public PurchaseRequest(int id, int money) {
        this.id = id;
        this.money = money;
    }

    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;
    }
}

 

⑦、Clinet

package org.example.shiyanshiwu;

public class Client {
    public static void main(String[] args) {
        Leader objDirector, objDepartmentManager, objManager, objGeneralManager;

        objDirector = new Director("张三");
        objDepartmentManager = new DepartmentManager("李四");
        objManager = new Manager("王五");
        objGeneralManager = new GeneralManager("赵六");

        objDirector.setSuccessor(objDepartmentManager);
        objDepartmentManager.setSuccessor(objManager);
        objManager.setSuccessor(objGeneralManager);

        PurchaseRequest pr1 = new PurchaseRequest(1001, 10000);
        objDirector.handleRequest(pr1);

        PurchaseRequest pr2 = new PurchaseRequest(1002, 50000);
        objDirector.handleRequest(pr2);

        PurchaseRequest pr3 = new PurchaseRequest(1003, 100000);
        objDirector.handleRequest(pr3);

        PurchaseRequest pr4 = new PurchaseRequest(1004, 200000);
        objDirector.handleRequest(pr4);

        PurchaseRequest pr5 = new PurchaseRequest(1005, 200001);
        objDirector.handleRequest(pr5);
    }
}

 

3、运行结果

 

 

标签:name,11.14,request,PurchaseRequest,handleRequest,public,successor
From: https://www.cnblogs.com/zzqq1314/p/18545050

相关文章

  • C++ //练习 11.14 扩展你在11.2.1节练习(第378页)中编写的孩子姓到名的map,添加一个pair
    C++Primer(第5版)练习11.14练习11.14扩展你在11.2.1节练习(第378页)中编写的孩子姓到名的map,添加一个pair的vector,保存孩子的名和生日。环境:LinuxUbuntu(云服务器)工具:vim 代码块/************************************************************************* >Fil......
  • 大二快乐日记11.14
    基本语法当不再需要索引时,可以使用DROPINDEX语句或ALTERTABLE语句来对索引进行删除。1)使用DROPINDEX语句语法格式:DROPINDEX<索引名>ON<表名>语法说明如下:<索引名>:要删除的索引名。<表名>:指定该索引所在的表名。2)使用ALTERTABLE语句根据ALTERTABLE语句的语......
  • 11.14
    3.编写一个程序,将字符数组s2中的全部字符复制到字符数组s1中。不用strcpy函数。复制时’\0’也要复制过去,’\0’后面的字符不复制。  程序代码:#include<stdio.h> voidcopyString(chars1[],chars2[]){   inti=0;   while(s2[i]!='\0'){     ......
  • 2023.11.14——每日总结
    昨天忘记发了,补上学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.mybatis明日计划:学习......
  • 2023.11.14测试
    \[\text{NOIP模拟赛-2023.11.14}\]T1简单的题给三个数\(n,G,L\),要求从\(1\dotsn\)中选出一个非空子集使\(\gcd=G\),\(\operatorname{lcm}=L\)。问方案数。同时有若干询问,给定\(a_i\),求在包含\(a_i\)的前提下的方案数。\(n,G,L\leq10^8\),\(Q\leq5000\)把\(L\)所有......
  • 2023.11.14
    CLYZ联考。鉴定为FJOI。A问\(\{1,2,\dots,n\}\)有多少子集的\(\gcd=G\),\(\operatorname{lcm}=L\)。另外地,多次询问若子集包含\(x\)的方案数。答案模\(998244853\)。\(1\len,G,L\le10^8\),\(1\leq\le5000\),\(1\lex\len\)。\(\mathrm{TL}=6\mathrm{s}\)。先解决......
  • 11.14打卡
    1.最小覆盖字串(76)给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。classSolution{publicStringminWindow(Strings,Stringt){intn=s.length();......
  • 11.14 衔花
    垫底好心情,从我做起也只能我了昨天改的GNUK改坏了,今天重改妈的模拟赛垫了,现在完全不会计数。好多技巧都忘光光了。幸好很快就要结束了。再见呢朋友们。http://www.ccgp-hebei.gov.cn/sjz/sjz/cggg/zhbggAAAS/202311/t20231114_1910631.htmlS2新购买了三块显卡,一款古......
  • 2023.11.14 总结
    T1题意:已知\(P=10^{18}+31\)为质数且存在原根\(g=42\),记\(A_0\)为\(795484359100928850\),\(A_k=f(A_{k-1})\),其中\(0<f(x)<P\)且满足\(g^{f(x)}\equivx(modP)\),可证明这样\(f(x)\)唯一存在,每次查询一点\(f(x)\)的取值,\(1\lex\le10^5\)。事实上,此......
  • 11.14每日总结
    目中在搜索商品时,在没有搜索按钮的情况下,刚开始是写的当用户输入完成后,input框失去焦点blur事件处理,产品提议用户输入后,按enter回车键返回搜索结果。vue中失去焦点事件写法:@blurvue中enter回车键事件写法:@keyup.enter.native......