网文提到表格驱动,总喜欢拿一层if做例子,然而这样未免也太简单.

下文是三层缩进的if和表驱动比较,大家可自行判断优劣.

业务是这样的,某景点分旺季票价和淡季票价,淡季票为旺季的一半,15岁以下孩子再减半,60岁以上老人再三三折,成人中有军官证的再打二五折,有士兵证的打两折.(假定非实际业务,勿对号入座)

代码:

package tabledriven;

/**
 *   门票计价器
 * 传统方式和表格驱动比较
 *
 */
public class TicketCalculater {
    // Price
    private double price=144;

    // discout array
    private int[][][] arr= {
                                {
                                    {2*4,2*5,2*1},
                                    {2*2,2*2,2*2},
                                    {2*3,2*3,2*3},
                                },
                                {
                                    {1*4,1*5,1*1},
                                    {1*2,1*2,1*2},
                                    {1*3,1*3,1*3},
                                },
                           };

    // Traditional
    public double getPriceTraditional(int month,Person person) {
        double discount=1;

        if(6<month && month<11) {
            discount*=1;

            if(person.getAge()<15) {
                discount*=0.5;
            }else if(person.getAge()>60) {
                discount*=0.33333333333333;
            }else {
                if(person.isOfficer()) {
                    discount*=0.25;
                }else if(person.isSoldier()) {
                    discount*=0.20;
                }
            }
        }else {
            discount*=0.5;

            if(person.getAge()<15) {
                discount*=0.5;
            }else if(person.getAge()>60) {
                discount*=0.333333333333333;
            }else {
                if(person.isOfficer()) {
                    discount*=0.25;
                }else if(person.isSoldier()) {
                    discount*=0.20;
                }
            }
        }

        return price*discount;
    }

    // Table driven
    public double getPriceTableDriven(int month,Person person) {
        double discount=1;

        int index1,index2,index3;

        if(6<month && month<11) {
            index1=1;
        }else {
            index1=0;
        }

        if(person.getAge()<15) {
            index2=1;
        }else if(person.getAge()>60) {
            index2=2;
        }else {
            index2=0;
        }

        if(person.isOfficer()) {
            index3=0;
        }else if(person.isSoldier()) {
            index3=1;
        }else {
            index3=2;
        }

        discount=arr[index1][index2][index3];

        return price/discount;
    }

    // Entry point
    public static void main(String[] args) {
        TicketCalculater tc=new TicketCalculater();

        Person p1=new Person(30,false,false);
        System.out.println("Ticket price="+round2DecimalPlaces(tc.getPriceTraditional(2, p1)));
        System.out.println("Ticket price(Table)="+round2DecimalPlaces(tc.getPriceTableDriven(2, p1)));

        Person p2=new Person(14,false,false);
        System.out.println("Ticket price="+round2DecimalPlaces(tc.getPriceTraditional(5, p2)));
        System.out.println("Ticket price(Table)="+round2DecimalPlaces(tc.getPriceTableDriven(5, p2)));

        Person p3=new Person(44,true,false);
        System.out.println("Ticket price="+round2DecimalPlaces(tc.getPriceTraditional(8, p3)));
        System.out.println("Ticket price(Table)="+round2DecimalPlaces(tc.getPriceTableDriven(8, p3)));

        Person p4=new Person(68,false,true);
        System.out.println("Ticket price="+round2DecimalPlaces(tc.getPriceTraditional(11, p4)));
        System.out.println("Ticket price(Table)="+round2DecimalPlaces(tc.getPriceTableDriven(11, p4)));
    }

    // round two decimal places of a double
    public static String round2DecimalPlaces(double d){
        java.text.DecimalFormat df =new java.text.DecimalFormat("#0.00");
        return df.format(d);
    }
}

Person类:

package tabledriven;

public class Person {
    private int age;
    private boolean isOfficer;
    private boolean isSoldier;

    public Person(int age,boolean isOfficer,boolean isSoldier) {
        this.age=age;
        this.isOfficer=isOfficer;
        this.isSoldier=isSoldier;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public boolean isOfficer() {
        return isOfficer;
    }
    public void setOfficer(boolean isOfficer) {
        this.isOfficer = isOfficer;
    }
    public boolean isSoldier() {
        return isSoldier;
    }
    public void setSoldier(boolean isSoldier) {
        this.isSoldier = isSoldier;
    }
}

运行结果:

Ticket price=72.00
Ticket price(Table)=72.00
Ticket price=36.00
Ticket price(Table)=36.00
Ticket price=36.00
Ticket price(Table)=36.00
Ticket price=24.00
Ticket price(Table)=24.00

--END-- 2019-12-11 14:01

多层If语句 和 表格驱动 的对比的更多相关文章

  1. Linux与Windows的设备驱动模型对比

