Yushi can be reconstructed: ID card number parsing and verification tools (Third Day)

Source: Internet
Author: User
Document directory
  • = "Verification code"

The progress in the previous two days seems to be a little slow. Today, we have accelerated a little bit, so we will not detail each step.

= Extraction of Area Information

After gender and birthday, the last information block will only list the tests as follows.

= "Validity

This is a big problem. previously, I temporarily removed the verification from different places. the original author of the Code also talked about his views on verification. he is right. This completely verified method is basically the design idea of DDD. However, I want to say that my knowledge is temporarily removed to ensure the unit of testing. The verification function is driven by the verification test. The second consideration is that my verification is intended to be placed in the constructor. That is to say, if there is any wrong input, the first door cannot be entered.

Here, the test and implementation are both simple and look like a lot, just some listing, different error scenarios.

1 [subject ("ID card, validity")] 2 public class when_create_social_id_with_valid_format {3 private because of = () => subject = new socialid ("430103123456780020 "); 4 5 private it should_create_social_properly = 6 () => subject. getcardnumber (). shouldequal ("430103123456780020"); 7 Private Static socialid subject; 8} 9 [subject ("ID card, validity")] 10 public class when_create_social_id_with_null_string {11 private because of = () => exception = catch. exception () => New socialid (null); 12 13 private it should_not_allow_to_create = 14 () => exception. shouldnotbenull (); 15 Private Static socialid subject; 16 Private Static exception; 17} 18 19 [subject ("ID card, validity")] 20 public class when_create_social_id_with_empty_string {21 private because of = () => exception = catch. exception () => New socialid (string. empty); 22 23 private it should_not_allow_to_create = 24 () => exception. shouldnotbenull (); 25 Private Static socialid subject; 26 Private Static exception; 27} 28 29 [subject ("ID card, validity")] 30 public class when_create_social_id_with2_length_string {31 private because of = () => exception = catch. exception () => New socialid ("12"); 32 33 private it should_not_allow_to_create = 34 () => exception. shouldnotbenull (); 35 Private Static socialid subject; 36 Private Static exception; 37} 38 [subject ("ID card, validity")] 39 public class when_create_social_id_with_20_length_string {40 private because of = () => exception = catch. exception () => New socialid ("12345678901234567890"); 41 42 private it should_not_allow_to_create = 43 () => exception. shouldnotbenull (); 44 Private Static socialid subject; 45 Private Static exception; 46} 47 [subject ("ID card, validity")] 48 public class when_create_social_id_alphet_length_string {49 private because of = () => exception = catch. exception () => New socialid ("a23456789012345678"); 50 51 private it should_not_allow_to_create = 52 () => exception. shouldnotbenull (); 53 Private Static socialid subject; 54 private static exception; 55}

Implementation

1 public SocialID(String cardNumber)2         {3             if (string.IsNullOrEmpty(cardNumber))4                 throw new ApplicationException("Card Number is empty");5             if (cardNumber.Length != CARD_NUMBER_LENGTH)6                 throw new ApplicationException("Card Number Length is wrong.");7             if (!SOCIAL_NUMBER_PATTERN.IsMatch(cardNumber))8                 throw new ApplicationException("Card Number has wrong charactor(s).");          9         }

 

= "Verification code"

The verification code is a special validity check, which is complicated. Here, I extract this part of logic code into a validator.

The test is extremely simple, and the implementation is almost intact.

Test:

1 public class when_verify_soical_number:Specification<Verifier>2     {3         Because of = () => { code = subject.verify("43010319791211453"); };4 5         private It verify_code_should_match =6             () => code.ShouldEqual('4');7         private static char code;8     }

Implementation

1 namespace skight. eliteweb. domain. specs. properties 2 {3 Public class verifier 4 {5 Private Static char [] verify_code = 6 {7 '1', '0', 'x', '9', '8 ', '7', 8'6', '5', '4', '3', '2' 9}; 10 11/** 12*18 ID cards, value 13 */14 15 Private Static int [] verify_code_weight = 16 {17 7, 9, 10, 5, 8, 4, 2, 4,219 6, 3, 7, 9, 10, 5, 8,}; 20 private static int card_number_length = 18; 21 22 public char verify (string source) 23 {24/*** <li> check code (18th digits ): <br/> 26 * <ul> 27 * <li> the weighted sum formula S = sum (ai * WI), I = 0... 16. First, sum the weights of the first 17 digits. 28 * Ai: indicates the ID card number at position I. Digital value wi: indicates the Weighted Factor wi at position I: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 429*2; </LI> 30 * <li> computing mode y = Mod (S, 11) </LI> 31 * <li> use the modulo to obtain the corresponding verification code Y: 0 1 2 3 4 5 6 7 8 9 10: 1 0x9 8 7 6 5 4 3 2 </LI> 32 * </ul> 33*34 * @ Param cardnumber35 * @ return36 */37 38 int sum = 0; 39 For (INT I = 0; I <card_number_length-1; I ++) 40 {41 char CH = source [I]; 42 sum + = (INT) (CH-'0') * verify_code_weight [I]; 43} 44 return verify_code [Sum % 11]; 45} 46 47} 48}

 

At this time, the complete implementation of the ID card constructor becomes

 1 public SocialID(String cardNumber) 2         { 3             if (string.IsNullOrEmpty(cardNumber)) 4                 throw new ApplicationException("Card Number is empty"); 5             if (cardNumber.Length != CARD_NUMBER_LENGTH) 6                 throw new ApplicationException("Card Number Length is wrong."); 7             if (!SOCIAL_NUMBER_PATTERN.IsMatch(cardNumber)) 8                 throw new ApplicationException("Card Number has wrong charactor(s)."); 9 10             if (cardNumber[CARD_NUMBER_LENGTH - 1] != verifier.verify(cardNumber))11                 throw new ApplicationException("Card Number verified code is not match.");12             this.cardNumber = cardNumber;13         }

So far, the code is very clean. Yes, there are further improvements. For example, the extraction of the three elements (Region, birthday, and gender) should be moved to the constructor. The extraction function is changed to simple data reading. The type of social, instead of class, is struct, because it is a typical value object. In addition, I removed the 15-to-18-bit portion, which can be regarded as a utilit and can be done externally, not a core function.

Can you continue?

Finally, let's take a look at the test results:


Complete code:

Socialid. CS

Verifier. CS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.