# Table Driven（表驱动）方法实例

```/**
* 驾驶人年龄段
* 保险行业，会对驾驶人的年龄做年龄段的区分判断
* 驾驶人年龄段：01-[18,25);02-[25,30);03-[30-35);04-[35,40);05-[40,45);06-[45,50);07-[50-55);08-[55,+∞)
*/
public class AgePeriodTest {

//if...else...方法
/*
public static String getAgePeriod(int age) {

String agePeriod = "";
if (age > 55) {
agePeriod = "08";
} else if (age >= 50) {
agePeriod = "07";
} else if (age >= 45) {
agePeriod = "06";
} else if (age >= 40) {
agePeriod = "05";
} else if (age >= 35) {
agePeriod = "04";
} else if (age >= 30) {
agePeriod = "03";
} else if (age >= 25) {
agePeriod = "02";
} else if (age >= 18) {
agePeriod = "01";
}
return agePeriod;
}
*/

//Table Driven（表驱动）方法
enum AgePeriod {
A(55, "08"),
B(50, "07"),
C(45, "06"),
D(40, "05"),
E(35, "04"),
F(30, "03"),
G(25, "02"),
H(18, "01");

private int age;
private String agePeriod;

AgePeriod(int age, String agePeriod) {
this.age= age;
this.agePeriod = agePeriod;
}
}

public static String getAgePeriod(int age) {
for (AgePeriod agePeriod : AgePeriod.values()) {
if (age >= agePeriod.age) {
return agePeriod.agePeriod;
}
}
return "";
}

public static void main(String[] args) {
String res = getAgePeriod(1);
System.out.println("res:-" + res);
res = getAgePeriod(18);
System.out.println("res:01-" + res);
res = getAgePeriod(24);
System.out.println("res:01-" + res);
res = getAgePeriod(25);
System.out.println("res:02-" + res);
res = getAgePeriod(29);
System.out.println("res:02-" + res);
res = getAgePeriod(30);
System.out.println("res:03-" + res);
res = getAgePeriod(34);
System.out.println("res:03-" + res);
res = getAgePeriod(35);
System.out.println("res:04-" + res);
res = getAgePeriod(39);
System.out.println("res:04-" + res);
res = getAgePeriod(40);
System.out.println("res:05-" + res);
res = getAgePeriod(44);
System.out.println("res:05-" + res);
res = getAgePeriod(45);
System.out.println("res:06-" + res);
res = getAgePeriod(49);
System.out.println("res:06-" + res);
res = getAgePeriod(50);
System.out.println("res:07-" + res);
res = getAgePeriod(54);
System.out.println("res:07-" + res);
res = getAgePeriod(55);
System.out.println("res:08-" + res);
res = getAgePeriod(59);
System.out.println("res:08-" + res);
res = getAgePeriod(60);
System.out.println("res:08-" + res);
res = getAgePeriod(100);
System.out.println("res:08-" + res);
}
}```

```res:-
res:01-01
res:01-01
res:02-02
res:02-02
res:03-03
res:03-03
res:04-04
res:04-04
res:05-05
res:05-05
res:06-06
res:06-06
res:07-07
res:07-07
res:08-07
res:08-08
res:08-08
res:08-08```

```import java.util.HashMap;
import java.util.Map;

public class TableDriven {

//    public static String convertCertificatTypeToClient(String pasType) {
//
//        if (pasType.equalsIgnoreCase("01")) {
//            return "1";
//        } else if (pasType.equalsIgnoreCase("02")) {
//            return "2";
//
//        } else if (pasType.equalsIgnoreCase("03")) {
//            return "3";
//
//        } else if (pasType.equalsIgnoreCase("04")) {
//            return "7";
//
//        } else if (pasType.equalsIgnoreCase("05")) {
//            return "6";
//
//        } else if (pasType.equalsIgnoreCase("06")) {
//            return "0";
//        } else {
//            return "0";
//        }
//    }

public static String convertCertificatTypeToClient(String pasType) {

//表驱动法
Map<String, String> stateTable = new HashMap<String, String>();
stateTable.put("01", "1");
stateTable.put("02", "2");
stateTable.put("03", "3");
stateTable.put("04", "7");
stateTable.put("05", "6");
stateTable.put("06", "0");

String res = stateTable.get(pasType);
if(res == null) {
res = "0";
}
return res;
}

public static void main(String[] args) {
String res = convertCertificatTypeToClient("01");
System.out.println("1:" + res);
res = convertCertificatTypeToClient("02");
System.out.println("2:" + res);
res = convertCertificatTypeToClient("03");
System.out.println("3:" + res);
res = convertCertificatTypeToClient("04");
System.out.println("7:" + res);
res = convertCertificatTypeToClient("05");
System.out.println("6:" + res);
res = convertCertificatTypeToClient("06");
System.out.println("0:" + res);
res = convertCertificatTypeToClient("00");
System.out.println("0:" + res);
}
}```

```1:1
2:2
3:3
7:7
6:6
0:0
0:0```

Table Driven（表驱动）方法实例

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