Generate mobile phone number using Java


Preface

During the project development, I encountered a requirement to randomly generate phone numbers ( in China ), and record the implementation process.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Implementation code

In actual projects, in order to facilitate hierarchical maintenance, enumerations will appear in the code . The enumeration class has no impact on the core code of the current tool class. Readers can decide whether to use enumerations according to their own needs. At the same time, in order to improve the simplicity and readability of the code, I also introduced the Lombok toolkit in the project.

1. Operator enumeration

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 运营商枚举
 *
 * @author zhangrongkang
 * @since 2023/4/2
 */
@Getter
@AllArgsConstructor
public enum OperatorEnum {
    
    

    /**
     * 中国移动
     */
    CHINA_MOBILE(0, "中国移动"),
    /**
     * 中国联通
     */
    CHINA_UNICOM(1, "中国联通"),
    /**
     * 中国电信
     */
    CHINA_TELECOM(2, "中国电信");

    /**
     * 运营商识别码
     */
    private final Integer code;

    /**
     * 运营商名称
     */
    private final String name;

    /**
     * 通过运营商识别码获取运营商名称
     *
     * @param code 运营商识别码
     * @return 运营商名称
     */
    public static String getOperateNameByCode(Integer code) {
    
    
        // 循环遍历所有枚举
        for (OperatorEnum value : OperatorEnum.values()) {
    
    
            // 返回当前运营商名称
            if (value.code.equals(code)) {
    
    
                return value.name;
            }
        }
        return null;
    }
}

2. Mobile phone number tools

import com.zrkizzy.seckill.enums.OperatorEnum;
import java.util.Random;

/**
 * 随机生成电话号码工具类
 *
 * @author zhangrongkang
 * @since 2023/4/2
 */
public class PhoneNumberUtil {
    
    

    /**
     * 中国移动
     */
    private static final String[] CHINA_MOBILE = {
    
    
            "134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159",
            "182", "183", "184", "187", "188", "178", "147", "172", "198"
    };

    /**
     * 中国联通
     */
    private static final String[] CHINA_UNICOM = {
    
    "130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186", "166"};

    /**
     * 中国电信
     */
    private static final String[] CHINA_TELECOM = {
    
    "133", "149", "153", "173", "177", "180", "181", "189", "199"};

    /**
     * 生成手机号方法
     *
     * @param operator 运营商识别码
     */
    public static String createPhoneNumber(int operator) {
    
    
        // 定义随机数对象
        Random random = new Random();
        // 定义StringBuilder对象用于存储生成的手机号
        StringBuilder builder = new StringBuilder();
        // 手机号前三位
        String mobilePrefix = null;
        // 随机生成指定运营商中的手机前三位
        mobilePrefix = switch (operator) {
    
    
            // 中国移动
            case 0 -> CHINA_MOBILE[random.nextInt(CHINA_MOBILE.length)];
            // 中国联通
            case 1 -> CHINA_UNICOM[random.nextInt(CHINA_UNICOM.length)];
            // 中国电信
            case 2 -> CHINA_TELECOM[random.nextInt(CHINA_TELECOM.length)];
            default -> "运营商错误";
        };
        // 拼接手机号前三位
        builder.append(mobilePrefix);
        // 定义辅助变量用于手机号后八位的生成
        int temp;
        // 生成手机号后8位
        for (int i = 0; i < 8; i++) {
    
    
            // 随机生成一个 [0, 9] 以内的整数
            temp = random.nextInt(10);
            // 拼接当前随机数
            builder.append(temp);
        }
        // 将生成的电话号码返回
        return builder.toString();
    }

    public static void main(String[] args) {
    
    
        Random random = new Random();
        for (int i = 1; i <= 10; i++) {
    
    
            // 随机运营商识别码
            int code = random.nextInt(3);
            System.out.println(OperatorEnum.getOperateNameByCode(code) + ": " +createPhoneNumber(code));
        }
    }
}

2. Operation results

10 phone numbers are randomly generated here, and the operator is random
Insert image description here


Summarize

The above is a tool class for generating mobile phone numbers in Java. The implementation logic is relatively simple. The phone number prefix sets of the three major operators are used as member attributes, and then each of the last eight digits of the mobile phone number is randomly generated. Splicing. However, the problem with my generation method is that it cannot ensure that duplicate mobile phone numbers will not be generated. Although the probability of duplication is relatively small , if anyone has better methods and logic, please leave a message in the comment area and make progress together with the bosses. !


Change the time Modify content
2023/04/03 Change the operator's access modifier from public to private

Guess you like

Origin blog.csdn.net/qq_48455576/article/details/129919791