字母数字组合生成邀请码,唯一的递增的

Java生成不重复的邀请码/退换码,数字字母组合,从0A0A到9Z9Z到00A00A到99Z99Z依此类推

/**
 * 生成邀请码工具类
 */
public class CreateCode {
    private static String []str_char = {"A","B","C","D","E","F","G","H",
            "I","J","K","L","M","N","O","P","Q",
            "R","S","T","U","V","W","X","Y","Z"};

    private static String one_str_max_num = "9";
    private static String two_str_max_num = "9";
    private static int str_max_char = 25;


    /**
     * 邀请码
     * @param code
     * @return
     */
    public static String getInviteCode(String code){
        String newCode = "";
        if (StringUtils.isEmpty(code)){
            newCode = "0A0A";
        }else {
            StringBuffer sb = new StringBuffer();

            String []str_string = code.split("\\d");
            String []num_string = code.split("\\D");
            for (int i = 0; i < str_string.length; i++) {
                if (StringUtils.isEmpty(str_string[i])){
                    continue;
                }
                sb.append(str_string[i]);
                if (i != str_string.length -1){
                    sb.append(",");
                }
            }
            str_string = sb.toString().split(",");

            int one_char = 0;
            int two_char = 0;
            for (int j = 0; j < str_char.length; j++) {
                if (str_char[j].equals(str_string[0])){
                    one_char = j;
                    break;
                }
            }
            for (int j = 0; j < str_char.length; j++) {
                if (str_char[j].equals(str_string[1])){
                    two_char = j;
                    break;
                }
            }

            one_str_max_num = "9";
            two_str_max_num = "9";

            for (int i = 1; i < num_string[0].length(); i++) {
                one_str_max_num += "9";
            }
            for (int i = 1; i < num_string[1].length(); i++) {
                two_str_max_num += "9";
            }

            newCode = createCode(num_string[0], one_char, num_string[1], two_char);
        }
        return newCode;
    }

    /**
     * 产生code
     * @param one_char
     * @param two_char
     * @param num_string_one
     * @param num_string_two
     * @return
     */
    private static String createCode(String num_string_one, int one_char, String num_string_two, int two_char){
        int one_max_num = Integer.valueOf(one_str_max_num);
        int two_max_num = Integer.valueOf(two_str_max_num);
        int one_num = Integer.valueOf(num_string_one);
        int two_num = Integer.valueOf(num_string_two);

        if (two_num < two_max_num){
            two_num += 1;
        }else if (two_num == two_max_num && two_char < str_max_char){
            two_num = 0;
            two_char += 1;
        }else if (two_num == two_max_num && two_char == str_max_char && one_num < one_max_num){
            two_num = 0;
            two_char = 0;
            one_num += 1;
        }else if (two_num == two_max_num && two_char == str_max_char && one_num == one_max_num && one_char < str_max_char){
            two_num = 0;
            two_char = 0;
            one_num = 0;
            one_char += 1;
        }else if (two_num == two_max_num && two_char == str_max_char
                && one_num == one_max_num && one_char == str_max_char && one_max_num == two_max_num){
            two_str_max_num += "9";
            two_num = 0;
            two_char = 0;
            one_num = 0;
            one_char = 0;
            return createCode(one_num + "", one_char, two_num + "", two_char);
        }else if (two_num > two_max_num){
            for (int i = 1; i < num_string_two.length(); i++) {
                two_str_max_num += "9";
            }
            return createCode(one_num + "", one_char, two_num + "" , two_char);
        }else if (one_num > one_max_num){
            for (int i = 1; i < num_string_one.length(); i++) {
                one_str_max_num += "9";
            }
            return createCode(one_num + "", one_char, two_num + "", two_char);
        }else if (two_num == two_max_num){
            if (two_char == str_max_char && one_num < two_num){
                one_str_max_num += "9";
                one_num = 0;
                one_char = 0;
                two_num = 0;
                two_char = 0;
            }
            return createCode(one_num + "", one_char, two_num + "", two_char);
        }else if (one_num == one_max_num){
            if (one_char == str_max_char){
                two_str_max_num += "9";
                one_num = 0;
                one_char = 0;
                two_num = 0;
                two_char = 0;
            }
            return createCode(one_num + "", one_char,two_num + "", two_char);
        }
        String one_num_str = one_num + "";
        String two_num_str = two_num + "";
        for (int i = 1;i < one_str_max_num.length(); i++) {
            if (one_num < Math.pow(10, i)){
                one_num_str = "0" + one_num_str;
            }
        }
        for (int i = 1;i < two_str_max_num.length(); i++) {
            if (two_num < Math.pow(10, i)){
                two_num_str = "0" + two_num_str;
            }
        }
        String code =one_num_str + str_char[one_char] + two_num_str + str_char[two_char];
        return code;
    }

    public static void main(String []arg){
        String code = "";
        for (int i = 0;i < 100; i++) {
            code = getInviteCode(code);
            System.out.println("第"+i+"#######code#########"+code);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值