网文提到表格驱动,总喜欢拿一层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. Linux正则表达式,grep总结,sed用法

    原文: 1.sed   流编辑器,实现对文字的增删改替换查(过滤.取行),能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件 输入到屏幕,可以把只匹配到模式的内容输入到屏幕上.还可以对原文件 ...

  2. WinServer-开关机日志

    开关机日志正常1074, 6006, 13, 12, 6005,41,60081074 记录某用户在某计划下重启6006 日志服务关闭13 OS关闭时间按12 OS启动时间6005 日志服务开启 异常 ...

  3. VBS 自动发消息给对方

    http://www.vbsedit.com/ Dim Name,Msg Name= "我家丫头" Msg = "333" set ws=wscript.cre ...

  4. Python3执行top指令

    import subprocess top_info = subprocess.Popen(["], stdout=subprocess.PIPE) out, err = top_info. ...

  5. samba 问题解决

    问题1:使用cifscreds 命令提示 You already have stashed credentials for bash-4.2$ cifscreds add 172.24.8.11 Yo ...

  6. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Sharding-JDBC(二)2.0.3版本实践

    目录 一.Sharding-JDBC依赖 二.分片策略 1. 标准分片策略 2. 复合分片策略 3. Inline表达式分片策略 4. 通过Hint而非SQL解析的方式分片的策略 5. 不分片的策略 ...

  8. 《你说对就队》第九次团队作业:Beta冲刺与验收准备

    <你说对就队>第九次团队作业:Beta冲刺与验收准备 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 <你说对就队> ...

  9. python_并发编程——管道

    1.管道 from multiprocessing import Pipe conn1,conn2 = Pipe() #返回两个值 conn1.send('wdc') #发送 print(conn2. ...

  10. ubuntu下subversion1.7的安装

    环境: ubuntu11.10 subversion1.7 1.用synaptic安装的svn都是1.6版,根本于现在的主流svn服务器无法通信.需要升级为1.7. 1.1 apt-add-repos ...