本篇文章為大家展示了JAVA中怎么實現(xiàn)社會統(tǒng)一信用代碼校驗,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
專注于為中小企業(yè)提供網(wǎng)站設計制作、成都做網(wǎng)站服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)敦煌免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
package com.mengyunzhi.common.utils;import org.apache.commons.collections4.BidiMap;import org.apache.commons.collections4.bidimap.TreeBidiMap;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.validation.ValidationException;import java.util.Map;import java.util.Random;/** * 統(tǒng)一社會停用用代碼 */public interface UnifiedCreditCodeUtils { Logger logger = LoggerFactory.getLogger(UnifiedCreditCodeUtils.class); String baseCode = "0123456789ABCDEFGHJKLMNPQRTUWXY"; char[] baseCodeArray = baseCode.toCharArray(); int[] wi = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28}; /** * 生成供較驗使用的 Code Map * * @return BidiMap */ static BidiMap<Character, Integer> generateCodes() { BidiMap<Character, Integer> codes = new TreeBidiMap<>(); for (int i = 0; i < baseCode.length(); i++) { codes.put(baseCodeArray[i], i); } return codes; } /** * 較驗社會統(tǒng)一信用代碼 * * @param unifiedCreditCode 統(tǒng)一社會信息代碼 * @return 符合: true */ static boolean validateUnifiedCreditCode(String unifiedCreditCode) { if ((unifiedCreditCode.equals("")) || unifiedCreditCode.length() != 18) { return false; } Map<Character, Integer> codes = generateCodes(); int parityBit; try { parityBit = getParityBit(unifiedCreditCode, codes); } catch (ValidationException e) { return false; } return parityBit == codes.get(unifiedCreditCode.charAt(unifiedCreditCode.length() - 1)); } /** * 獲取較驗碼 * * @param unifiedCreditCode 統(tǒng)一社會信息代碼 * @param codes 帶有映射關系的國家代碼 * @return 獲取較驗位的值 */ static int getParityBit(String unifiedCreditCode, Map<Character, Integer> codes) { char[] businessCodeArray = unifiedCreditCode.toCharArray(); int sum = 0; for (int i = 0; i < 17; i++) { char key = businessCodeArray[i]; if (baseCode.indexOf(key) == -1) { throw new ValidationException("第" + String.valueOf(i + 1) + "位傳入了非法的字符" + key); } sum += (codes.get(key) * wi[i]); } int result = 31 - sum % 31; return result == 31 ? 0 : result; } /** * 獲取一個隨機的統(tǒng)一社會信用代碼 * * @return 統(tǒng)一社會信用代碼 */ static String generateOneUnifiedCreditCode() { Random random = new Random(); StringBuilder buf = new StringBuilder(); for (int i = 0; i < 17; ++i) { int num = random.nextInt(baseCode.length() - 1); buf.append(baseCode.charAt(num)); } String code = buf.toString(); String upperCode = code.toUpperCase(); BidiMap<Character, Integer> codes = generateCodes(); int parityBit = getParityBit(upperCode, codes); if (codes.getKey(parityBit) == null) { logger.debug("生成社會統(tǒng)一信用代碼不符合規(guī)則"); upperCode = generateOneUnifiedCreditCode(); } else { upperCode = upperCode + codes.getKey(parityBit); } return upperCode; }}
測試
package com.mengyunzhi.common.utils;import org.assertj.core.api.Assertions;import org.junit.Test;import static org.junit.Assert.*;public class UnifiedCreditCodeUtilsTest { @Test public void validateUnifiedCreditCode() { String code = "911310827965850580"; Assertions.assertThat(UnifiedCreditCodeUtils.validateUnifiedCreditCode(code)).isTrue(); }}
上述內(nèi)容就是JAVA中怎么實現(xiàn)社會統(tǒng)一信用代碼校驗,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當前題目:JAVA中怎么實現(xiàn)社會統(tǒng)一信用代碼校驗
轉(zhuǎn)載源于:http://vcdvsql.cn/article10/gdjogo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、移動網(wǎng)站建設、網(wǎng)站設計公司、商城網(wǎng)站、外貿(mào)網(wǎng)站建設、品牌網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)