当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Table Driven(表驱动)方法实例

发表于: 2015-01-06   作者:bijian1013   来源:转载   浏览:
摘要: 实例一: /** * 驾驶人年龄段 * 保险行业,会对驾驶人的年龄做年龄段的区分判断 * 驾驶人年龄段:01-[18,25);02-[25,30);03-[30-35);04-[35,40);05-[40,45);06-[45,50);07-[50-55);08-[55,+∞) */ public class AgePeriodTest { //if...el
实例一:
/**
 * 驾驶人年龄段
 * 保险行业,会对驾驶人的年龄做年龄段的区分判断
 * 驾驶人年龄段:01-[18,25);02-[25,30);03-[30-35);04-[35,40);05-[40,45);06-[45,50);07-[50-55);08-[55,+∞)
 */
public class AgePeriodTest {

    //if...else...方法
    /*
    public static String getAgePeriod(int age) {
        
        String agePeriod = "";
        if (age > 55) {
            agePeriod = "08";
        } else if (age >= 50) {
            agePeriod = "07";
        } else if (age >= 45) {
            agePeriod = "06";
        } else if (age >= 40) {
            agePeriod = "05";
        } else if (age >= 35) {
            agePeriod = "04";
        } else if (age >= 30) {
            agePeriod = "03";
        } else if (age >= 25) {
            agePeriod = "02";
        } else if (age >= 18) {
            agePeriod = "01";
        }
        return agePeriod;
    }
    */
    
    //Table Driven(表驱动)方法
    enum AgePeriod {
        A(55, "08"),   
        B(50, "07"),   
        C(45, "06"),
        D(40, "05"),   
        E(35, "04"),   
        F(30, "03"),   
        G(25, "02"),   
        H(18, "01");    
        
        private int age;   
        private String agePeriod;     
        
        AgePeriod(int age, String agePeriod) {     
            this.age= age;     
            this.agePeriod = agePeriod;   
        } 
    }
    
    public static String getAgePeriod(int age) {
        for (AgePeriod agePeriod : AgePeriod.values()) {
            if (age >= agePeriod.age) {
                return agePeriod.agePeriod;
            }
        }
        return "";
    }
    
    public static void main(String[] args) {
        String res = getAgePeriod(1);
        System.out.println("res:-" + res);
        res = getAgePeriod(18);
        System.out.println("res:01-" + res);
        res = getAgePeriod(24);
        System.out.println("res:01-" + res);
        res = getAgePeriod(25);
        System.out.println("res:02-" + res);
        res = getAgePeriod(29);
        System.out.println("res:02-" + res);
        res = getAgePeriod(30);
        System.out.println("res:03-" + res);
        res = getAgePeriod(34);
        System.out.println("res:03-" + res);
        res = getAgePeriod(35);
        System.out.println("res:04-" + res);
        res = getAgePeriod(39);
        System.out.println("res:04-" + res);
        res = getAgePeriod(40);
        System.out.println("res:05-" + res);
        res = getAgePeriod(44);
        System.out.println("res:05-" + res);
        res = getAgePeriod(45);
        System.out.println("res:06-" + res);
        res = getAgePeriod(49);
        System.out.println("res:06-" + res);
        res = getAgePeriod(50);
        System.out.println("res:07-" + res);
        res = getAgePeriod(54);
        System.out.println("res:07-" + res);
        res = getAgePeriod(55);
        System.out.println("res:08-" + res);
        res = getAgePeriod(59);
        System.out.println("res:08-" + res);
        res = getAgePeriod(60);
        System.out.println("res:08-" + res);
        res = getAgePeriod(100);
        System.out.println("res:08-" + res);
    }
}
运行结果:
res:-
res:01-01
res:01-01
res:02-02
res:02-02
res:03-03
res:03-03
res:04-04
res:04-04
res:05-05
res:05-05
res:06-06
res:06-06
res:07-07
res:07-07
res:08-07
res:08-08
res:08-08
res:08-08
 
实例二:
import java.util.HashMap;
import java.util.Map;

public class TableDriven {

//    public static String convertCertificatTypeToClient(String pasType) {
//        
//        if (pasType.equalsIgnoreCase("01")) {
//            return "1";
//        } else if (pasType.equalsIgnoreCase("02")) {
//            return "2";
//
//        } else if (pasType.equalsIgnoreCase("03")) {
//            return "3";
//
//        } else if (pasType.equalsIgnoreCase("04")) {
//            return "7";
//
//        } else if (pasType.equalsIgnoreCase("05")) {
//            return "6";
//
//        } else if (pasType.equalsIgnoreCase("06")) {
//            return "0";
//        } else {
//            return "0";
//        }
//    }
    
    public static String convertCertificatTypeToClient(String pasType) {
        
        //表驱动法   
        Map<String, String> stateTable = new HashMap<String, String>();
        stateTable.put("01", "1");
        stateTable.put("02", "2");
        stateTable.put("03", "3");
        stateTable.put("04", "7");
        stateTable.put("05", "6");
        stateTable.put("06", "0");
        
        String res = stateTable.get(pasType);
        if(res == null) {
            res = "0";
        }
        return res;
    }
    
    public static void main(String[] args) {
        String res = convertCertificatTypeToClient("01");
        System.out.println("1:" + res);
        res = convertCertificatTypeToClient("02");
        System.out.println("2:" + res);
        res = convertCertificatTypeToClient("03");
        System.out.println("3:" + res);
        res = convertCertificatTypeToClient("04");
        System.out.println("7:" + res);
        res = convertCertificatTypeToClient("05");
        System.out.println("6:" + res);
        res = convertCertificatTypeToClient("06");
        System.out.println("0:" + res);
        res = convertCertificatTypeToClient("00");
        System.out.println("0:" + res);
    }
}
运行结果:
1:1
2:2
3:3
7:7
6:6
0:0
0:0

Table Driven(表驱动)方法实例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Model Driven Architecture 模型驱动架构 为了实现MDA这一宏大构想,OMG(Object Management Group
Model Driven Architecture 模型驱动架构 为了实现MDA这一宏大构想,OMG(Object Management Group
领域驱动设计(Domain Driven Design)参考架构详解 摘要 本文将介绍领域驱动设计(Domain Driven Desi
Windows 程序的进行系依靠外部发生的事件来驱动。换句话说,程序不断等待(利用一个while 回路),等
原文地址:http://www.uml.org.cn/zjjs/201108312.asp 摘要 本文将介绍领域驱动设计(Domain Driven
摘要:本文将介绍领域驱动设计(Domain Driven Design)的官方参考架构,该架构分成了Interfaces、App
今天客户提了一个很有意思的要求,需要使用Reporting Services构建一个警告功能,比如:只有当客户的
摘要:本 文将介绍领域驱动设计(Domain Driven Design)的官方参考架构,该架构分成了Interfaces、Ap
领域驱动设计(Domain Driven Design)参考架构详解 摘要 本文将介绍领域驱动设计(Domain Driven Desi
领域驱动设计(Domain Driven Design)参考架构详解 摘要 本文将介绍领域驱动设计(Domain Driven Desi
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号