写的~! 留下脚印
配置文件config.propertise
主要的类方法
用于测试的方法
配置文件config.propertise
unicom=133|132|131|134
chinamobile=135|136|137|138|139|159
reg_templete=^0?(_phone_)[0-9]{8}
主要的类方法
package demo.phone;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Pattern;
public class Phone {
private String unicom;
private String chinamobile;
private String reg_templete;
private String runicom;
private String rchinamobile;
public Phone() {
//读取配置文件
Properties prop = new Properties();
try {
prop.load(new FileInputStream("D://workspace//gsj//phone//src//demo//phone//config.properties"));
//联通的号段
unicom=prop.getProperty("unicom");
//移动的号段
chinamobile=prop.getProperty("chinamobile");
//表达式模板
reg_templete=prop.getProperty("reg_templete");
//联通的表达式
runicom=reg_templete.replaceAll("_phone_",unicom);
//移动的表达式
rchinamobile=reg_templete.replaceAll("_phone_",chinamobile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public int validate(String phone){
if(validateUnicom(phone))
return 1;//联通的号码
if(validateChinamobile(phone))
return 2;//移动的号码
return 0;//小灵通号码
}
//判断是否是联通的号码
private boolean validateUnicom(String phone){
return Pattern.compile(runicom).matcher(phone).matches();
}
//判断是否是移动的号码
private boolean validateChinamobile(String phone){
return Pattern.compile(rchinamobile).matcher(phone).matches();
}
}
用于测试的方法
package demo.phone;
public class Main {
public static void main(String[] args) {
//如果是1表示联通,2移动,3小灵通
Say("013578481235");
Say("13678481235");
Say("13378481235");
Say("13178481235");
Say("015978481235");
Say("99690814");
Say("89892015");
}
public static void Say(String phone){
Phone p = new Phone();
System.out.println(phone+" "+p.validate(phone));
}
}