一个校验身份证,更新身份证,获取身份证信息的JAVA程序。很简单,主要是为了完成学校的作业。反正不管怎么说也是自己写的,所以就拿上来自爽一下了 呵呵 以后有时间会把这个程序更新成友好的用户界面的形式。
//////////////////////////////////////////////////////////////
(请将ID.getCheck()改为:
public char getCheck()
{
if(id.charAt(17)=='X')
{
return 'x';
}
else
{
return id.charAt(17);
}
}
)
/////////////////////////////////////////////////////////////
import javax.swing.JOptionPane;
class ID
{
private String id;
public ID(String s)
{
id=s;
}
public String getID()
{
return id;
}
public int getLength()
{
return id.length();
}
public String getProvince()
{
return id.substring(0,2);
}
public String getCity()
{
return id.substring(2,4);
}
public String getCountry()
{
return id.substring(4,6);
}
public String getYear()
{
if(id.length()==15)
{
return id.substring(6,8);
}
if(id.length()==18)
{
return id.substring(6,10);
}
return "error";
}
public String getMonth()
{
if(id.length()==15)
{
return id.substring(8,10);
}
if(id.length()==18)
{
return id.substring(10,12);
}
return "error";
}
public String getDay()
{
if(id.length()==15)
{
return id.substring(10,12);
}
if(id.length()==18)
{
return id.substring(12,14);
}
return "error";
}
private String getOrder()
{
if(id.length()==15)
{
return id.substring(12,15);
}
if(id.length()==18)
{
return id.substring(14,17);
}
return "error";
}
public String getSex()
{
int p=Integer.parseInt(getOrder());
if(p%2==1)
{
return "男性";
}
else
{
return "女性";
}
}
public char getCheck()
{
return id.charAt(17);
}
public char getCheckKey()
{
String idmid;
if(id.length()==15)
{
idmid=id.substring(0,6)+"19"+id.substring(6,15)+' ';
}
else
{
idmid=id;
}
int[] wi=new int[19];
for(int i=2;i<wi.length;i++)
{
int a=(int)Math.pow(2,i-1);
wi[i]=a%11;
}
int sum=0;
for(int i=2;i<wi.length;i++)
{
sum=sum+wi[i]*Integer.parseInt(idmid.substring(18-i,18-i+1));
}
char[] arr=new char[]{'1','0','x','9','8','7','6','5','4','3','2'};
return arr[sum%11];
}
}
class IDCheck
{
private static boolean isfullYear(ID x)
{
int ye=(x.getLength()==18?Integer.parseInt(x.getYear()):Integer.parseInt(x.getYear())+1900);
if((ye%4==0&&ye%100!=0)||ye%400==0)
{
return true;
}
else
{
return false;
}
}
private static String getInformation(ID x)
{
String result="";
if(x.getLength()==15)
{
result="旧身份证。\n";
}
if(x.getLength()==18)
{
result="新身份证。\n";
}
result=result+"身份证号码为:"+x.getID()+"\n省份代码为:"+x.getProvince()+" 城市代码为:"+x.getCity()+" 区县代码为:"+x.getCountry()+"\n出生日期为:"+x.getYear()+"年"+x.getMonth()+"月"+x.getDay()+"日"+"\n性别为:"+x.getSex();
return result;
}
private static String renew(ID x)
{
return x.getID().substring(0,6)+"19"+x.getID().substring(6,15)+x.getCheckKey();
}
private static String check(ID x)
{
for(int i=0;(x.getLength()==18?i<x.getLength()-1:i<x.getLength());i++)
{
if(x.getID().charAt(i)<'0'||x.getID().charAt(i)>'9')
{
return "身份证中含有非法字符!";
}
}
if((x.getLength()==18?Integer.parseInt(x.getYear()):Integer.parseInt(x.getYear())+1900)>1989||(x.getLength()==18?Integer.parseInt(x.getYear()):Integer.parseInt(x.getYear())+1900)<1900)
{
return "出生年信息错误!";
}
if(Integer.parseInt(x.getMonth())>12||Integer.parseInt(x.getMonth())<1)
{
return "出生月信息错误!";
}
if(Integer.parseInt(x.getMonth())==1||Integer.parseInt(x.getMonth())==3||Integer.parseInt(x.getMonth())==5||Integer.parseInt(x.getMonth())==7||Integer.parseInt(x.getMonth())==8||Integer.parseInt(x.getMonth())==10||Integer.parseInt(x.getMonth())==12)
{
if(Integer.parseInt(x.getDay())>31||Integer.parseInt(x.getDay())<1)
{
return "出生日信息错误!";
}
}
else if(Integer.parseInt(x.getMonth())!=2)
{
if(Integer.parseInt(x.getDay())>30||Integer.parseInt(x.getDay())<1)
{
return "出生日信息错误!";
}
}
else
{
if(isfullYear(x))
{
if(Integer.parseInt(x.getDay())>29||Integer.parseInt(x.getDay())<1)
{
return "出生日信息错误!";
}
}
else
{
if(Integer.parseInt(x.getDay())>28||Integer.parseInt(x.getDay())<1)
{
return "出生日信息错误!";
}
}
}
if(x.getLength()==18&&x.getCheck()!=x.getCheckKey())
{
return "校验码出错!";
}
return "right";
}
public static void main(String[] arg)
{
String idnum="";
while(true)
{
idnum=JOptionPane.showInputDialog("请输入身份证号码 (制作:郭渊哲(Giftedbird))");
if(idnum.length()==15||idnum.length()==18)
{
break;
}
JOptionPane.showMessageDialog(null,"您输入的身份证号码的位数不正确!请重新输入!","ERROR!!!",JOptionPane.ERROR_MESSAGE);
}
ID oldid=new ID(idnum);
if(check(oldid).compareTo("right")!=0)
{
String errorInformation="这个身份证是假的!\n虚假信息:"+check(oldid);
JOptionPane.showMessageDialog(null,errorInformation,"RESULT",JOptionPane.ERROR_MESSAGE);
return;
}
ID newid;
JOptionPane.showMessageDialog(null,getInformation(oldid),"RESULT",JOptionPane.INFORMATION_MESSAGE);
if(idnum.length()==15)
{
while(true)
{
String mid=JOptionPane.showInputDialog("这是一个旧身份证,是否升级为18位?(Y/N)");
if(mid.equals("y")||mid.equals("Y"))
{
newid=new ID(renew(oldid));
JOptionPane.showMessageDialog(null,"身份证已经更新!\n"+getInformation(newid),"RESULT",JOptionPane.INFORMATION_MESSAGE);
break;
}
if(mid.equals("n")||mid.equals("N"))
{
break;
}
JOptionPane.showMessageDialog(null,"您输入错误!请重新输入!","ERROR!!!",JOptionPane.ERROR_MESSAGE);
}
}
}
}