表驱动法2
2014-05-24 20:47 ttylinux 阅读(262) 评论(0) 收藏 举报继续展示使用表驱动法实现的例子。
例子1:
需求:输入一个月份,和一个年份,输出该月的天数。
public class DaysPerMonth {
private int daysPerMonthLeapYear[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 };
private int daysPerMonthNLeapYear[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 };
private final String ErrorHint = "输入参数不符合规定";
public boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0) {
return true;
}
if (year % 4 == 0 && year % 400 == 0) {
return true;
}
return false;
}
public int getDaysOfPerMonth(int month, int year) throws Exception {
if (!validateParam(month, year)) {
throw new Exception(ErrorHint);
}
if (isLeapYear(year)) {
return daysPerMonthLeapYear[month - 1];
} else {
return daysPerMonthNLeapYear[month - 1];
}
}
private boolean validateParam(int month, int year) {
if (month <= 0 || month > 12 || year <= 0) {
return false;
} else {
return true;
}
}
public static void main(String[] args) {
DaysPerMonth one = new DaysPerMonth();
try {
System.out.println(one.getDaysOfPerMonth(2, 2000));
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用表驱动法和不使用,效果的是明显的。不使用,又将会是一大堆的if(month == 1) 输出31之类的。可以看出,表驱动法,实现上述的需求过程中,也是将逻辑判断简化为查表操作。
例子2:
需求:输入是年龄,性别,婚姻状况,是否吸烟,然后,按照给定的规则,得到相应的费率。
为了简单起见,假设有以下组合:
1.男,已婚,吸烟,年龄。
2.女,已婚,不吸烟,年龄。
3.男,未婚,吸烟,年龄。
其实,按照上述情况,组合数量是2*2*2*X = 8X,这个X不同年龄对应的费率。
这个如果写成if判断的话,那是不可想象的,那会有很多if。
假设,有以下费率规定:
1.男,已婚,吸烟,年龄(age)
| age>=18 | 25% |
| age = 19 | 12% |
| age = 20 | 23% |
| age = 21 | 20% |
| age >=22 | 30% |
2.女,已婚,不吸烟,年龄
| age>=18 | 22% |
| age=19 | 12% |
| age=20 | 2% |
| age=21 | 23% |
| age >=22 | 21% |
一个简单的思路是,上述三个组合中,每个组合对应一张表;确定了是哪张表之后,再使用年龄作为索引,在这张表里面查询对应的值。
但是,这里有个问题,从输入的因素:性别,婚姻状况,吸烟否,要从这三个输入因素中确定出对应的表,这里要使用多个if逻辑判断。我想尽力避免这种情况,通过一个转换函数,
该函数的功能是根据这三个输入因素,确定出类型,然后根据该类型找对应的表,这里面再次用到了表驱动方法。
实现:
public class RateTable {
public static class Gender {
public static String Feamle = "0";
public static String Male = "1";
}
public static class MaritalStatus {
public static String Single = "0";
public static String Married = "1";
}
public static class SmokingStatus {
public static String Smoking = "0";
public static String NonSomking = "1";
}
private HashMap<Integer, Float> typeOne = new HashMap<Integer, Float>();
private HashMap<Integer, Float> typeTwo = new HashMap<Integer, Float>();
private HashMap<Integer, Float> typeThree = new HashMap<Integer, Float>();
private HashMap<String, HashMap<Integer, Float>> typeTable = new HashMap<String, HashMap<Integer, Float>>();
public static final int FloorBoundary = 18;
public static final int UpperBoundary = 22;
public RateTable() {
initalRateTable();
initalTypeTable();
}
private void initalRateTable() {
initalTypeOne();
initalTypeTwo();
}
private void initalTypeOne() {
typeOne.put(FloorBoundary, (float) 0.25);
typeOne.put(19, (float) 0.12);
typeOne.put(20, (float) 0.23);
typeOne.put(21, (float) 0.2);
typeOne.put(UpperBoundary, (float) 0.3);
}
private void initalTypeTwo() {
typeTwo.put(FloorBoundary, (float) 0.22);
typeTwo.put(19, (float) 0.12);
typeTwo.put(20, (float) 0.02);
typeTwo.put(21, (float) 0.23);
typeTwo.put(UpperBoundary, (float) 0.21);
}
private void initalTypeTable() {
// 表示的顺序是:性别,婚姻,吸烟
typeTable.put(
(Gender.Male + MaritalStatus.Married + SmokingStatus.Smoking),
typeOne);
typeTable.put(Gender.Feamle + MaritalStatus.Married
+ SmokingStatus.NonSomking, typeTwo);
}
public float getRate(String Gender, String MarriedStatus,
String SmokingStatus, int age) throws Exception {
HashMap<Integer, Float> table = typeTable.get(Gender + MarriedStatus
+ SmokingStatus);
if (table == null) {
throw new Exception("找不到对应的费率表,请重新输入参数");
}
if (age > 0 && age <= 18) {
return table.get(RateTable.FloorBoundary);
}
if (age >= 22) {
return table.get(RateTable.UpperBoundary);
}
return table.get(age);
}
public static void main(String[] args) {
RateTable one = new RateTable();
try {
System.out.println("Male_Married_Smoking_ age 20 , the rate:"
+ one.getRate(Gender.Male, MaritalStatus.Married,
SmokingStatus.Smoking, 20));
System.out.println("Femal_Married_NonSmoking_ age 19, the rate:"
+ one.getRate(Gender.Feamle, MaritalStatus.Married,
SmokingStatus.NonSomking, 19));
} catch (Exception e) {
e.printStackTrace();
}
}
}
-----
上面,两次用到了表驱动方法。一次是,输入的三种信息组合:性别+婚姻状况+吸烟否,将这三个输入项作为一个Key去查表,查找到目标表;然后,得到了目标表之后,再使用
age作为key查询到对应的费率数值。
同样,还是方便扩展:有多少种类型,就添加多少个HashMap<Integer, Float>,每个代表某种具体类型下的费率表;也方便修改,只要修改某个具体费率表下某个年龄对应的费率便可。
同样,还是减少了多次if的逻辑判断。
浙公网安备 33010602011771号