-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSweID.java
144 lines (128 loc) · 3.83 KB
/
SweID.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package jl223vy_assign3;
import java.util.*;
public class SweID {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
//application of method areEqual
System.out.print("Please enter your Swedish identity number in the form YYMMDD-NNNN: ");
String iDN1=sc.nextLine();
System.out.print("Please enter your Swedish identity number in the form YYMMDD-NNNN again: ");
String iDN2=sc.nextLine();
if(!areEqual(iDN1,iDN2)){
System.out.print("Please re-enter your Swedish identity number in the form YYMMDD-NNNN: ");
iDN1=sc.nextLine();
}
else
System.out.println("\n Checking...\n");
//application of methods getFirstPart and getSecondPart
System.out.println("Confirming YYMMDD part of your Swedish identity number: " + getFirstPart(iDN1));
System.out.println("Confirming NNNN part of your Swedish identity number: "+ getSecondPart(iDN1));
//application of method isCorrect
System.out.print("\nPlease enter your year of birth in the form YYYY: ");
int year=sc.nextInt();
System.out.println("\n Confirming...");
if(!isCorrect(iDN1,year)){
System.out.print("\nUnvalid Swedish identity number!");
}
else{
System.out.println("\nYour Swedish identity number has confirmed.");
//application of methods isFemaleNumber and isMaleNumber
int NO9=Character.getNumericValue(iDN1.charAt(9));
if(isFemaleNumber(NO9))
System.out.println("\n Hello Madam!");
if(isMaleNumber(NO9))
System.out.println("\n Hello Sir!");
}
sc.close();
}
public static String getFirstPart(String iDN){
String firstP="";
for(int i=0; i<iDN.length(); i++){
if(iDN.charAt(i)=='-')
break;
firstP=firstP+iDN.charAt(i);
}
return firstP;
}
public static String getSecondPart(String iDN){
String secP="";
for(int i=7; i<iDN.length(); i++){
secP=secP+iDN.charAt(i);
}
return secP;
}
public static boolean isFemaleNumber(int no9){
if(no9%2==0)
return true;
else
return false;
}
public static boolean isMaleNumber(int no9){
if(no9%2!=0)
return true;
else
return false;
}
public static boolean areEqual(String fir, String sec){
for(int i=0; i<fir.length(); i++){
if(fir.charAt(i)!=sec.charAt(i))
return false;
}
return true;
}
public static boolean isCorrect(String iDN, int year){
int month=Integer.parseInt(iDN.substring(2,4));
int day=Integer.parseInt(iDN.substring(4,6));
String iDNW=getFirstPart(iDN)+getSecondPart(iDN); //ID number without '-'
if(month<1 || month>12)
return false;
if(month==2){
if(isLeapYear(year)){
if(day>29 || day<1)
return false;
}
else{
if(day>28 || day<1)
return false;
}
}
else if(month==4 || month==6 || month==9 || month==11){
if(day<1 || day>30)
return false;
}
else{
if(day<1 || day>31)
return false;
}
if(!isValidLD(iDNW)){ //check the last digit
return false;
}
return true;
}
public static boolean isLeapYear(int year){
if(year%4==0 && year%100!=0 || year%400==0) //四年一闰,百年不闰,四百年再闰。 four years one leap, hundred years no leap, four hundred years leap again
return true;
else
return false;
}
public static boolean isValidLD(String iDN){
int[] digit=new int[10];
digit[9]=Character.getNumericValue(iDN.charAt(9));
int sum=0;
for(int i=0; i<iDN.length()-1; i++){
digit[i]=Character.getNumericValue(iDN.charAt(i));
if(i%2==0){
digit[i]=digit[i]*2;
if(digit[i]>=10){
digit[i]=digit[i]/10+digit[i]%10;
}
}
sum=sum+digit[i];
}
int correctLD=10-sum%10;
if(digit[9]==correctLD)
return true;
else
return false;
}
}