多层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 ...
随机推荐
- linux删除同目录及子目录下统一扩展名的文件
find . -name '*.csv' -type f -print -exec rm -rf {} \; 利用find去查找文件,点是指当前目录下,引号中是相应的数据的名称,自己可以定义,然后用t ...
- 自动网页截图并指定元素位置裁剪图片并保存到excel表格
# coding=utf-8 import os import time from selenium import webdriver from selenium.webdriver.chrome.o ...
- Spring boot集成Websocket,前端监听心跳实现
第一:引入jar 由于项目是springboot的项目所以我这边简单的应用了springboot自带的socket jar <dependency> <groupId>org. ...
- Windows连接Linux服务器远程开发解决方案
解决方案 vscode+Linux服务器 解决连接问题 vscode商店下载remote-ssh工具,然后进行配置. 这个网上依旧有很多详细的教程了,这里就不过多赘述. 配置免密登录 这一部分是我要重 ...
- 29.LINQ初探
本页内容: 1.LINQ查询语言的组成部分: 2.使用LINQ方法语法: 3.排序查询结果 orderby 字句: 4.聚合运算符: 5.查询复杂对象: 6.投影:在查询中创建新对象: 7.Disti ...
- Django之路——10 django的分页组件
Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views here ...
- 《CoderXiaoban》第八次团队作业:Alpha冲刺 2
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 Coderxiaoban团队 作业学习目标 (1)掌握软件测试基 ...
- 用python爬取全网妹子图片【附源码笔记】
这是晚上没事无聊写的python爬虫小程序,专门爬取妹子图的,养眼用的,嘻嘻!身为程序狗只会这个了! 废话不多说,代码附上,仅供参考学习! """ 功能:爬取妹子图全网妹 ...
- redis 缓存问题,转载:https://www.cnblogs.com/liangsonghua/p/www_liangsonghua_me_22.html
缓存穿透: 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且处于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义 ...
- Mysql 的使用方法
一 .Mysql 优势: 1.程序稳定性 :这样任意一台服务所在的机器崩溃了都不会影响数据和另外的服务. 2.数据一致性 :所有的数据都存储在一起,所有的程序操作的数据都是统一的,就不会出现数据不一致 ...