    Linux与Windows的设备驱动模型对比 名词缩写: API 应用程序接口(Application Program Interface ) ABI 应用系统二进制接口(Application Bi ...

  2. JS010. 三元运算符扩展运用(多层判断语句 / 多条表达式)

    MDN - 三元运算符 语法 Condition ? exprIfTrue : exprIfFalse 用例: function getFee(isMember) { return(isMember ...

  3. 任务驱动,对比式学习.NET开发系列之开篇------开源2个小框架(一个Winform框架,一个Web框架)

    一 源码位置 1. Winform框架 2. web框架 二 高效学习编程的办法 1 任务驱动方式学习软件开发 大部分人学习软件开发技术是通过看书,看视频,听老师上课的方式.这些方式有一个共同点即按知 ...

  4. 4 - SQL Server 2008 之 使用SQL语句删除表格

    使用删除表格的SQL命令与删除数据的命令一样,只是删除的是表格这个对象, 语法如下:DROP TABLE 表名 一般在删除表格之前,需判断这个表格存不存在,存在则删除,不存在则不进行执行任何代码. 代 ...

  5. golang的表格驱动测试

    一.leetcode的算法题 package main import ( "fmt" "strings" ) func lengthOfNonRepeating ...

  6. [转帖]Docker五种存储驱动原理及应用场景和性能测试对比

    Docker五种存储驱动原理及应用场景和性能测试对比 来源:http://dockone.io/article/1513 作者: 陈爱珍 布道师@七牛云   Docker最开始采用AUFS作为文件系统 ...

  7. python init 方法 与 sql语句当前时间对比

    def init(self,cr): tools.sql.drop_view_if_exists(cr, 'custrom_product_infomation_report') cr.execute ...

  8. Docker存储驱动之OverlayFS简介

    简介 OverlayFS是一种和AUFS很类似的文件系统,与AUFS相比,OverlayFS有以下特性: 1) 更简单地设计: 2) 从3.18开始,就进入了Linux内核主线: 3) 可能更快一些. ...

  9. python中if语句和循环语句

    //2019.12.05 python循环控制结构(一)控制循环结构 1-1 程序的判断语句与组合1.对于python的控制结构主要有以下三大类:(1)分支结构(2)循环结构(3)异常处理 2.pyt ...

随机推荐

  1. 使用virtualbox安装unbuntu开启共享文件夹时遇到的权限问题

    在安装完虚拟机之后,开启文件夹共享,发现只能用root进行访问,个人帐号无权限: cust@hqjia-desktop:/media$ ll drwxr-xr-x 4 root root 4096 2 ...

  2. @ConfigurationProperties绑定配置信息至Array、List、Map、Bean

    原文:https://blog.csdn.net/justry_deng/article/details/90758250 相关说明:在SpringBoot中,我们可以通过以下几种方式获取并绑定配置文 ...

  3. Immediate Decodability UVA-644(qsort排序 + 模拟)

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  4. 手写热更新阐述tinker实现原理

    关于热更新如今面试也是基本上都会提及到的,我上一家公司用的是tinker,而这里准备研究的也是它的原理,而关于tinker的原理网上也是一大堆文章进行介绍,为了对它有个更加进一步的认识,所以自己动手来 ...

  5. 移动App性能评测与优化-Android内存测试 ,DVM原理

    常见的测试方法包括Monkey/UIAutomator类的常规压力测试,大数据/操作的峰值压力测试,长时间运行的稳定性测试等. 前提: 测试准备:版本是纯净版本,不应该附加多余的log和调试用组件. ...

  6. 「TJOI2018」str

    碱基序列 题目描述 小豆参加了生物实验室.在实验室里,他主要研究蛋白质.他现在研究的蛋白质是由$k$个氨基酸按一定顺序构成的.每一个氨基酸都可能有$a$种碱基序列$s_{i,j}$构成. 现在小豆有一 ...

  7. (java)selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致

    描述:selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致,若一致则切换到该窗口并获取标题 跳出if判断,获取父级标题,并关闭 HTML标签不太明显时,可以用路径表示 ...

  8. 【Python学习】Python3 基本数据类型

    参考学习地址:https://www.runoob.com/python3/python3-data-type.html Python3 基本数据类型 Python 中的变量不需要声明.每个变量在使用 ...

  9. 常用的HTTP状态码,网站开发请求状态必备

    成功的状态码: 200 – 服务器成功返回网页 304 – 未修改 失败的状态码: 404 – 请求的网页不存在 503 – 服务器暂时不可用 500 – 服务器内部错误 下面的不是很常用,记住上面那 ...

  10. iframe使用总结(实战)

    说在前面的话,iframe是可以做很多事情的.例如:a>通过iframe实现跨域;b>使用iframe解决IE6下select遮挡不住的问题c>通过iframe解决Ajax的前进后退 ...