多层If语句 和 表格驱动 的对比
网文提到表格驱动,总喜欢拿一层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语句 和 表格驱动 的对比的更多相关文章
- Linux与Windows的设备驱动模型对比
Linux与Windows的设备驱动模型对比 名词缩写: API 应用程序接口(Application Program Interface ) ABI 应用系统二进制接口(Application Bi ...
- JS010. 三元运算符扩展运用(多层判断语句 / 多条表达式)
MDN - 三元运算符 语法 Condition ? exprIfTrue : exprIfFalse 用例: function getFee(isMember) { return(isMember ...
- 任务驱动,对比式学习.NET开发系列之开篇------开源2个小框架(一个Winform框架,一个Web框架)
一 源码位置 1. Winform框架 2. web框架 二 高效学习编程的办法 1 任务驱动方式学习软件开发 大部分人学习软件开发技术是通过看书,看视频,听老师上课的方式.这些方式有一个共同点即按知 ...
- 4 - SQL Server 2008 之 使用SQL语句删除表格
使用删除表格的SQL命令与删除数据的命令一样,只是删除的是表格这个对象, 语法如下:DROP TABLE 表名 一般在删除表格之前,需判断这个表格存不存在,存在则删除,不存在则不进行执行任何代码. 代 ...
- golang的表格驱动测试
一.leetcode的算法题 package main import ( "fmt" "strings" ) func lengthOfNonRepeating ...
- [转帖]Docker五种存储驱动原理及应用场景和性能测试对比
Docker五种存储驱动原理及应用场景和性能测试对比 来源:http://dockone.io/article/1513 作者: 陈爱珍 布道师@七牛云 Docker最开始采用AUFS作为文件系统 ...
- python init 方法 与 sql语句当前时间对比
def init(self,cr): tools.sql.drop_view_if_exists(cr, 'custrom_product_infomation_report') cr.execute ...
- Docker存储驱动之OverlayFS简介
简介 OverlayFS是一种和AUFS很类似的文件系统,与AUFS相比,OverlayFS有以下特性: 1) 更简单地设计: 2) 从3.18开始,就进入了Linux内核主线: 3) 可能更快一些. ...
- python中if语句和循环语句
//2019.12.05 python循环控制结构(一)控制循环结构 1-1 程序的判断语句与组合1.对于python的控制结构主要有以下三大类:(1)分支结构(2)循环结构(3)异常处理 2.pyt ...
随机推荐
- Android自动化测试探索(一)adb详细介绍
adb详细介绍 #1. 基本简介 adb,即Android Debug Bridge,它是Android开发/测试人员不可替代的强大工具 #2. Mac上安装adb 安装brew /usr/bin/r ...
- Flink原理(五)——容错机制
本文是博主阅读Flink官方文档以及<Flink基础教程>后结合自己理解所写,若有表达有误的地方欢迎大伙留言指出. 1. 前言 流式计算分为有状态和无状态两种情况,所谓状态就是计算过程中 ...
- 用js刷剑指offer(数组中的逆序对)
题目描述 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P ...
- 用js刷剑指offer(把数组排成最小的数)
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 思路 对ve ...
- xss获取cookie源码附利用代码
保存为cookie.asp <% testfile=Server.MapPath("cookies.txt") msg=Request("msg") se ...
- 移动端开发-viewport与媒体查询
首先要知道,在移动开发中,手机的浏览器会默认网页是为宽屏而设计的,它会缩小整个页面来适应屏幕. 1. 不使用viewport出现的问题 提到响应式设计,大家首先想到的可能是 Bootstrap , @ ...
- C++ 类的静态成员
https://www.cnblogs.com/lvxiaoning/p/7598062.html
- b、B、KB、MB、GB 的关系?
1. 8bit (位) = 1Byte (字节) 2.1024Byte (字节 ) = 1KB 3.1024KB = 1MB 4.1024MB = 1GB 5.1024GB = 1TB
- 模拟赛20181101 雅礼 Wearry 施工 蔬菜 联盟
% Day2 Solution % Wearry % Stay determined! 施工 记 fif_{i}fi 表示考虑前 iii 个建筑, 并且第 iii 个建筑的高度不变的答案, 每 ...
- BlockingCollection<T> 类实现 列队操作
官方文档 为实现 IProducerConsumerCollection<T> 的线程安全集合提供阻塞和限制功能. 通过 BlockingCollection<T> 实现列队调 ...