网文提到表格驱动,总喜欢拿一层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